Skip to content

Commit ae11689

Browse files
committed
Removed fallback code from web service client
1 parent e00efd1 commit ae11689

File tree

5 files changed

+47
-119
lines changed

5 files changed

+47
-119
lines changed

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ CHANGELOG
1212
`WebServiceException`.
1313
* A `DatabaseReader` class was added to the distribution. This reads GeoIP2
1414
databases and returns similar product object to `WebServiceClient`.
15-
* A `fallbackDatabase` option was added to `WebServiceClient.Builder`.
16-
This allows you to specify a database to fall back to if the web service
17-
request fails.
1815

1916
0.2.0 (2013-06-13)
2017
------------------

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,38 @@ which represents part of the data returned by the web service.
5050

5151
See the API documentation for more details.
5252

53-
## Example ##
53+
## Web Service Example ##
5454

5555
```java
5656

5757
WebServiceClient client = new WebServiceClient.Builder(42, "abcfe12345").build();
5858

5959
Omni omni = client.omni(InetAddress.getByName("24.24.24.24"));
6060

61-
CityRecord city = omni.getCity();
62-
System.out.println(city.getName());
61+
CountryRecord countryRecord = omni.getCountry();
62+
System.out.println(countryRecord.getName());
6363

64-
PostalRecord postal = omni.getPostal();
65-
System.out.println(postal.getCode());
64+
PostalRecord postalRecord = omni.getPostal();
65+
System.out.println(postalRecord.getCode());
6666

6767
```
6868

69+
## Database Example ##
70+
71+
```java
72+
73+
DatabaseReader reader = new DatabaseReader(new File("/path/to/GeoIP2-City.mmdb");
74+
75+
City city = reader.get(InetAddress.getByName("24.24.24.24"));
76+
77+
System.out.println(city.getCountry().getName());
78+
79+
PostalRecord postalRecord = city.getPostal();
80+
System.out.println(postalRecord.getCode());
81+
82+
```
83+
84+
6985
## Exceptions ##
7086

7187
For details on the possible errors returned by the web service itself, [see

src/main/java/com/maxmind/geoip2/WebServiceClient.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.maxmind.geoip2;
22

3-
import java.io.Closeable;
4-
import java.io.File;
53
import java.io.IOException;
64
import java.net.InetAddress;
75
import java.util.Arrays;
@@ -26,8 +24,8 @@
2624
import com.maxmind.geoip2.exception.HttpException;
2725
import com.maxmind.geoip2.exception.InvalidRequestException;
2826
import com.maxmind.geoip2.exception.OutOfQueriesException;
29-
import com.maxmind.geoip2.model.CityIspOrg;
3027
import com.maxmind.geoip2.model.City;
28+
import com.maxmind.geoip2.model.CityIspOrg;
3129
import com.maxmind.geoip2.model.Country;
3230
import com.maxmind.geoip2.model.Omni;
3331

@@ -101,18 +99,16 @@
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
}

src/test/java/com/maxmind/geoip2/database/ReaderTest.java renamed to src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.maxmind.geoip2.database;
1+
package com.maxmind.geoip2;
22

33
import static org.hamcrest.CoreMatchers.containsString;
44
import static org.junit.Assert.assertEquals;
@@ -12,41 +12,41 @@
1212
import org.junit.Test;
1313
import org.junit.rules.ExpectedException;
1414

15-
import com.maxmind.geoip2.DatabaseReader;
1615
import com.maxmind.geoip2.exception.AddressNotFoundException;
17-
import com.maxmind.geoip2.exception.GeoIp2Exception;
1816
import com.maxmind.geoip2.model.City;
1917
import com.maxmind.geoip2.model.Omni;
2018

2119
@SuppressWarnings("static-method")
22-
public class ReaderTest {
20+
public class DatabaseReaderTest {
2321

2422
@Rule
2523
public ExpectedException exception = ExpectedException.none();
2624

2725
@Test
2826
public void testDefaultLanguage() throws IOException,
2927
AddressNotFoundException {
30-
DatabaseReader reader = new DatabaseReader(new File("test-data/GeoIP2-City.mmdb"));
28+
DatabaseReader reader = new DatabaseReader(new File(
29+
"test-data/GeoIP2-City.mmdb"));
3130
City city = reader.get(InetAddress.getByName("81.2.69.160"));
3231
assertEquals("London", city.getCity().getName());
3332
reader.close();
3433
}
3534

3635
@Test
3736
public void testLanguageList() throws IOException, AddressNotFoundException {
38-
DatabaseReader reader = new DatabaseReader(new File("test-data/GeoIP2-City.mmdb"),
39-
Arrays.asList("xx", "ru", "pt-BR", "es", "en"));
37+
DatabaseReader reader = new DatabaseReader(new File(
38+
"test-data/GeoIP2-City.mmdb"), Arrays.asList("xx", "ru",
39+
"pt-BR", "es", "en"));
4040
Omni city = reader.get(InetAddress.getByName("81.2.69.160"));
4141
assertEquals("Лондон", city.getCity().getName());
42-
System.out.println(city.getTraits().getIpAddress());
4342
reader.close();
4443

4544
}
4645

4746
@Test
4847
public void hasIpAddress() throws IOException, AddressNotFoundException {
49-
DatabaseReader reader = new DatabaseReader(new File("test-data/GeoIP2-City.mmdb"));
48+
DatabaseReader reader = new DatabaseReader(new File(
49+
"test-data/GeoIP2-City.mmdb"));
5050
City city = reader.get(InetAddress.getByName("81.2.69.160"));
5151
assertEquals("81.2.69.160", city.getTraits().getIpAddress());
5252
reader.close();
@@ -58,7 +58,8 @@ public void unknownAddress() throws IOException, AddressNotFoundException {
5858
this.exception
5959
.expectMessage(containsString("The address 10.10.10.10 is not in the database."));
6060

61-
DatabaseReader reader = new DatabaseReader(new File("test-data/GeoIP2-City.mmdb"));
61+
DatabaseReader reader = new DatabaseReader(new File(
62+
"test-data/GeoIP2-City.mmdb"));
6263
@SuppressWarnings("unused")
6364
City city = reader.get(InetAddress.getByName("10.10.10.10"));
6465
reader.close();

src/test/java/com/maxmind/geoip2/FallbackTest.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)