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 |
|
limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund |
28 |
} |
} |
29 |
|
|
30 |
@Override |
@Override |
32 |
if (conf.useOpenStreetMaps == false) |
if (conf.useOpenStreetMaps == false) |
33 |
return null; |
return null; |
34 |
|
|
35 |
|
|
36 |
result.osm = true; |
result.osm = true; |
37 |
|
|
38 |
try { |
try { |
39 |
|
|
40 |
OSMInvocation wrapper = new OSMInvocation( conf, request.postnr, request.vejnavn ); |
OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn ); |
41 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); |
42 |
|
|
43 |
result.osmVej = (String) breaker.invoke(wrapper); |
result.osmVej = (String) breaker.invoke(wrapper); |
53 |
int postnr; |
int postnr; |
54 |
String vejnavn; |
String vejnavn; |
55 |
ServiceConfig conf; |
ServiceConfig conf; |
56 |
|
RateLimiter limiter; |
57 |
|
|
58 |
public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) { |
public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) { |
59 |
|
this.limiter = limiter; |
60 |
this.conf = conf; |
this.conf = conf; |
61 |
this.postnr= postnr; |
this.postnr= postnr; |
62 |
this.vejnavn = vejnavn; |
this.vejnavn = vejnavn; |
64 |
|
|
65 |
@Override |
@Override |
66 |
public String proceed() throws Exception { |
public String proceed() throws Exception { |
67 |
|
limiter.acquire(); |
68 |
|
|
69 |
|
|
70 |
//TimingHelper timer = new TimingHelper(); |
//TimingHelper timer = new TimingHelper(); |
71 |
String encVej = URLEncoder.encode(vejnavn, "UTF-8"); |
String encVej = URLEncoder.encode(vejnavn, "UTF-8"); |
72 |
|
|
88 |
if (adrList.length != 1) |
if (adrList.length != 1) |
89 |
return null; |
return null; |
90 |
|
|
91 |
if (adrList[0].address != null) { |
if (adrList[0].getAddress() != null) { |
92 |
return adrList[0].address.road; |
return adrList[0].getAddress().getRoad(); |
93 |
} |
} |
94 |
|
|
95 |
return null; |
return null; |