/[projects]/dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java
ViewVC logotype

Diff of /dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2349 by torben, Mon Feb 23 15:02:48 2015 UTC revision 2364 by torben, Tue Feb 24 11:48:58 2015 UTC
# Line 16  import com.google.gson.Gson; Line 16  import com.google.gson.Gson;
16  import dk.daoas.daoadresseservice.admin.ServiceConfig;  import dk.daoas.daoadresseservice.admin.ServiceConfig;
17  import dk.daoas.daoadresseservice.beans.OSMAddress;  import dk.daoas.daoadresseservice.beans.OSMAddress;
18  import dk.daoas.daoadresseservice.util.HttpUtil;  import dk.daoas.daoadresseservice.util.HttpUtil;
19    import dk.thoerup.circuitbreaker.CircuitBreaker;
20    import dk.thoerup.circuitbreaker.CircuitBreakerManager;
21    import dk.thoerup.circuitbreaker.CircuitInvocation;
22    
23  public class GeocodeHelper {  public class GeocodeHelper {
24                                    
# Line 42  public class GeocodeHelper { Line 45  public class GeocodeHelper {
45          public static String openstreetmapHelper(int postnr, String vejnavn) {          public static String openstreetmapHelper(int postnr, String vejnavn) {
46                                    
47                  try {                  try {
48                                    
49                            OSMInvocation wrapper = new OSMInvocation( postnr, vejnavn );
50                            CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm");
51    
52                            return (String) breaker.invoke(wrapper);
53    
54                            
55                    } catch (Exception e) {
56                            System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() );
57                    }                                                              
58                    return null;
59            }
60            
61            
62            
63            public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {
64                    try {
65                            GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn );
66                            CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google");
67    
68                            return (String) breaker.invoke(wrapper);
69                            
70                    } catch (Exception e) {
71                            System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() );
72                    }
73                    return null;    
74            }
75            
76    
77            public static class OSMInvocation implements CircuitInvocation {
78                    int postnr;
79                    String vejnavn;
80                    
81                    public OSMInvocation(int postnr, String vejnavn) {
82                            this.postnr= postnr;
83                            this.vejnavn = vejnavn;                
84                    }
85                    
86                    @Override
87                    public String proceed() throws Exception {
88                            //TimingHelper timer = new TimingHelper();
89                          String encVej = URLEncoder.encode(vejnavn, "UTF-8");                          String encVej = URLEncoder.encode(vejnavn, "UTF-8");
90                                                    
91                          String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";                          String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1";
# Line 55  public class GeocodeHelper { Line 98  public class GeocodeHelper {
98                          Gson gson = new Gson();                          Gson gson = new Gson();
99                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);                          OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class);
100                                                    
101                            //timer.printElapsed("OSM elapsed");
102                            
103                          //System.out.println("Count: " + adrList.length);                          //System.out.println("Count: " + adrList.length);
104                          if (adrList.length != 1)                          if (adrList.length != 1)
105                                  return null;                                  return null;
# Line 63  public class GeocodeHelper { Line 108  public class GeocodeHelper {
108                                  return adrList[0].address.road;                                  return adrList[0].address.road;
109                          }                          }
110    
111                                                    return null;
112                  } catch (Exception e) {                  }
                         System.out.println( "Error: " + e.getMessage() );  
                 }                                                                
                 return null;  
113          }          }
114                    
115                    
116                    public static class GoogleInvocation implements CircuitInvocation {
117          public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) {                  ServiceConfig conf;
118                  try {                  int postnr;
119                          //Todo: Load api key from context config                  String vejnavn;
120                    
121                    public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) {
122                            this.conf = conf;
123                            this.postnr= postnr;
124                            this.vejnavn = vejnavn;
125                            
126                    }
127    
128                    @Override
129                    public String proceed() throws Exception {
130                            
131                          final Geocoder geocoder;                          final Geocoder geocoder;
132                          if ( conf.googleApiKey != null ) {                          if ( conf.googleApiKey != null ) {
133                                  geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException                                  geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException
134                          } else {                          } else {
135                                  geocoder = new Geocoder();                                  geocoder = new Geocoder();
136                          }                          }
137                                                    //TimingHelper timer = new TimingHelper();
138                                                    
139                          String search = vejnavn + ", " + postnr + ", Denmark";                          String search = vejnavn + ", " + postnr + ", Denmark";
140                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();                          GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest();
141                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);                          GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
142                                                            
143                            //timer.printElapsed("Google elapsed");
144                                                    
145                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");                          //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<");
146                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                          if (  geocoderResponse.getStatus() != GeocoderStatus.OK) {                              
147                                  System.out.println("Google responded with " + geocoderResponse.getStatus() );                                  
148                                  return null;                                  if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) {
149                                            return null;
150                                    } else {
151                                            System.out.println("Google responded with " + geocoderResponse.getStatus() );
152                                            //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren
153                                            throw new Exception("Google responded with " + geocoderResponse.getStatus() );
154                                    }
155                                    
156                          }                          }
157                                                    
158                          List<GeocoderResult> resList = geocoderResponse.getResults();                          List<GeocoderResult> resList = geocoderResponse.getResults();
# Line 109  public class GeocodeHelper { Line 170  public class GeocodeHelper {
170                                          return c.getLongName();                                          return c.getLongName();
171                                  }                                  }
172                          }                          }
173                                                    return null;
                 } catch (Exception e) {  
                         System.out.println( "GoogleError: " + e.getMessage() );  
174                  }                  }
                 return null;  
175                                    
176          }          }
177    

Legend:
Removed from v.2349  
changed lines
  Added in v.2364

  ViewVC Help
Powered by ViewVC 1.1.20