1 |
package dk.daoas.daoadresseservice;
|
2 |
|
3 |
import java.util.List;
|
4 |
|
5 |
import com.google.code.geocoder.Geocoder;
|
6 |
import com.google.code.geocoder.GeocoderRequestBuilder;
|
7 |
import com.google.code.geocoder.model.GeocodeResponse;
|
8 |
import com.google.code.geocoder.model.GeocoderAddressComponent;
|
9 |
import com.google.code.geocoder.model.GeocoderRequest;
|
10 |
import com.google.code.geocoder.model.GeocoderResult;
|
11 |
import com.google.code.geocoder.model.GeocoderStatus;
|
12 |
|
13 |
import com.google.common.util.concurrent.RateLimiter;
|
14 |
|
15 |
import dk.daoas.daoadresseservice.admin.ServiceConfig;
|
16 |
import dk.daoas.daoadresseservice.beans.SearchRequest;
|
17 |
import dk.daoas.daoadresseservice.beans.SearchResult;
|
18 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
19 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
20 |
import dk.thoerup.circuitbreaker.CircuitInvocation;
|
21 |
|
22 |
public class GoogleStreetnameHelper implements StreetnameHelper {
|
23 |
|
24 |
ServiceConfig conf;
|
25 |
RateLimiter limiter;
|
26 |
|
27 |
public GoogleStreetnameHelper(ServiceConfig conf) {
|
28 |
this.conf = conf;
|
29 |
|
30 |
limiter = RateLimiter.create( 10.0 ); //max 10 req /sec
|
31 |
}
|
32 |
|
33 |
@Override
|
34 |
public String proposeStreetName(SearchRequest request, SearchResult result) {
|
35 |
if (conf.useGoogle == false)
|
36 |
return null;
|
37 |
|
38 |
limiter.acquire();
|
39 |
|
40 |
result.google = true;
|
41 |
|
42 |
try {
|
43 |
GoogleInvocation wrapper = new GoogleInvocation( conf, request.postnr, request.vejnavn );
|
44 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google");
|
45 |
|
46 |
|
47 |
result.googleVej = (String) breaker.invoke(wrapper);
|
48 |
return result.googleVej;
|
49 |
|
50 |
|
51 |
} catch (Exception e) {
|
52 |
System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() );
|
53 |
}
|
54 |
return null;
|
55 |
}
|
56 |
|
57 |
static class GoogleInvocation implements CircuitInvocation {
|
58 |
ServiceConfig conf;
|
59 |
int postnr;
|
60 |
String vejnavn;
|
61 |
|
62 |
public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
|
63 |
this.conf = conf;
|
64 |
this.postnr= postnr;
|
65 |
this.vejnavn = vejnavn;
|
66 |
|
67 |
}
|
68 |
|
69 |
@Override
|
70 |
public String proceed() throws Exception {
|
71 |
|
72 |
final Geocoder geocoder;
|
73 |
if ( conf.googleApiKey != null ) {
|
74 |
geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
|
75 |
} else {
|
76 |
geocoder = new Geocoder();
|
77 |
}
|
78 |
//TimingHelper timer = new TimingHelper();
|
79 |
|
80 |
String search = vejnavn + ", " + postnr + ", Denmark";
|
81 |
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
|
82 |
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
|
83 |
|
84 |
//timer.printElapsed("Google elapsed");
|
85 |
|
86 |
//System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
|
87 |
if ( geocoderResponse.getStatus() != GeocoderStatus.OK) {
|
88 |
|
89 |
if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
|
90 |
return null;
|
91 |
} else {
|
92 |
System.out.println("Google responded with " + geocoderResponse.getStatus() );
|
93 |
//Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
|
94 |
throw new Exception("Google responded with " + geocoderResponse.getStatus() );
|
95 |
}
|
96 |
|
97 |
}
|
98 |
|
99 |
List<GeocoderResult> resList = geocoderResponse.getResults();
|
100 |
//System.out.println( "Count: " + resList.size() );
|
101 |
|
102 |
if (resList.size() != 1) {
|
103 |
return null;
|
104 |
}
|
105 |
GeocoderResult res = resList.get(0);
|
106 |
|
107 |
List<GeocoderAddressComponent> compList = res.getAddressComponents();
|
108 |
for (GeocoderAddressComponent c : compList) {
|
109 |
//System.out.println(c);
|
110 |
if (c.getTypes().contains("route")) {
|
111 |
return c.getLongName();
|
112 |
}
|
113 |
}
|
114 |
return null;
|
115 |
}
|
116 |
|
117 |
}
|
118 |
|
119 |
|
120 |
}
|