/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2566 - (show annotations) (download)
Thu Jun 4 19:29:39 2015 UTC (8 years, 11 months ago) by torben
File size: 2721 byte(s)
integrate RateLimiter from google guava
1 package dk.daoas.daoadresseservice;
2
3 import java.net.URLEncoder;
4
5 import com.google.gson.Gson;
6
7 import com.google.common.util.concurrent.RateLimiter;
8
9
10 import dk.daoas.daoadresseservice.admin.ServiceConfig;
11 import dk.daoas.daoadresseservice.beans.OSMAddress;
12 import dk.daoas.daoadresseservice.beans.SearchRequest;
13 import dk.daoas.daoadresseservice.beans.SearchResult;
14 import dk.daoas.daoadresseservice.util.HttpUtil;
15 import dk.thoerup.circuitbreaker.CircuitBreaker;
16 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17 import dk.thoerup.circuitbreaker.CircuitInvocation;
18
19 public class OSMStreetnameHelper implements StreetnameHelper {
20
21 ServiceConfig conf;
22 RateLimiter limiter;
23
24 public OSMStreetnameHelper(ServiceConfig conf) {
25 this.conf = conf;
26
27 limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund
28 }
29
30 @Override
31 public String proposeStreetName(SearchRequest request, SearchResult result) {
32 if (conf.useOpenStreetMaps == false)
33 return null;
34
35 limiter.acquire();
36
37 result.osm = true;
38
39 try {
40
41 OSMInvocation wrapper = new OSMInvocation( conf, request.postnr, request.vejnavn );
42 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
43
44 result.osmVej = (String) breaker.invoke(wrapper);
45 return result.osmVej;
46
47 } catch (Exception e) {
48 System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
49 }
50 return null;
51 }
52
53 static class OSMInvocation implements CircuitInvocation {
54 int postnr;
55 String vejnavn;
56 ServiceConfig conf;
57
58 public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) {
59 this.conf = conf;
60 this.postnr= postnr;
61 this.vejnavn = vejnavn;
62 }
63
64 @Override
65 public String proceed() throws Exception {
66 //TimingHelper timer = new TimingHelper();
67 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
68
69
70 String url = conf.nominatimBase + "/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
71
72 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
73 //System.out.println (url);
74
75 String json = HttpUtil.getContentString(url, conf.osmTimeout);
76 //System.out.println(json);
77
78 Gson gson = new Gson();
79 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
80
81 //timer.printElapsed("OSM elapsed");
82
83 //System.out.println("Count: " + adrList.length);
84 if (adrList.length != 1)
85 return null;
86
87 if (adrList[0].address != null) {
88 return adrList[0].address.road;
89 }
90
91 return null;
92 }
93 }
94
95
96 }

  ViewVC Help
Powered by ViewVC 1.1.20