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