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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2567 - (hide 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 torben 2409 package dk.daoas.daoadresseservice;
2    
3     import java.net.URLEncoder;
4    
5     import com.google.gson.Gson;
6    
7 torben 2566 import com.google.common.util.concurrent.RateLimiter;
8    
9    
10 torben 2409 import dk.daoas.daoadresseservice.admin.ServiceConfig;
11     import dk.daoas.daoadresseservice.beans.OSMAddress;
12 torben 2411 import dk.daoas.daoadresseservice.beans.SearchRequest;
13 torben 2409 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 torben 2566 RateLimiter limiter;
23 torben 2409
24     public OSMStreetnameHelper(ServiceConfig conf) {
25     this.conf = conf;
26 torben 2566
27     limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund
28 torben 2409 }
29    
30     @Override
31 torben 2411 public String proposeStreetName(SearchRequest request, SearchResult result) {
32 torben 2409 if (conf.useOpenStreetMaps == false)
33     return null;
34    
35 torben 2566
36 torben 2409 result.osm = true;
37    
38     try {
39    
40 torben 2567 OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn );
41 torben 2409 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 torben 2567 RateLimiter limiter;
57 torben 2409
58 torben 2567 public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) {
59     this.limiter = limiter;
60 torben 2409 this.conf = conf;
61     this.postnr= postnr;
62     this.vejnavn = vejnavn;
63     }
64    
65     @Override
66     public String proceed() throws Exception {
67 torben 2567 limiter.acquire();
68    
69    
70 torben 2409 //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