--- dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/23 15:20:10 2351 +++ dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/23 15:49:00 2352 @@ -16,6 +16,9 @@ import dk.daoas.daoadresseservice.admin.ServiceConfig; import dk.daoas.daoadresseservice.beans.OSMAddress; import dk.daoas.daoadresseservice.util.HttpUtil; +import dk.thoerup.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.circuitbreaker.CircuitInvocation; public class GeocodeHelper { @@ -42,7 +45,46 @@ public static String openstreetmapHelper(int postnr, String vejnavn) { try { - + + OSMInvocation wrapper = new OSMInvocation( postnr, vejnavn ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); + + return (String) breaker.invoke(wrapper); + + + } catch (Exception e) { + System.out.println( "Error: " + e.getMessage() ); + } + return null; + } + + + + public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) { + try { + GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google"); + + return (String) breaker.invoke(wrapper); + + } catch (Exception e) { + System.out.println( "GoogleError: " + e.getMessage() ); + } + return null; + } + + + public static class OSMInvocation implements CircuitInvocation { + int postnr; + String vejnavn; + + public OSMInvocation(int postnr, String vejnavn) { + this.postnr= postnr; + this.vejnavn = vejnavn; + } + + @Override + public String proceed() throws Exception { String encVej = URLEncoder.encode(vejnavn, "UTF-8"); String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1"; @@ -63,18 +105,26 @@ return adrList[0].address.road; } - - } catch (Exception e) { - System.out.println( "Error: " + e.getMessage() ); - } - return null; + return null; + } } - - public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) { - try { - //Todo: Load api key from context config + public static class GoogleInvocation implements CircuitInvocation { + ServiceConfig conf; + int postnr; + String vejnavn; + + public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) { + this.conf = conf; + this.postnr= postnr; + this.vejnavn = vejnavn; + + } + + @Override + public String proceed() throws Exception { + final Geocoder geocoder; if ( conf.googleApiKey != null ) { geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException @@ -86,12 +136,19 @@ String search = vejnavn + ", " + postnr + ", Denmark"; GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest); - + //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<"); if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { System.out.println("Google responded with " + geocoderResponse.getStatus() ); - return null; + + if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) { + return null; + } else { + //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren + throw new Exception("Google responded with " + geocoderResponse.getStatus() ); + } + } List resList = geocoderResponse.getResults(); @@ -109,11 +166,8 @@ return c.getLongName(); } } - - } catch (Exception e) { - System.out.println( "GoogleError: " + e.getMessage() ); + return null; } - return null; }