Skip to content

Commit f7182fd

Browse files
committed
Merge pull request #16 from maxmind/greg/new-dbs
Greg/new dbs
2 parents de562af + 1f33fb4 commit f7182fd

File tree

15 files changed

+447
-71
lines changed

15 files changed

+447
-71
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ See the API documentation for more details.
9999

100100
## Database Example ##
101101

102+
### City ###
102103
```java
103104

104105
// A File object pointing to your GeoIP2 or GeoLite2 database
@@ -128,6 +129,66 @@ System.out.println(response.getLocation().getLongitude()); // -93.2323
128129

129130
```
130131

132+
### Connection-Type ###
133+
134+
```java
135+
136+
// A File object pointing to your GeoIP2 Connection-Type database
137+
File database = new File("/path/to/GeoIP2-Connection-Type.mmdb");
138+
139+
// This creates the DatabaseReader object, which should be reused across
140+
// lookups.
141+
DatabaseReader reader = new DatabaseReader.Builder(database).build();
142+
143+
144+
ConnectionTypeResponse response = reader.connectionType(
145+
InetAddress.getByName("128.101.101.101"));
146+
147+
// getConnectionType() returns a ConnectionType enum
148+
ConnectionType type = response.getConnectionType();
149+
150+
System.out.println(type); // 'Corporate'
151+
```
152+
153+
### Domain ###
154+
155+
```java
156+
157+
// A File object pointing to your GeoIP2 Domain database
158+
File database = new File("/path/to/GeoIP2-Domain.mmdb");
159+
160+
// This creates the DatabaseReader object, which should be reused across
161+
// lookups.
162+
DatabaseReader reader = new DatabaseReader.Builder(database).build();
163+
164+
165+
DomainResponse response = reader.domain(
166+
InetAddress.getByName("128.101.101.101"));
167+
168+
System.out.println(response.getDomain()); // 'Corporate'
169+
```
170+
171+
### ISP ###
172+
173+
```java
174+
175+
// A File object pointing to your GeoIP2 ISP database
176+
File database = new File("/path/to/GeoIP2-ISP.mmdb");
177+
178+
// This creates the DatabaseReader object, which should be reused across
179+
// lookups.
180+
DatabaseReader reader = new DatabaseReader.Builder(database).build();
181+
182+
183+
IspResponse response = reader.isp(InetAddress.getByName("128.101.101.101"));
184+
185+
System.out.println(response.getAutonomousSystemNumber()); // 217
186+
System.out.println(response.getAutonomousSystemOrganization()); // 'University of Minnesota'
187+
System.out.println(response.getIsp()); // 'University of Minnesota'
188+
System.out.println(response.getOrganization()); // 'University of Minnesota'
189+
190+
```
191+
131192
## Exceptions ##
132193

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

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import com.maxmind.geoip2.exception.GeoIp2Exception;
1919
import com.maxmind.geoip2.model.CityIspOrgResponse;
2020
import com.maxmind.geoip2.model.CityResponse;
21+
import com.maxmind.geoip2.model.ConnectionTypeResponse;
2122
import com.maxmind.geoip2.model.CountryResponse;
23+
import com.maxmind.geoip2.model.DomainResponse;
24+
import com.maxmind.geoip2.model.IspResponse;
2225
import com.maxmind.geoip2.model.OmniResponse;
2326

