11package com .maxmind .geoip2 ;
22
3- import java .io .Closeable ;
4- import java .io .File ;
53import java .io .IOException ;
64import java .net .InetAddress ;
75import java .util .Arrays ;
2624import com .maxmind .geoip2 .exception .HttpException ;
2725import com .maxmind .geoip2 .exception .InvalidRequestException ;
2826import com .maxmind .geoip2 .exception .OutOfQueriesException ;
29- import com .maxmind .geoip2 .model .CityIspOrg ;
3027import com .maxmind .geoip2 .model .City ;
28+ import com .maxmind .geoip2 .model .CityIspOrg ;
3129import com .maxmind .geoip2 .model .Country ;
3230import com .maxmind .geoip2 .model .Omni ;
3331
10199 * throws a {@link GeoIp2Exception}.
102100 * </p>
103101 */
104- public class WebServiceClient implements Closeable {
102+ public class WebServiceClient {
105103 private final String host ;
106104 private final List <String > languages ;
107105 private final String licenseKey ;
108106 private final int timeout ;
109107 private final HttpTransport transport ;
110108 private final int userId ;
111- private final DatabaseReader fallbackDatabase ;
112109
113110 @ SuppressWarnings ("synthetic-access" )
114111 private WebServiceClient (Builder builder ) {
115- this .fallbackDatabase = builder .fallbackDatabase ;
116112 this .host = builder .host ;
117113 this .languages = builder .languages ;
118114 this .licenseKey = builder .licenseKey ;
@@ -125,11 +121,12 @@ private WebServiceClient(Builder builder) {
125121 * <code>Builder</code> creates instances of
126122 * <code>WebServiceClient</client> from values set by the methods.
127123 *
128- * This example shows how to create a <code>WebServiceClient</code> object with the
129- * <code>Builder</code>:
124+ * This example shows how to create a <code>WebServiceClient</code> object
125+ * with the <code>Builder</code>:
130126 *
131127 * WebServiceClient client = new
132- * WebServiceClient.Builder(12,"licensekey").host("geoip.maxmind.com").build();
128+ * WebServiceClient.Builder(12,"licensekey").host
129+ * ("geoip.maxmind.com").build();
133130 *
134131 * Only the values set in the <code>Builder</code> constructor are required.
135132 */
@@ -141,7 +138,6 @@ public static class Builder {
141138 private List <String > languages = Arrays .asList ("en" );
142139 private int timeout = 3000 ;
143140 private HttpTransport transport = new NetHttpTransport ();
144- private DatabaseReader fallbackDatabase ;
145141
146142 /**
147143 * @param userId
@@ -154,17 +150,6 @@ public Builder(int userId, String licenseKey) {
154150 this .licenseKey = licenseKey ;
155151 }
156152
157- /**
158- * @param val
159- * A fallback GeoIP2 database to use if the the web service
160- * query fails.
161- * @throws IOException
162- */
163- public Builder fallbackDatabase (File val ) throws IOException {
164- this .fallbackDatabase = new DatabaseReader (val );
165- return this ;
166- }
167-
168153 /**
169154 * @param val
170155 * The host to use.
@@ -201,8 +186,8 @@ Builder transport(HttpTransport val) {
201186 }
202187
203188 /**
204- * @return an instance of <code>WebServiceClient</code> created from the fields
205- * set on this builder.
189+ * @return an instance of <code>WebServiceClient</code> created from the
190+ * fields set on this builder.
206191 */
207192 @ SuppressWarnings ("synthetic-access" )
208193 public WebServiceClient build () {
@@ -247,8 +232,7 @@ public City city() throws IOException, GeoIp2Exception {
247232 * @throws GeoIp2Exception
248233 * @throws IOException
249234 */
250- public City city (InetAddress ipAddress ) throws IOException ,
251- GeoIp2Exception {
235+ public City city (InetAddress ipAddress ) throws IOException , GeoIp2Exception {
252236 return this .responseFor ("city" , ipAddress , City .class );
253237 }
254238
@@ -268,10 +252,9 @@ public CityIspOrg cityIspOrg() throws IOException, GeoIp2Exception {
268252 * @throws GeoIp2Exception
269253 * @throws IOException
270254 */
271- public CityIspOrg cityIspOrg (InetAddress ipAddress )
272- throws IOException , GeoIp2Exception {
273- return this .responseFor ("city_isp_org" , ipAddress ,
274- CityIspOrg .class );
255+ public CityIspOrg cityIspOrg (InetAddress ipAddress ) throws IOException ,
256+ GeoIp2Exception {
257+ return this .responseFor ("city_isp_org" , ipAddress , CityIspOrg .class );
275258 }
276259
277260 /**
@@ -290,25 +273,11 @@ public Omni omni() throws IOException, GeoIp2Exception {
290273 * @throws GeoIp2Exception
291274 * @throws IOException
292275 */
293- public Omni omni (InetAddress ipAddress ) throws IOException ,
294- GeoIp2Exception {
276+ public Omni omni (InetAddress ipAddress ) throws IOException , GeoIp2Exception {
295277 return this .responseFor ("omni" , ipAddress , Omni .class );
296278 }
297279
298280 private <T extends Country > T responseFor (String path ,
299- InetAddress ipAddress , Class <T > cls ) throws IOException ,
300- GeoIp2Exception {
301- try {
302- return this .webServiceResponseFor (path , ipAddress , cls );
303- } catch (GeoIp2Exception e ) {
304- if (this .fallbackDatabase != null ) {
305- return this .fallbackDatabase .get (ipAddress );
306- }
307- throw e ;
308- }
309- }
310-
311- private <T extends Country > T webServiceResponseFor (String path ,
312281 InetAddress ipAddress , Class <T > cls ) throws GeoIp2Exception ,
313282 AddressNotFoundException {
314283 GenericUrl uri = this .createUri (path , ipAddress );
@@ -357,7 +326,8 @@ private HttpResponse getResponse(GenericUrl uri) throws GeoIp2Exception,
357326 try {
358327 return request .execute ();
359328 } catch (HttpResponseException e ) {
360- WebServiceClient .handleErrorStatus (e .getContent (), e .getStatusCode (), uri );
329+ WebServiceClient .handleErrorStatus (e .getContent (),
330+ e .getStatusCode (), uri );
361331 throw new AssertionError (
362332 "Something very strange happened. This code should be unreachable." );
363333 } catch (IOException e ) {
@@ -455,17 +425,4 @@ private GenericUrl createUri(String path, InetAddress ipAddress) {
455425 + "/" + (ipAddress == null ? "me" : ipAddress .getHostAddress ()));
456426
457427 }
458-
459- /**
460- * Closes the GeoIP2 fallback database and returns resources to the system.
461- *
462- * @throws IOException
463- * if an I/O error occurs.
464- */
465- @ Override
466- public void close () throws IOException {
467- if (this .fallbackDatabase != null ) {
468- this .fallbackDatabase .close ();
469- }
470- }
471428}
0 commit comments