4 |
|
|
5 |
import com.google.gson.Gson; |
import com.google.gson.Gson; |
6 |
|
|
7 |
|
import com.google.common.util.concurrent.RateLimiter; |
8 |
|
|
9 |
|
|
10 |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
11 |
import dk.daoas.daoadresseservice.beans.OSMAddress; |
import dk.daoas.daoadresseservice.beans.OSMAddress; |
12 |
import dk.daoas.daoadresseservice.beans.SearchRequest; |
import dk.daoas.daoadresseservice.beans.SearchRequest; |
19 |
public class OSMStreetnameHelper implements StreetnameHelper { |
public class OSMStreetnameHelper implements StreetnameHelper { |
20 |
|
|
21 |
ServiceConfig conf; |
ServiceConfig conf; |
22 |
|
RateLimiter limiter; |
23 |
|
|
24 |
public OSMStreetnameHelper(ServiceConfig conf) { |
public OSMStreetnameHelper(ServiceConfig conf) { |
25 |
this.conf = conf; |
this.conf = conf; |
26 |
|
|
27 |
|
// http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy |
28 |
|
limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund |
29 |
} |
} |
30 |
|
|
31 |
@Override |
@Override |
33 |
if (conf.useOpenStreetMaps == false) |
if (conf.useOpenStreetMaps == false) |
34 |
return null; |
return null; |
35 |
|
|
36 |
|
|
37 |
result.osm = true; |
result.osm = true; |
38 |
|
|
39 |
try { |
try { |
40 |
|
|
41 |
OSMInvocation wrapper = new OSMInvocation( conf, request.postnr, request.vejnavn ); |
OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn ); |
42 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); |
43 |
|
|
44 |
result.osmVej = (String) breaker.invoke(wrapper); |
result.osmVej = (String) breaker.invoke(wrapper); |
54 |
int postnr; |
int postnr; |
55 |
String vejnavn; |
String vejnavn; |
56 |
ServiceConfig conf; |
ServiceConfig conf; |
57 |
|
RateLimiter limiter; |
58 |
|
|
59 |
public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) { |
public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) { |
60 |
|
this.limiter = limiter; |
61 |
this.conf = conf; |
this.conf = conf; |
62 |
this.postnr= postnr; |
this.postnr= postnr; |
63 |
this.vejnavn = vejnavn; |
this.vejnavn = vejnavn; |
65 |
|
|
66 |
@Override |
@Override |
67 |
public String proceed() throws Exception { |
public String proceed() throws Exception { |
68 |
|
limiter.acquire(); |
69 |
|
|
70 |
|
|
71 |
//TimingHelper timer = new TimingHelper(); |
//TimingHelper timer = new TimingHelper(); |
72 |
String encVej = URLEncoder.encode(vejnavn, "UTF-8"); |
String encVej = URLEncoder.encode(vejnavn, "UTF-8"); |
73 |
|
|
89 |
if (adrList.length != 1) |
if (adrList.length != 1) |
90 |
return null; |
return null; |
91 |
|
|
92 |
if (adrList[0].address != null) { |
if (adrList[0].getAddress() != null) { |
93 |
return adrList[0].address.road; |
return adrList[0].getAddress().getRoad(); |
94 |
} |
} |
95 |
|
|
96 |
return null; |
return null; |