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

  ViewVC Help
Powered by ViewVC 1.1.20