2427
/**
@@ -45,6 +48,8 @@ public class DatabaseReader implements GeoIp2Provider, Closeable {
4548
this.om = new ObjectMapper();
4649
this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
4750
false);
51+
this.om.configure(
52+
DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
4853
InjectableValues inject = new InjectableValues.Std().addValue(
4954
"locales", builder.locales);
5055
this.om.setInjectableValues(inject);
@@ -54,11 +59,10 @@ public class DatabaseReader implements GeoIp2Provider, Closeable {
5459
* Constructs a Builder for the DatabaseReader. The file passed to it must
5560
* be a valid GeoIP2 database file.
5661
*
57-
* <code>Builder</code> creates instances of
58-
* <code>DatabaseReader</code> from values set by the methods.
62+
* <code>Builder</code> creates instances of <code>DatabaseReader</code>
63+
* from values set by the methods.
5964
*
60-
* Only the values set in the <code>Builder</code> constructor are
61-
* required.
65+
* Only the values set in the <code>Builder</code> constructor are required.
6266
*/
6367
public final static class Builder {
6468
final File database;
@@ -117,7 +121,8 @@ public Builder fileMode(FileMode val) {
117121
/**
118122
* @return an instance of <code>DatabaseReader</code> created from the
119123
* fields set on this builder.
120-
* @throws IOException if there is an error reading the database
124+
* @throws IOException
125+
* if there is an error reading the database
121126
*/
122127
public DatabaseReader build() throws IOException {
123128
return new DatabaseReader(this);
@@ -135,6 +140,11 @@ public DatabaseReader build() throws IOException {
135140
*/
136141
private <T> T get(InetAddress ipAddress, Class<T> cls) throws IOException,
137142
AddressNotFoundException {
143+
return this.get(ipAddress, cls, true);
144+
}
145+
146+
private <T> T get(InetAddress ipAddress, Class<T> cls, boolean hasTraits)
147+
throws IOException, AddressNotFoundException {
138148
ObjectNode node = (ObjectNode) this.reader.get(ipAddress);
139149

140150
// We throw the same exception as the web service when an IP is not in
@@ -144,11 +154,16 @@ private <T> T get(InetAddress ipAddress, Class<T> cls) throws IOException,
144154
+ ipAddress.getHostAddress() + " is not in the database.");
145155
}
146156

147-
if (!node.has("traits")) {
148-
node.put("traits", this.om.createObjectNode());
157+
ObjectNode ipNode;
158+
if (hasTraits) {
159+
if (!node.has("traits")) {
160+
node.put("traits", this.om.createObjectNode());
161+
}
162+
ipNode = (ObjectNode) node.get("traits");
163+
} else {
164+
ipNode = node;
149165
}
150-
ObjectNode traits = (ObjectNode) node.get("traits");
151-
traits.put("ip_address", ipAddress.getHostAddress());
166+
ipNode.put("ip_address", ipAddress.getHostAddress());
152167

153168
// The cast and the Omni.class are sort of ugly. There might be a
154169
// better way
@@ -189,4 +204,19 @@ public OmniResponse omni(InetAddress ipAddress) throws IOException,
189204
GeoIp2Exception {
190205
return this.get(ipAddress, OmniResponse.class);
191206
}
207+
208+
public ConnectionTypeResponse connectionType(InetAddress ipAddress)
209+
throws IOException, GeoIp2Exception {
210+
return this.get(ipAddress, ConnectionTypeResponse.class, false);
211+
}
212+
213+
public DomainResponse domain(InetAddress ipAddress) throws IOException,
214+
GeoIp2Exception {
215+
return this.get(ipAddress, DomainResponse.class, false);
216+
}
217+
218+
public IspResponse isp(InetAddress ipAddress) throws IOException,
219+
GeoIp2Exception {
220+
return this.get(ipAddress, IspResponse.class, false);
221+
}
192222
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public interface GeoIp2Provider {
1515
* @param ipAddress
1616
* IPv4 or IPv6 address to lookup.
1717
* @return A Country model for the requested IP address.
18-
* @throws GeoIp2Exception if there is an error looking up the IP
19-
* @throws IOException if there is an IO error
18+
* @throws GeoIp2Exception
19+
* if there is an error looking up the IP
20+
* @throws IOException
21+
* if there is an IO error
2022
*/
2123
public CountryResponse country(InetAddress ipAddress) throws IOException,
2224
GeoIp2Exception;
@@ -25,8 +27,10 @@ public CountryResponse country(InetAddress ipAddress) throws IOException,
2527
* @param ipAddress
2628
* IPv4 or IPv6 address to lookup.
2729
* @return A City model for the requested IP address.
28-
* @throws GeoIp2Exception if there is an error looking up the IP
29-
* @throws IOException if there is an IO error
30+
* @throws GeoIp2Exception
31+
* if there is an error looking up the IP
32+
* @throws IOException
33+
* if there is an IO error
3034
*/
3135
public CityResponse city(InetAddress ipAddress) throws IOException,
3236
GeoIp2Exception;
@@ -35,8 +39,10 @@ public CityResponse city(InetAddress ipAddress) throws IOException,
3539
* @param ipAddress
3640
* IPv4 or IPv6 address to lookup.
3741
* @return A CityIspOrg model for the requested IP address.
38-
* @throws GeoIp2Exception if there is an error looking up the IP
39-
* @throws IOException if there is an IO error
42+
* @throws GeoIp2Exception
43+
* if there is an error looking up the IP
44+
* @throws IOException
45+
* if there is an IO error
4046
*/
4147
public CityIspOrgResponse cityIspOrg(InetAddress ipAddress)
4248
throws IOException, GeoIp2Exception;
@@ -45,8 +51,10 @@ public CityIspOrgResponse cityIspOrg(InetAddress ipAddress)
4551
* @param ipAddress
4652
* IPv4 or IPv6 address to lookup.
4753
* @return An Omni model for the requested IP address.
48-
* @throws GeoIp2Exception if there is an error looking up the IP
49-
* @throws IOException if there is an IO error
54+
* @throws GeoIp2Exception
55+
* if there is an error looking up the IP
56+
* @throws IOException
57+
* if there is an IO error
5058
*/
5159
public OmniResponse omni(InetAddress ipAddress) throws IOException,
5260
GeoIp2Exception;

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public class WebServiceClient implements GeoIp2Provider {
118118
}
119119

120120
/**
121-
* <code>Builder</code> creates instances of
122-
* <code>WebServiceClient</code> from values set by the methods.
121+
* <code>Builder</code> creates instances of <code>WebServiceClient</code>
122+
* from values set by the methods.
123123
*
124-
* This example shows how to create a <code>WebServiceClient</code> object
124+
* This example shows how to create a <code>WebServiceClient</code> object
125125
* with the <code>Builder</code>:
126126
*
127127
* WebServiceClient client = new
@@ -183,7 +183,8 @@ public Builder timeout(int val) {
183183
}
184184

185185
/**
186-
* @param val Transport for unit testing.
186+
* @param val
187+
* Transport for unit testing.
187188
* @return Builder object
188189
*/
189190
Builder testTransport(HttpTransport val) {
@@ -202,8 +203,10 @@ public WebServiceClient build() {
202203

203204
/**
204205
* @return A Country model for the requesting IP address
205-
* @throws GeoIp2Exception if there is an error from the web service
206-
* @throws IOException if an IO error happens during the request
206+
* @throws GeoIp2Exception
207+
* if there is an error from the web service
208+
* @throws IOException
209+
* if an IO error happens during the request
207210
*/
208211
public CountryResponse country() throws IOException, GeoIp2Exception {
209212
return this.country(null);
@@ -217,8 +220,10 @@ public CountryResponse country(InetAddress ipAddress) throws IOException,
217220

218221
/**
219222
* @return A City model for the requesting IP address
220-
* @throws GeoIp2Exception if there is an error from the web service
221-
* @throws IOException if an IO error happens during the request
223+
* @throws GeoIp2Exception
224+
* if there is an error from the web service
225+
* @throws IOException
226+
* if an IO error happens during the request
222227
*/
223228
public CityResponse city() throws IOException, GeoIp2Exception {
224229
return this.city(null);
@@ -232,8 +237,10 @@ public CityResponse city(InetAddress ipAddress) throws IOException,
232237

233238
/**
234239
* @return A City/ISP/Org model for the requesting IP address
235-
* @throws GeoIp2Exception if there is an error from the web service
236-
* @throws IOException if an IO error happens during the request
240+
* @throws GeoIp2Exception
241+
* if there is an error from the web service
242+
* @throws IOException
243+
* if an IO error happens during the request
237244
*/
238245
public CityIspOrgResponse cityIspOrg() throws IOException, GeoIp2Exception {
239246
return this.cityIspOrg(null);
@@ -248,8 +255,10 @@ public CityIspOrgResponse cityIspOrg(InetAddress ipAddress)
248255

249256
/**
250257
* @return An Omni model for the requesting IP address
251-
* @throws GeoIp2Exception if there is an error from the web service
252-
* @throws IOException if an IO error happens during the request
258+
* @throws GeoIp2Exception
259+
* if there is an error from the web service
260+
* @throws IOException
261+
* if an IO error happens during the request
253262
*/
254263
public OmniResponse omni() throws IOException, GeoIp2Exception {
255264
return this.omni(null);
@@ -291,9 +300,11 @@ private <T> T responseFor(String path, InetAddress ipAddress, Class<T> cls)
291300

292301
private HttpResponse getResponse(GenericUrl uri) throws GeoIp2Exception,
293302
IOException {
294-
// We only use a instance variable for HttpTransport when unit testing as
303+
// We only use a instance variable for HttpTransport when unit testing
304+
// as
295305
// HttpTransport is not thread safe.
296-
HttpTransport transport = this.testTransport == null ? new NetHttpTransport() : this.testTransport;
306+
HttpTransport transport = this.testTransport == null ? new NetHttpTransport()
307+
: this.testTransport;
297308
HttpRequestFactory requestFactory = transport.createRequestFactory();
298309
HttpRequest request;
299310
try {

src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public String toString() {
7979
+ (this.getPostal() != null ? "getPostal()=" + this.getPostal()
8080
+ ", " : "")
8181
+ (this.getSubdivisions() != null ? "getSubdivisionsList()="
82-
+ this.getSubdivisions() + ", "
83-
: "")
82+
+ this.getSubdivisions() + ", " : "")
8483
+ (this.getContinent() != null ? "getContinent()="
8584
+ this.getContinent() + ", " : "")
8685
+ (this.getCountry() != null ? "getCountry()="

src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package com.maxmind.geoip2.model;
22

3-
import java.io.IOException;
4-
5-
import com.fasterxml.jackson.annotation.JsonInclude.Include;
63
import com.fasterxml.jackson.annotation.JsonProperty;
7-
import com.fasterxml.jackson.databind.ObjectMapper;
84
import com.maxmind.geoip2.record.Continent;
95
import com.maxmind.geoip2.record.Country;
106
import com.maxmind.geoip2.record.MaxMind;
117
import com.maxmind.geoip2.record.RepresentedCountry;
128
import com.maxmind.geoip2.record.Traits;
139

14-
abstract class AbstractCountryResponse {
10+
abstract class AbstractCountryResponse extends AbstractResponse {
1511
@JsonProperty
1612
private Continent continent = new Continent();
1713

@@ -80,19 +76,6 @@ public Traits getTraits() {
8076
return this.traits;
8177
}
8278

83-
/**
84-
* @return JSON representation of this object. The structure is the same as
85-
* the JSON provided by the GeoIP2 web service.
86-
* @throws IOException
87-
* if there is an error serializing the object to JSON.
88-
*/
89-
public String toJson() throws IOException {
90-
ObjectMapper mapper = new ObjectMapper();
91-
mapper.setSerializationInclusion(Include.NON_NULL);
92-
mapper.setSerializationInclusion(Include.NON_EMPTY);
93-
return mapper.writeValueAsString(this);
94-
}
95-
9679
/*
9780
* (non-Javadoc)
9881
*
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.maxmind.geoip2.model;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
public class AbstractResponse {
9+
10+
/**
11+
* @return JSON representation of this object. The structure is the same as
12+
* the JSON provided by the GeoIP2 web service.
13+
* @throws IOException
14+
* if there is an error serializing the object to JSON.
15+
*/
16+
public String toJson() throws IOException {
17+
ObjectMapper mapper = new ObjectMapper();
18+
mapper.setSerializationInclusion(Include.NON_NULL);
19+
mapper.setSerializationInclusion(Include.NON_EMPTY);
20+
return mapper.writeValueAsString(this);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)