/[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 2567 - (show annotations) (download)
Sun Jun 7 18:09:16 2015 UTC (8 years, 11 months ago) by torben
File size: 2808 byte(s)
use ratelimiter AFTER service health has been checked with circuitbreaker
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
36 result.osm = true;
37
38 try {
39
40 OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn );
41 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
42
43 result.osmVej = (String) breaker.invoke(wrapper);
44 return result.osmVej;
45
46 } catch (Exception e) {
47 System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
48 }
49 return null;
50 }
51
52 static class OSMInvocation implements CircuitInvocation {
53 int postnr;
54 String vejnavn;
55 ServiceConfig conf;
56 RateLimiter limiter;
57
58 public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) {
59 this.limiter = limiter;
60 this.conf = conf;
61 this.postnr= postnr;
62 this.vejnavn = vejnavn;
63 }
64
65 @Override
66 public String proceed() throws Exception {
67 limiter.acquire();
68
69
70 //TimingHelper timer = new TimingHelper();
71 String encVej = URLEncoder.encode(vejnavn, "UTF-8");
72
73
74 String url = conf.nominatimBase + "/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
75
76 //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1";
77 //System.out.println (url);
78
79 String json = HttpUtil.getContentString(url, conf.osmTimeout);
80 //System.out.println(json);
81
82 Gson gson = new Gson();
83 OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
84
85 //timer.printElapsed("OSM elapsed");
86
87 //System.out.println("Count: " + adrList.length);
88 if (adrList.length != 1)
89 return null;
90
91 if (adrList[0].address != null) {
92 return adrList[0].address.road;
93 }
94
95 return null;
96 }
97 }
98
99
100 }

  ViewVC Help
Powered by ViewVC 1.1.20