From 417b82b1aaf1ed467e9d48786b2fd722d3c74d59 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 12:14:31 -0700 Subject: [PATCH 01/37] Use stream in tests as appears intended --- src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 4049505d..2272e386 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -83,7 +83,7 @@ public void testLocaleListFile() throws Exception { @Test public void testLocaleListURL() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) + try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipStream) .locales(Arrays.asList("xx", "ru", "pt-BR", "es", "en")) .build() ) { @@ -108,7 +108,7 @@ public void testMemoryModeFile() throws Exception { @Test public void testMemoryModeURL() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) + try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipStream) .fileMode(Reader.FileMode.MEMORY).build() ) { this.testMemoryMode(reader); @@ -140,7 +140,7 @@ public void hasIpAddressFile() throws Exception { @Test public void hasIpAddressURL() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) + try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipStream) .build() ) { this.hasIpInfo(reader); @@ -165,7 +165,7 @@ public void unknownAddressFile() throws Exception { @Test public void unknownAddressURL() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) + try (DatabaseReader reader = new DatabaseReader.Builder(this.geoipStream) .build() ) { this.unknownAddress(reader); From 453f63df87f6cae84ef225035b85d31db2fddc60 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 12:14:58 -0700 Subject: [PATCH 02/37] Do not test confidence from database, but test more fields --- src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 2272e386..40cb8081 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -291,8 +291,9 @@ public void testCountry() throws Exception { InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); CountryResponse response = reader.country(ipAddress); - assertEquals(99, response.getCountry().getConfidence().intValue()); + assertEquals("NA", response.getContinent().getCode()); assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); + assertEquals(6252001, response.getRegisteredCountry().getGeoNameId().intValue()); assertEquals(ipAddress.getHostAddress(), response.getTraits().getIpAddress()); assertEquals("74.209.16.0/20", response.getTraits().getNetwork().toString()); From 1a64e0ab10c620e92ff4dde56c62f72b7345ba02 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 12:15:42 -0700 Subject: [PATCH 03/37] Support ASN, Country, and City database lookups without Jackson --- .../com/maxmind/geoip2/DatabaseProvider.java | 123 +++++-- .../com/maxmind/geoip2/DatabaseReader.java | 342 +++++++++++++++--- .../com/maxmind/geoip2/GeoIp2Provider.java | 19 +- .../geoip2/model/AsnDatabaseModel.java | 26 ++ .../com/maxmind/geoip2/model/AsnResponse.java | 15 +- .../geoip2/model/CityDatabaseModel.java | 85 +++++ .../maxmind/geoip2/model/CityResponse.java | 125 +++++++ .../geoip2/model/CountryDatabaseModel.java | 51 +++ .../maxmind/geoip2/model/CountryResponse.java | 93 +++++ .../geoip2/record/CityDatabaseRecord.java | 28 ++ .../record/ContinentDatabaseRecord.java | 35 ++ .../geoip2/record/CountryDatabaseRecord.java | 45 +++ .../com/maxmind/geoip2/record/Location.java | 17 +- .../com/maxmind/geoip2/record/Postal.java | 8 +- .../RepresentedCountryDatabaseRecord.java | 52 +++ .../record/SubdivisionDatabaseRecord.java | 35 ++ .../geoip2/record/TraitsDatabaseRecord.java | 156 ++++++++ .../maxmind/geoip2/DatabaseReaderTest.java | 254 +++++++------ 18 files changed, 1315 insertions(+), 194 deletions(-) create mode 100644 src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java create mode 100644 src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java create mode 100644 src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java create mode 100644 src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java create mode 100644 src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java create mode 100644 src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java create mode 100644 src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java create mode 100644 src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java create mode 100644 src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java diff --git a/src/main/java/com/maxmind/geoip2/DatabaseProvider.java b/src/main/java/com/maxmind/geoip2/DatabaseProvider.java index 3fcab05e..955a8855 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseProvider.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseProvider.java @@ -6,6 +6,7 @@ import java.util.Optional; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; public interface DatabaseProvider extends GeoIp2Provider { @@ -16,8 +17,13 @@ public interface DatabaseProvider extends GeoIp2Provider { * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - Optional tryCountry(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryCountry(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * @param ipAddress IPv4 or IPv6 address to lookup. @@ -25,8 +31,13 @@ Optional tryCountry(InetAddress ipAddress) throws IOException, * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - Optional tryCity(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryCity(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Anonymous IP. @@ -36,8 +47,13 @@ Optional tryCity(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + AnonymousIpResponse anonymousIp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Anonymous IP. @@ -47,8 +63,13 @@ AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryAnonymousIp(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryAnonymousIp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoLite2 ASN database. @@ -58,8 +79,13 @@ Optional tryAnonymousIp(InetAddress ipAddress) throws IOExc * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - AsnResponse asn(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + AsnResponse asn(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoLite2 ASN database. @@ -69,8 +95,13 @@ AsnResponse asn(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryAsn(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryAsn(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Connection Type database. @@ -81,7 +112,12 @@ Optional tryAsn(InetAddress ipAddress) throws IOException, * @throws java.io.IOException if there is an IO error */ ConnectionTypeResponse connectionType(InetAddress ipAddress) - throws IOException, GeoIp2Exception; + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Connection Type database. @@ -92,7 +128,12 @@ ConnectionTypeResponse connectionType(InetAddress ipAddress) * @throws java.io.IOException if there is an IO error */ Optional tryConnectionType(InetAddress ipAddress) - throws IOException, GeoIp2Exception; + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Domain database. @@ -102,8 +143,13 @@ Optional tryConnectionType(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - DomainResponse domain(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + DomainResponse domain(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Domain database. @@ -113,8 +159,13 @@ DomainResponse domain(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryDomain(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryDomain(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Enterprise database. @@ -124,8 +175,13 @@ Optional tryDomain(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + EnterpriseResponse enterprise(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 Enterprise database. @@ -135,8 +191,13 @@ EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryEnterprise(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryEnterprise(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 ISP database. @@ -146,8 +207,13 @@ Optional tryEnterprise(InetAddress ipAddress) throws IOExcep * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - IspResponse isp(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + IspResponse isp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * Look up an IP address in a GeoIP2 ISP database. @@ -157,6 +223,11 @@ IspResponse isp(InetAddress ipAddress) throws IOException, * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryIsp(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + Optional tryIsp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; } diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 0b63977d..816f905e 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -12,6 +12,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.InstantiationException; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.util.Collections; import java.util.List; @@ -88,7 +90,12 @@ private enum DatabaseType { } } - private DatabaseReader(Builder builder) throws IOException { + private DatabaseReader(Builder builder) + throws IOException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { if (builder.stream != null) { this.reader = new Reader(builder.stream, builder.cache); } else if (builder.database != null) { @@ -222,11 +229,40 @@ public Builder fileMode(FileMode val) { * fields set on this builder. * @throws IOException if there is an error reading the database */ - public DatabaseReader build() throws IOException { + public DatabaseReader build() + throws IOException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return new DatabaseReader(this); } } + static final class LookupResult { + final T model; + final String ipAddress; + final Network network; + + LookupResult(T model, String ipAddress, Network network) { + this.model = model; + this.ipAddress = ipAddress; + this.network = network; + } + + T getModel() { + return this.model; + } + + String getIpAddress() { + return this.ipAddress; + } + + Network getNetwork() { + return this.network; + } + } + /** * @param ipAddress IPv4 or IPv6 address to lookup. * @param cls The class to deserialize to. @@ -236,7 +272,13 @@ public DatabaseReader build() throws IOException { * @throws AddressNotFoundException if the IP address is not in our database */ private T getOrThrowException(InetAddress ipAddress, Class cls, - DatabaseType expectedType) throws IOException, AddressNotFoundException { + DatabaseType expectedType) + throws IOException, + AddressNotFoundException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { Optional t = get(ipAddress, cls, expectedType, 1); if (!t.isPresent()) { throw new AddressNotFoundException("The address " @@ -258,7 +300,13 @@ private T getOrThrowException(InetAddress ipAddress, Class cls, * @throws IOException if there is an error opening or reading from the file. */ private Optional get(InetAddress ipAddress, Class cls, - DatabaseType expectedType, int stackDepth) throws IOException, AddressNotFoundException { + DatabaseType expectedType, int stackDepth) + throws IOException, + AddressNotFoundException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { if ((databaseType & expectedType.type) == 0) { String caller = Thread.currentThread().getStackTrace()[2 + stackDepth] @@ -270,19 +318,40 @@ private Optional get(InetAddress ipAddress, Class cls, // We are using the fully qualified name as otherwise it is ambiguous // on Java 14 due to the new java.lang.Record. - com.maxmind.db.Record record = reader.getRecord(ipAddress); - - ObjectNode node = jsonNodeToObjectNode(record.getData()); + com.maxmind.db.Record record = reader.getRecord(ipAddress, cls); - if (node == null) { + T o = cls.cast(record.getData()); + if (o == null) { return Optional.empty(); } + return Optional.of(o); + } - InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress(), record.getNetwork()); + private LookupResult get2(InetAddress ipAddress, Class cls, + DatabaseType expectedType, int stackDepth) + throws IOException, + AddressNotFoundException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { - return Optional.of(this.om.reader(inject).treeToValue(node, cls)); - } + if ((databaseType & expectedType.type) == 0) { + String caller = Thread.currentThread().getStackTrace()[2 + stackDepth] + .getMethodName(); + throw new UnsupportedOperationException( + "Invalid attempt to open a " + getMetadata().getDatabaseType() + + " database using the " + caller + " method"); + } + + // We are using the fully qualified name as otherwise it is ambiguous + // on Java 14 due to the new java.lang.Record. + com.maxmind.db.Record record = reader.getRecord(ipAddress, cls); + T o = cls.cast(record.getData()); + + return new LookupResult<>(o, ipAddress.getHostAddress(), record.getNetwork()); + } private ObjectNode jsonNodeToObjectNode(JsonNode node) throws InvalidDatabaseException { @@ -313,27 +382,115 @@ public void close() throws IOException { } @Override - public CountryResponse country(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.getOrThrowException(ipAddress, CountryResponse.class, DatabaseType.COUNTRY); + public CountryResponse country(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + Optional r = getCountry(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override - public Optional tryCountry(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.get(ipAddress, CountryResponse.class, DatabaseType.COUNTRY, 0); + public Optional tryCountry(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + return getCountry(ipAddress, 0); + } + + private Optional getCountry( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + CountryDatabaseModel.class, + DatabaseType.COUNTRY, + stackDepth + ); + CountryDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new CountryResponse( + model, + result.getIpAddress(), + result.getNetwork(), + locales + ) + ); } @Override - public CityResponse city(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.getOrThrowException(ipAddress, CityResponse.class, DatabaseType.CITY); + public CityResponse city(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + Optional r = getCity(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override - public Optional tryCity(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.get(ipAddress, CityResponse.class, DatabaseType.CITY, 0); + public Optional tryCity(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + return getCity(ipAddress, 0); + } + + private Optional getCity( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + CityDatabaseModel.class, + DatabaseType.CITY, + stackDepth + ); + CityDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new CityResponse( + model, + result.getIpAddress(), + result.getNetwork(), + locales + ) + ); } /** @@ -345,14 +502,24 @@ public Optional tryCity(InetAddress ipAddress) throws IOException, * @throws IOException if there is an IO error */ @Override - public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public AnonymousIpResponse anonymousIp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.getOrThrowException(ipAddress, AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP); } @Override - public Optional tryAnonymousIp(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public Optional tryAnonymousIp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.get(ipAddress, AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP, 0); } @@ -365,15 +532,56 @@ public Optional tryAnonymousIp(InetAddress ipAddress) throw * @throws IOException if there is an IO error */ @Override - public AsnResponse asn(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.getOrThrowException(ipAddress, AsnResponse.class, DatabaseType.ASN); + public AsnResponse asn(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + Optional r = getAsn(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override - public Optional tryAsn(InetAddress ipAddress) throws IOException, - GeoIp2Exception { - return this.get(ipAddress, AsnResponse.class, DatabaseType.ASN, 0); + public Optional tryAsn(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + return getAsn(ipAddress, 0); + } + + private Optional getAsn(InetAddress ipAddress, int stackDepth) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + AsnDatabaseModel.class, + DatabaseType.ASN, + stackDepth + ); + AsnDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new AsnResponse( + model, + result.getIpAddress(), + result.getNetwork() + ) + ); } /** @@ -386,14 +594,24 @@ public Optional tryAsn(InetAddress ipAddress) throws IOException, */ @Override public ConnectionTypeResponse connectionType(InetAddress ipAddress) - throws IOException, GeoIp2Exception { + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.getOrThrowException(ipAddress, ConnectionTypeResponse.class, DatabaseType.CONNECTION_TYPE); } @Override public Optional tryConnectionType(InetAddress ipAddress) - throws IOException, GeoIp2Exception { + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.get(ipAddress, ConnectionTypeResponse.class, DatabaseType.CONNECTION_TYPE, 0); } @@ -407,15 +625,25 @@ public Optional tryConnectionType(InetAddress ipAddress) * @throws IOException if there is an IO error */ @Override - public DomainResponse domain(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public DomainResponse domain(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this .getOrThrowException(ipAddress, DomainResponse.class, DatabaseType.DOMAIN); } @Override - public Optional tryDomain(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public Optional tryDomain(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this .get(ipAddress, DomainResponse.class, DatabaseType.DOMAIN, 0); } @@ -429,14 +657,24 @@ public Optional tryDomain(InetAddress ipAddress) throws IOExcept * @throws IOException if there is an IO error */ @Override - public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public EnterpriseResponse enterprise(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.getOrThrowException(ipAddress, EnterpriseResponse.class, DatabaseType.ENTERPRISE); } @Override - public Optional tryEnterprise(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public Optional tryEnterprise(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.get(ipAddress, EnterpriseResponse.class, DatabaseType.ENTERPRISE, 0); } @@ -450,14 +688,24 @@ public Optional tryEnterprise(InetAddress ipAddress) throws * @throws IOException if there is an IO error */ @Override - public IspResponse isp(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public IspResponse isp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.getOrThrowException(ipAddress, IspResponse.class, DatabaseType.ISP); } @Override - public Optional tryIsp(InetAddress ipAddress) throws IOException, - GeoIp2Exception { + public Optional tryIsp(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { return this.get(ipAddress, IspResponse.class, DatabaseType.ISP, 0); } diff --git a/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java b/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java index 9243d203..8ebbb3a3 100644 --- a/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java +++ b/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java @@ -5,6 +5,7 @@ import com.maxmind.geoip2.model.CountryResponse; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; public interface GeoIp2Provider { @@ -15,8 +16,13 @@ public interface GeoIp2Provider { * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - CountryResponse country(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + CountryResponse country(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; /** * @param ipAddress IPv4 or IPv6 address to lookup. @@ -24,6 +30,11 @@ CountryResponse country(InetAddress ipAddress) throws IOException, * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - CityResponse city(InetAddress ipAddress) throws IOException, - GeoIp2Exception; + CityResponse city(InetAddress ipAddress) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException; } diff --git a/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java new file mode 100644 index 00000000..9ab98a43 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java @@ -0,0 +1,26 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +public class AsnDatabaseModel { + private final Long autonomousSystemNumber; + private final String autonomousSystemOrganization; + + @MaxMindDbConstructor + public AsnDatabaseModel ( + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization + ) { + this.autonomousSystemNumber = autonomousSystemNumber; + this.autonomousSystemOrganization = autonomousSystemOrganization; + } + + public Long getAutonomousSystemNumber() { + return this.autonomousSystemNumber; + } + + public String getAutonomousSystemOrganization() { + return this.autonomousSystemOrganization; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java index 22eb7ce5..7e888fd5 100644 --- a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java @@ -19,7 +19,7 @@ public class AsnResponse extends AbstractResponse { private final Network network; AsnResponse() { - this(null, null, null); + this(null, null, null, null); } public AsnResponse( @@ -42,6 +42,19 @@ public AsnResponse( this.network = network; } + public AsnResponse( + AsnDatabaseModel model, + String ipAddress, + Network network + ) { + this( + model.getAutonomousSystemNumber().intValue(), + model.getAutonomousSystemOrganization(), + ipAddress, + network + ); + } + /** * @return The autonomous system number associated with the IP address. */ diff --git a/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java new file mode 100644 index 00000000..6674ca45 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java @@ -0,0 +1,85 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; +import com.maxmind.geoip2.record.CityDatabaseRecord; +import com.maxmind.geoip2.record.ContinentDatabaseRecord; +import com.maxmind.geoip2.record.CountryDatabaseRecord; +import com.maxmind.geoip2.record.Location; +import com.maxmind.geoip2.record.Postal; +import com.maxmind.geoip2.record.RepresentedCountryDatabaseRecord; +import com.maxmind.geoip2.record.SubdivisionDatabaseRecord; +import com.maxmind.geoip2.record.TraitsDatabaseRecord; + +import java.util.ArrayList; + +public class CityDatabaseModel { + private final CityDatabaseRecord city; + private final ContinentDatabaseRecord continent; + private final CountryDatabaseRecord country; + private final Location location; + private final Postal postal; + private final CountryDatabaseRecord registeredCountry; + private final RepresentedCountryDatabaseRecord representedCountry; + private final ArrayList subdivisions; + private final TraitsDatabaseRecord traits; + + @MaxMindDbConstructor + public CityDatabaseModel( + @MaxMindDbParameter(name="city") CityDatabaseRecord city, + @MaxMindDbParameter(name="continent") ContinentDatabaseRecord continent, + @MaxMindDbParameter(name="country") CountryDatabaseRecord country, + @MaxMindDbParameter(name="location") Location location, + @MaxMindDbParameter(name="postal") Postal postal, + @MaxMindDbParameter(name="registered_country") CountryDatabaseRecord registeredCountry, + @MaxMindDbParameter(name="represented_country") RepresentedCountryDatabaseRecord representedCountry, + @MaxMindDbParameter(name="subdivisions") ArrayList subdivisions, + @MaxMindDbParameter(name="traits") TraitsDatabaseRecord traits + ) { + this.city = city; + this.continent = continent; + this.country = country; + this.location = location; + this.postal = postal; + this.registeredCountry = registeredCountry; + this.representedCountry = representedCountry; + this.subdivisions = subdivisions; + this.traits = traits; + } + + public CityDatabaseRecord getCity() { + return this.city; + } + + public ContinentDatabaseRecord getContinent() { + return this.continent; + } + + public CountryDatabaseRecord getCountry() { + return this.country; + } + + public Location getLocation() { + return this.location; + } + + public Postal getPostal() { + return this.postal; + } + + public CountryDatabaseRecord getRegisteredCountry() { + return this.registeredCountry; + } + + public RepresentedCountryDatabaseRecord getRepresentedCountry() { + return this.representedCountry; + } + + public ArrayList getSubdivisions() { + return this.subdivisions; + } + + public TraitsDatabaseRecord getTraits() { + return this.traits; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index 2ad5409b..c1abd851 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -2,9 +2,12 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; +import java.net.InetAddress; import java.util.ArrayList; +import java.util.List; /** *

@@ -42,4 +45,126 @@ public CityResponse( super(city, continent, country, location, maxmind, postal, registeredCountry, representedCountry, subdivisions, traits); } + + public CityResponse( + CityDatabaseModel model, + String ipAddress, + Network network, + List locales + ) { + this( + model.getCity() != null ? + new City( + locales, + null, + model.getCity().getGeoNameId().intValue(), + model.getCity().getNames() + ) : null, + model.getContinent() != null ? + new Continent( + locales, + model.getContinent().getCode(), + model.getContinent().getGeoNameId().intValue(), + model.getContinent().getNames() + ) : null, + model.getCountry() != null ? + new Country( + locales, + null, + model.getCountry().getGeoNameId().intValue(), + model.getCountry().getIsInEuropeanUnion(), + model.getCountry().getIsoCode(), + model.getCountry().getNames() + ) : null, + model.getLocation(), + null, + model.getPostal(), + model.getRegisteredCountry() != null ? + new Country( + locales, + null, + model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getIsInEuropeanUnion(), + model.getRegisteredCountry().getIsoCode(), + model.getRegisteredCountry().getNames() + ) : null, + model.getRepresentedCountry() != null ? + new RepresentedCountry( + locales, + null, + model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getIsInEuropeanUnion(), + model.getRepresentedCountry().getIsoCode(), + model.getRepresentedCountry().getNames(), + model.getRepresentedCountry().getType() + ) : null, + mapSubdivisions(locales, model.getSubdivisions()), + model.getTraits() != null ? + new Traits( + model.getTraits().getAutonomousSystemNumber(), + model.getTraits().getAutonomousSystemOrganization(), + model.getTraits().getConnectionType(), + model.getTraits().getDomain(), + ipAddress, + model.getTraits().isAnonymous(), + model.getTraits().isAnonymousProxy(), + model.getTraits().isAnonymousVpn(), + model.getTraits().isHostingProvider(), + model.getTraits().isLegitimateProxy(), + model.getTraits().isPublicProxy(), + model.getTraits().isSatelliteProvider(), + model.getTraits().isTorExitNode(), + model.getTraits().getIsp(), + network, + model.getTraits().getOrganization(), + model.getTraits().getUserType(), + model.getTraits().getUserCount(), + model.getTraits().getStaticIpScore() + ) : + new Traits( + null, + null, + null, + null, + ipAddress, + false, + false, + false, + false, + false, + false, + false, + false, + null, + network, + null, + null, + null, + null + ) + ); + } + + private static ArrayList mapSubdivisions( + List locales, + ArrayList subdivisions + ) { + if (subdivisions == null) { + return null; + } + + ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); + for (SubdivisionDatabaseRecord subdivision : subdivisions) { + subdivisions2.add( + new Subdivision( + locales, + null, + subdivision.getGeoNameId().intValue(), + subdivision.getIsoCode(), + subdivision.getNames() + ) + ); + } + return subdivisions2; + } } diff --git a/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java new file mode 100644 index 00000000..36b50fd4 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java @@ -0,0 +1,51 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; +import com.maxmind.geoip2.record.ContinentDatabaseRecord; +import com.maxmind.geoip2.record.CountryDatabaseRecord; +import com.maxmind.geoip2.record.RepresentedCountryDatabaseRecord; +import com.maxmind.geoip2.record.TraitsDatabaseRecord; + +public class CountryDatabaseModel { + private final ContinentDatabaseRecord continent; + private final CountryDatabaseRecord country; + private final CountryDatabaseRecord registeredCountry; + private final RepresentedCountryDatabaseRecord representedCountry; + private final TraitsDatabaseRecord traits; + + @MaxMindDbConstructor + public CountryDatabaseModel( + @MaxMindDbParameter(name="continent") ContinentDatabaseRecord continent, + @MaxMindDbParameter(name="country") CountryDatabaseRecord country, + @MaxMindDbParameter(name="registered_country") CountryDatabaseRecord registeredCountry, + @MaxMindDbParameter(name="represented_country") RepresentedCountryDatabaseRecord representedCountry, + @MaxMindDbParameter(name="traits") TraitsDatabaseRecord traits + ) { + this.continent = continent; + this.country = country; + this.registeredCountry = registeredCountry; + this.representedCountry = representedCountry; + this.traits = traits; + } + + public ContinentDatabaseRecord getContinent() { + return this.continent; + } + + public CountryDatabaseRecord getCountry() { + return this.country; + } + + public CountryDatabaseRecord getRegisteredCountry() { + return this.registeredCountry; + } + + public RepresentedCountryDatabaseRecord getRepresentedCountry() { + return this.representedCountry; + } + + public TraitsDatabaseRecord getTraits() { + return this.traits; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index f45efd10..dfe11c3f 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -2,8 +2,12 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; +import java.net.InetAddress; +import java.util.List; + /** * This class provides a model for the data returned by the GeoIP2 Precision: * Country end point. @@ -27,4 +31,93 @@ public CountryResponse( ) { super(continent, country, maxmind, registeredCountry, representedCountry, traits); } + + public CountryResponse( + CountryDatabaseModel model, + String ipAddress, + Network network, + List locales + ) { + this( + model.getContinent() != null ? + new Continent( + locales, + model.getContinent().getCode(), + model.getContinent().getGeoNameId().intValue(), + model.getContinent().getNames() + ) : null, + model.getCountry() != null ? + new Country( + locales, + null, + model.getCountry().getGeoNameId().intValue(), + model.getCountry().getIsInEuropeanUnion(), + model.getCountry().getIsoCode(), + model.getCountry().getNames() + ) : null, + null, + model.getRegisteredCountry() != null ? + new Country( + locales, + null, + model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getIsInEuropeanUnion(), + model.getRegisteredCountry().getIsoCode(), + model.getRegisteredCountry().getNames() + ) : null, + model.getRepresentedCountry() != null ? + new RepresentedCountry( + locales, + null, + model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getIsInEuropeanUnion(), + model.getRepresentedCountry().getIsoCode(), + model.getRepresentedCountry().getNames(), + model.getRepresentedCountry().getType() + ) : null, + model.getTraits() != null ? + new Traits( + model.getTraits().getAutonomousSystemNumber(), + model.getTraits().getAutonomousSystemOrganization(), + model.getTraits().getConnectionType(), + model.getTraits().getDomain(), + ipAddress, + model.getTraits().isAnonymous(), + model.getTraits().isAnonymousProxy(), + model.getTraits().isAnonymousVpn(), + model.getTraits().isHostingProvider(), + model.getTraits().isLegitimateProxy(), + model.getTraits().isPublicProxy(), + model.getTraits().isSatelliteProvider(), + model.getTraits().isTorExitNode(), + model.getTraits().getIsp(), + network, + model.getTraits().getOrganization(), + model.getTraits().getUserType(), + model.getTraits().getUserCount(), + model.getTraits().getStaticIpScore() + ) : + new Traits( + null, + null, + null, + null, + ipAddress, + false, + false, + false, + false, + false, + false, + false, + false, + null, + network, + null, + null, + null, + null + ) + ); + } } diff --git a/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java new file mode 100644 index 00000000..9bb82fe3 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java @@ -0,0 +1,28 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +import java.util.Map; + +public final class CityDatabaseRecord { + private final Long geoNameId; + private final Map names; + + @MaxMindDbConstructor + public CityDatabaseRecord( + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="names") Map names + ) { + this.geoNameId = geoNameId; + this.names = names; + } + + public Long getGeoNameId() { + return this.geoNameId; + } + + public Map getNames() { + return this.names; + } +} diff --git a/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java new file mode 100644 index 00000000..57f4fbf5 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java @@ -0,0 +1,35 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +import java.util.Map; + +public final class ContinentDatabaseRecord { + private final String code; + private final Long geoNameId; + private final Map names; + + @MaxMindDbConstructor + public ContinentDatabaseRecord( + @MaxMindDbParameter(name="code") String code, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="names") Map names + ) { + this.code = code; + this.geoNameId = geoNameId; + this.names = names; + } + + public String getCode() { + return this.code; + } + + public Long getGeoNameId() { + return this.geoNameId; + } + + public Map getNames() { + return this.names; + } +} diff --git a/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java new file mode 100644 index 00000000..7fb3e226 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java @@ -0,0 +1,45 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +import java.util.Map; + +public final class CountryDatabaseRecord { + private final Long geoNameId; + private final Boolean isInEuropeanUnion; + private final String isoCode; + private final Map names; + + @MaxMindDbConstructor + public CountryDatabaseRecord( + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names + ) { + this.geoNameId = geoNameId; + this.isInEuropeanUnion = isInEuropeanUnion; + this.isoCode = isoCode; + this.names = names; + } + + public Long getGeoNameId() { + return this.geoNameId; + } + + public boolean getIsInEuropeanUnion() { + if (this.isInEuropeanUnion == null) { + return false; + } + return this.isInEuropeanUnion; + } + + public String getIsoCode() { + return this.isoCode; + } + + public Map getNames() { + return this.names; + } +} diff --git a/src/main/java/com/maxmind/geoip2/record/Location.java b/src/main/java/com/maxmind/geoip2/record/Location.java index 397c429c..ac77dc68 100644 --- a/src/main/java/com/maxmind/geoip2/record/Location.java +++ b/src/main/java/com/maxmind/geoip2/record/Location.java @@ -1,6 +1,8 @@ package com.maxmind.geoip2.record; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; /** *

@@ -21,14 +23,15 @@ public Location() { this(null, null, null, null, null, null, null); } + @MaxMindDbConstructor public Location( - @JsonProperty("accuracy_radius") Integer accuracyRadius, - @JsonProperty("average_income") Integer averageIncome, - @JsonProperty("latitude") Double latitude, - @JsonProperty("longitude") Double longitude, - @JsonProperty("metro_code") Integer metroCode, - @JsonProperty("population_density") Integer populationDensity, - @JsonProperty("time_zone") String timeZone + @JsonProperty("accuracy_radius") @MaxMindDbParameter(name="accuracy_radius") Integer accuracyRadius, + @JsonProperty("average_income") @MaxMindDbParameter(name="average_income") Integer averageIncome, + @JsonProperty("latitude") @MaxMindDbParameter(name="latitude") Double latitude, + @JsonProperty("longitude") @MaxMindDbParameter(name="longitude") Double longitude, + @JsonProperty("metro_code") @MaxMindDbParameter(name="metro_code") Integer metroCode, + @JsonProperty("population_density") @MaxMindDbParameter(name="population_density") Integer populationDensity, + @JsonProperty("time_zone") @MaxMindDbParameter(name="time_zone") String timeZone ) { this.accuracyRadius = accuracyRadius; this.averageIncome = averageIncome; diff --git a/src/main/java/com/maxmind/geoip2/record/Postal.java b/src/main/java/com/maxmind/geoip2/record/Postal.java index 577c30b5..b455aee4 100644 --- a/src/main/java/com/maxmind/geoip2/record/Postal.java +++ b/src/main/java/com/maxmind/geoip2/record/Postal.java @@ -1,6 +1,8 @@ package com.maxmind.geoip2.record; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; /** *

@@ -19,7 +21,11 @@ public Postal() { this(null, null); } - public Postal(@JsonProperty("code") String code, @JsonProperty("confidence") Integer confidence) { + @MaxMindDbConstructor + public Postal( + @JsonProperty("code") @MaxMindDbParameter(name="code") String code, + @JsonProperty("confidence") @MaxMindDbParameter(name="confidence") Integer confidence + ) { this.code = code; this.confidence = confidence; } diff --git a/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java new file mode 100644 index 00000000..e73c037c --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java @@ -0,0 +1,52 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +import java.util.Map; + +public final class RepresentedCountryDatabaseRecord { + private final Long geoNameId; + private final Boolean isInEuropeanUnion; + private final String isoCode; + private final Map names; + private final String type; + + @MaxMindDbConstructor + public RepresentedCountryDatabaseRecord( + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names, + @MaxMindDbParameter(name="type") String type + ) { + this.geoNameId = geoNameId; + this.isInEuropeanUnion = isInEuropeanUnion; + this.isoCode = isoCode; + this.names = names; + this.type = type; + } + + public Long getGeoNameId() { + return this.geoNameId; + } + + public boolean getIsInEuropeanUnion() { + if (this.isInEuropeanUnion == null) { + return false; + } + return this.isInEuropeanUnion; + } + + public String getIsoCode() { + return this.isoCode; + } + + public Map getNames() { + return this.names; + } + + public String getType() { + return this.type; + } +} diff --git a/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java new file mode 100644 index 00000000..e1182eed --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java @@ -0,0 +1,35 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +import java.util.Map; + +public final class SubdivisionDatabaseRecord { + private final Long geoNameId; + private final String isoCode; + private final Map names; + + @MaxMindDbConstructor + public SubdivisionDatabaseRecord( + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names + ) { + this.geoNameId = geoNameId; + this.isoCode = isoCode; + this.names = names; + } + + public Long getGeoNameId() { + return this.geoNameId; + } + + public String getIsoCode() { + return this.isoCode; + } + + public Map getNames() { + return this.names; + } +} diff --git a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java new file mode 100644 index 00000000..558ea127 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java @@ -0,0 +1,156 @@ +package com.maxmind.geoip2.record; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; +import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; + +public final class TraitsDatabaseRecord { + private final Integer autonomousSystemNumber; + private final String autonomousSystemOrganization; + private final ConnectionType connectionType; + private final String domain; + private final Boolean isAnonymous; + private final Boolean isAnonymousProxy; + private final Boolean isAnonymousVpn; + private final Boolean isHostingProvider; + private final Boolean isLegitimateProxy; + private final Boolean isPublicProxy; + private final Boolean isSatelliteProvider; + private final Boolean isTorExitNode; + private final String isp; + private final String organization; + private final String userType; + private final Integer userCount; + private final Double staticIpScore; + + @MaxMindDbConstructor + public TraitsDatabaseRecord( + @MaxMindDbParameter(name="autonomous_system_number") Integer autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, + @MaxMindDbParameter(name="connection_type") ConnectionType connectionType, + @MaxMindDbParameter(name="domain") String domain, + @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, + @MaxMindDbParameter(name="is_anonymous_proxy") Boolean isAnonymousProxy, + @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, + @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, + @MaxMindDbParameter(name="is_legitimate_proxy") Boolean isLegitimateProxy, + @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, + @MaxMindDbParameter(name="is_satellite_provider") Boolean isSatelliteProvider, + @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode, + @MaxMindDbParameter(name="isp") String isp, + @MaxMindDbParameter(name="organization") String organization, + @MaxMindDbParameter(name="static_ip_score") Double staticIpScore, + @MaxMindDbParameter(name="user_count") Integer userCount, + @MaxMindDbParameter(name="user_type") String userType + ) { + this.autonomousSystemNumber = autonomousSystemNumber; + this.autonomousSystemOrganization = autonomousSystemOrganization; + this.connectionType = connectionType; + this.domain = domain; + this.isAnonymous = isAnonymous; + this.isAnonymousProxy = isAnonymousProxy; + this.isAnonymousVpn = isAnonymousVpn; + this.isHostingProvider = isHostingProvider; + this.isLegitimateProxy = isLegitimateProxy; + this.isPublicProxy = isPublicProxy; + this.isSatelliteProvider = isSatelliteProvider; + this.isTorExitNode = isTorExitNode; + this.isp = isp; + this.organization = organization; + this.staticIpScore = staticIpScore; + this.userCount = userCount; + this.userType = userType; + } + + public Integer getAutonomousSystemNumber() { + return this.autonomousSystemNumber; + } + + public String getAutonomousSystemOrganization() { + return this.autonomousSystemOrganization; + } + + public ConnectionType getConnectionType() { + return this.connectionType; + } + + public String getDomain() { + return this.domain; + } + + public boolean isAnonymous() { + if (this.isAnonymous == null) { + return false; + } + return this.isAnonymous; + } + + public boolean isAnonymousProxy() { + if (this.isAnonymousProxy == null) { + return false; + } + return this.isAnonymousProxy; + } + + public boolean isAnonymousVpn() { + if (this.isAnonymousVpn == null) { + return false; + } + return this.isAnonymousVpn; + } + + public boolean isHostingProvider() { + if (this.isHostingProvider == null) { + return false; + } + return this.isHostingProvider; + } + + public boolean isLegitimateProxy() { + if (this.isLegitimateProxy == null) { + return false; + } + return this.isLegitimateProxy; + } + + public boolean isPublicProxy() { + if (this.isPublicProxy == null) { + return false; + } + return this.isPublicProxy; + } + + public boolean isSatelliteProvider() { + if (this.isSatelliteProvider == null) { + return false; + } + return this.isSatelliteProvider; + } + + public boolean isTorExitNode() { + if (this.isTorExitNode == null) { + return false; + } + return this.isTorExitNode; + } + + public String getIsp() { + return this.isp; + } + + public String getOrganization() { + return this.organization; + } + + public Double getStaticIpScore() { + return this.staticIpScore; + } + + public Integer getUserCount() { + return this.userCount; + } + + public String getUserType() { + return this.userType; + } +} diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 40cb8081..362b9a29 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.URISyntaxException; import java.net.URL; @@ -54,8 +55,13 @@ public void testDefaultLocaleURL() throws Exception { } } - private void testDefaultLocale(DatabaseReader reader) throws IOException, - GeoIp2Exception { + private void testDefaultLocale(DatabaseReader reader) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("London", city.getCity().getName()); } @@ -91,8 +97,13 @@ public void testLocaleListURL() throws Exception { } } - private void testLocaleList(DatabaseReader reader) throws IOException, - GeoIp2Exception { + private void testLocaleList(DatabaseReader reader) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("Лондон", city.getCity().getName()); } @@ -115,15 +126,26 @@ public void testMemoryModeURL() throws Exception { } } - private void testMemoryMode(DatabaseReader reader) throws IOException, - GeoIp2Exception { + private void testMemoryMode(DatabaseReader reader) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("London", city.getCity().getName()); assertEquals(100, city.getLocation().getAccuracyRadius().longValue()); } @Test - public void metadata() throws IOException { + public void metadata() + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) .fileMode(Reader.FileMode.MEMORY).build(); assertEquals("GeoIP2-City", reader.getMetadata().getDatabaseType()); @@ -147,8 +169,13 @@ public void hasIpAddressURL() throws Exception { } } - private void hasIpInfo(DatabaseReader reader) throws IOException, - GeoIp2Exception { + private void hasIpInfo(DatabaseReader reader) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { CityResponse cio = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("81.2.69.160", cio.getTraits().getIpAddress()); assertEquals("81.2.69.160/27", cio.getTraits().getNetwork().toString()); @@ -172,8 +199,13 @@ public void unknownAddressURL() throws Exception { } } - private void unknownAddress(DatabaseReader reader) throws IOException, - GeoIp2Exception { + private void unknownAddress(DatabaseReader reader) + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { assertFalse(reader.tryCity(InetAddress.getByName("10.10.10.10")).isPresent()); this.exception.expect(AddressNotFoundException.class); @@ -184,7 +216,13 @@ private void unknownAddress(DatabaseReader reader) throws IOException, } @Test - public void testUnsupportedFileMode() throws IOException { + public void testUnsupportedFileMode() + throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { this.exception.expect(IllegalArgumentException.class); this.exception.expectMessage(containsString("Only FileMode.MEMORY")); try (DatabaseReader db = new DatabaseReader.Builder(this.geoipStream).fileMode( @@ -204,25 +242,25 @@ public void incorrectDatabaseMethod() throws Exception { } - @Test - public void testAnonymousIp() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder( - this.getFile("GeoIP2-Anonymous-IP-Test.mmdb")).build() - ) { - InetAddress ipAddress = InetAddress.getByName("1.2.0.1"); - AnonymousIpResponse response = reader.anonymousIp(ipAddress); - assertTrue(response.isAnonymous()); - assertTrue(response.isAnonymousVpn()); - assertFalse(response.isHostingProvider()); - assertFalse(response.isPublicProxy()); - assertFalse(response.isTorExitNode()); - assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - assertEquals("1.2.0.0/16", response.getNetwork().toString()); - - AnonymousIpResponse tryResponse = reader.tryAnonymousIp(ipAddress).get(); - assertEquals(response.toJson(), tryResponse.toJson()); - } - } + //@Test + //public void testAnonymousIp() throws Exception { + // try (DatabaseReader reader = new DatabaseReader.Builder( + // this.getFile("GeoIP2-Anonymous-IP-Test.mmdb")).build() + // ) { + // InetAddress ipAddress = InetAddress.getByName("1.2.0.1"); + // AnonymousIpResponse response = reader.anonymousIp(ipAddress); + // assertTrue(response.isAnonymous()); + // assertTrue(response.isAnonymousVpn()); + // assertFalse(response.isHostingProvider()); + // assertFalse(response.isPublicProxy()); + // assertFalse(response.isTorExitNode()); + // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + // assertEquals("1.2.0.0/16", response.getNetwork().toString()); + + // AnonymousIpResponse tryResponse = reader.tryAnonymousIp(ipAddress).get(); + // assertEquals(response.toJson(), tryResponse.toJson()); + // } + //} @Test public void testAsn() throws Exception { @@ -265,23 +303,23 @@ public void testCity() throws Exception { } } - @Test - public void testConnectionType() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder( - this.getFile("GeoIP2-Connection-Type-Test.mmdb")).build() - ) { - InetAddress ipAddress = InetAddress.getByName("1.0.1.0"); + //@Test + //public void testConnectionType() throws Exception { + // try (DatabaseReader reader = new DatabaseReader.Builder( + // this.getFile("GeoIP2-Connection-Type-Test.mmdb")).build() + // ) { + // InetAddress ipAddress = InetAddress.getByName("1.0.1.0"); - ConnectionTypeResponse response = reader.connectionType(ipAddress); + // ConnectionTypeResponse response = reader.connectionType(ipAddress); - assertEquals(ConnectionType.CABLE_DSL, response.getConnectionType()); - assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - assertEquals("1.0.1.0/24", response.getNetwork().toString()); + // assertEquals(ConnectionType.CABLE_DSL, response.getConnectionType()); + // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + // assertEquals("1.0.1.0/24", response.getNetwork().toString()); - ConnectionTypeResponse tryResponse = reader.tryConnectionType(ipAddress).get(); - assertEquals(response.toJson(), tryResponse.toJson()); - } - } + // ConnectionTypeResponse tryResponse = reader.tryConnectionType(ipAddress).get(); + // assertEquals(response.toJson(), tryResponse.toJson()); + // } + //} @Test public void testCountry() throws Exception { @@ -302,69 +340,69 @@ public void testCountry() throws Exception { } } - @Test - public void testDomain() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder( - this.getFile("GeoIP2-Domain-Test.mmdb")).build() - ) { - InetAddress ipAddress = InetAddress.getByName("1.2.0.0"); - DomainResponse response = reader.domain(ipAddress); - assertEquals("maxmind.com", response.getDomain()); - assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - assertEquals("1.2.0.0/16", response.getNetwork().toString()); - - DomainResponse tryResponse = reader.tryDomain(ipAddress).get(); - assertEquals(response.toJson(), tryResponse.toJson()); - } - } - - @Test - public void testEnterprise() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder( - getFile("GeoIP2-Enterprise-Test.mmdb")).build() - ) { - InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); - - EnterpriseResponse response = reader.enterprise(ipAddress); - assertEquals(11, response.getCity().getConfidence().intValue()); - assertEquals(99, response.getCountry().getConfidence().intValue()); - assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); - assertEquals(27, response.getLocation().getAccuracyRadius().intValue()); - assertEquals(ConnectionType.CABLE_DSL, response.getTraits().getConnectionType()); - assertTrue(response.getTraits().isLegitimateProxy()); - assertEquals(ipAddress.getHostAddress(), response.getTraits().getIpAddress()); - assertEquals("74.209.16.0/20", response.getTraits().getNetwork().toString()); - - EnterpriseResponse tryResponse = reader.tryEnterprise(ipAddress).get(); - assertEquals(response.toJson(), tryResponse.toJson()); - - // Test that the city and country methods can be called without - // an exception - reader.city(ipAddress); - reader.country(ipAddress); - } - } - - @Test - public void testIsp() throws Exception { - try (DatabaseReader reader = new DatabaseReader.Builder( - this.getFile("GeoIP2-ISP-Test.mmdb")).build() - ) { - InetAddress ipAddress = InetAddress.getByName("1.128.0.0"); - IspResponse response = reader.isp(ipAddress); - assertEquals(1221, response.getAutonomousSystemNumber().intValue()); - assertEquals("Telstra Pty Ltd", - response.getAutonomousSystemOrganization()); - assertEquals("Telstra Internet", response.getIsp()); - assertEquals("Telstra Internet", response.getOrganization()); - - assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - assertEquals("1.128.0.0/11", response.getNetwork().toString()); - - IspResponse tryResponse = reader.tryIsp(ipAddress).get(); - assertEquals(response.toJson(), tryResponse.toJson()); - } - } + //@Test + //public void testDomain() throws Exception { + // try (DatabaseReader reader = new DatabaseReader.Builder( + // this.getFile("GeoIP2-Domain-Test.mmdb")).build() + // ) { + // InetAddress ipAddress = InetAddress.getByName("1.2.0.0"); + // DomainResponse response = reader.domain(ipAddress); + // assertEquals("maxmind.com", response.getDomain()); + // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + // assertEquals("1.2.0.0/16", response.getNetwork().toString()); + + // DomainResponse tryResponse = reader.tryDomain(ipAddress).get(); + // assertEquals(response.toJson(), tryResponse.toJson()); + // } + //} + + //@Test + //public void testEnterprise() throws Exception { + // try (DatabaseReader reader = new DatabaseReader.Builder( + // getFile("GeoIP2-Enterprise-Test.mmdb")).build() + // ) { + // InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); + + // EnterpriseResponse response = reader.enterprise(ipAddress); + // assertEquals(11, response.getCity().getConfidence().intValue()); + // assertEquals(99, response.getCountry().getConfidence().intValue()); + // assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); + // assertEquals(27, response.getLocation().getAccuracyRadius().intValue()); + // assertEquals(ConnectionType.CABLE_DSL, response.getTraits().getConnectionType()); + // assertTrue(response.getTraits().isLegitimateProxy()); + // assertEquals(ipAddress.getHostAddress(), response.getTraits().getIpAddress()); + // assertEquals("74.209.16.0/20", response.getTraits().getNetwork().toString()); + + // EnterpriseResponse tryResponse = reader.tryEnterprise(ipAddress).get(); + // assertEquals(response.toJson(), tryResponse.toJson()); + + // // Test that the city and country methods can be called without + // // an exception + // reader.city(ipAddress); + // reader.country(ipAddress); + // } + //} + + //@Test + //public void testIsp() throws Exception { + // try (DatabaseReader reader = new DatabaseReader.Builder( + // this.getFile("GeoIP2-ISP-Test.mmdb")).build() + // ) { + // InetAddress ipAddress = InetAddress.getByName("1.128.0.0"); + // IspResponse response = reader.isp(ipAddress); + // assertEquals(1221, response.getAutonomousSystemNumber().intValue()); + // assertEquals("Telstra Pty Ltd", + // response.getAutonomousSystemOrganization()); + // assertEquals("Telstra Internet", response.getIsp()); + // assertEquals("Telstra Internet", response.getOrganization()); + + // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + // assertEquals("1.128.0.0/11", response.getNetwork().toString()); + + // IspResponse tryResponse = reader.tryIsp(ipAddress).get(); + // assertEquals(response.toJson(), tryResponse.toJson()); + // } + //} private File getFile(String filename) throws URISyntaxException { URL resource = DatabaseReaderTest.class From 3807c68d66c2ddb1b8e2c5b428ff97d36038c2f9 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 12:40:45 -0700 Subject: [PATCH 04/37] Support anonymous IP lookups without jackson --- .../com/maxmind/geoip2/DatabaseReader.java | 37 ++++++++++- .../model/AnonymousIpDatabaseModel.java | 62 +++++++++++++++++++ .../geoip2/model/AnonymousIpResponse.java | 18 +++++- .../maxmind/geoip2/DatabaseReaderTest.java | 36 +++++------ 4 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 816f905e..22dd9664 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -509,7 +509,12 @@ public AnonymousIpResponse anonymousIp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.getOrThrowException(ipAddress, AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP); + Optional r = getAnonymousIp(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override @@ -520,7 +525,35 @@ public Optional tryAnonymousIp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.get(ipAddress, AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP, 0); + return getAnonymousIp(ipAddress, 0); + } + + private Optional getAnonymousIp( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + AnonymousIpDatabaseModel.class, + DatabaseType.ANONYMOUS_IP, + stackDepth + ); + AnonymousIpDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new AnonymousIpResponse( + model, + result.getIpAddress(), + result.getNetwork() + ) + ); } /** diff --git a/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java new file mode 100644 index 00000000..815c9a0e --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java @@ -0,0 +1,62 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +public class AnonymousIpDatabaseModel { + private final Boolean isAnonymous; + private final Boolean isAnonymousVpn; + private final Boolean isHostingProvider; + private final Boolean isPublicProxy; + private final Boolean isTorExitNode; + + @MaxMindDbConstructor + public AnonymousIpDatabaseModel ( + @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, + @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, + @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, + @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, + @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode + ) { + this.isAnonymous = isAnonymous; + this.isAnonymousVpn = isAnonymousVpn; + this.isHostingProvider = isHostingProvider; + this.isPublicProxy = isPublicProxy; + this.isTorExitNode = isTorExitNode; + } + + public boolean getIsAnonymous() { + if (this.isAnonymous == null) { + return false; + } + return this.isAnonymous; + } + + public boolean getIsAnonymousVpn() { + if (this.isAnonymousVpn == null) { + return false; + } + return this.isAnonymousVpn; + } + + public boolean getIsHostingProvider() { + if (this.isHostingProvider == null) { + return false; + } + return this.isHostingProvider; + } + + public boolean getIsPublicProxy() { + if (this.isPublicProxy == null) { + return false; + } + return this.isPublicProxy; + } + + public boolean getIsTorExitNode() { + if (this.isTorExitNode == null) { + return false; + } + return this.isTorExitNode; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java b/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java index 3b81f4e0..e1e947c0 100644 --- a/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java @@ -55,6 +55,22 @@ public AnonymousIpResponse( this.network = network; } + public AnonymousIpResponse( + AnonymousIpDatabaseModel model, + String ipAddress, + Network network + ) { + this( + ipAddress, + model.getIsAnonymous(), + model.getIsAnonymousVpn(), + model.getIsHostingProvider(), + model.getIsPublicProxy(), + model.getIsTorExitNode(), + network + ); + } + /** * @return whether the IP address belongs to any sort of anonymous network. */ @@ -118,4 +134,4 @@ public String getIpAddress() { public Network getNetwork() { return this.network; } -} \ No newline at end of file +} diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 362b9a29..e3b136ea 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -242,25 +242,25 @@ public void incorrectDatabaseMethod() throws Exception { } - //@Test - //public void testAnonymousIp() throws Exception { - // try (DatabaseReader reader = new DatabaseReader.Builder( - // this.getFile("GeoIP2-Anonymous-IP-Test.mmdb")).build() - // ) { - // InetAddress ipAddress = InetAddress.getByName("1.2.0.1"); - // AnonymousIpResponse response = reader.anonymousIp(ipAddress); - // assertTrue(response.isAnonymous()); - // assertTrue(response.isAnonymousVpn()); - // assertFalse(response.isHostingProvider()); - // assertFalse(response.isPublicProxy()); - // assertFalse(response.isTorExitNode()); - // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - // assertEquals("1.2.0.0/16", response.getNetwork().toString()); + @Test + public void testAnonymousIp() throws Exception { + try (DatabaseReader reader = new DatabaseReader.Builder( + this.getFile("GeoIP2-Anonymous-IP-Test.mmdb")).build() + ) { + InetAddress ipAddress = InetAddress.getByName("1.2.0.1"); + AnonymousIpResponse response = reader.anonymousIp(ipAddress); + assertTrue(response.isAnonymous()); + assertTrue(response.isAnonymousVpn()); + assertFalse(response.isHostingProvider()); + assertFalse(response.isPublicProxy()); + assertFalse(response.isTorExitNode()); + assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + assertEquals("1.2.0.0/16", response.getNetwork().toString()); - // AnonymousIpResponse tryResponse = reader.tryAnonymousIp(ipAddress).get(); - // assertEquals(response.toJson(), tryResponse.toJson()); - // } - //} + AnonymousIpResponse tryResponse = reader.tryAnonymousIp(ipAddress).get(); + assertEquals(response.toJson(), tryResponse.toJson()); + } + } @Test public void testAsn() throws Exception { From 00b9fd91eea6be4c93f5d33090bdc19cfa2d2aec Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 13:09:02 -0700 Subject: [PATCH 05/37] Support connection type lookups without jackson --- .../com/maxmind/geoip2/DatabaseReader.java | 39 +++++++++++++++++-- .../model/ConnectionTypeDatabaseModel.java | 34 ++++++++++++++++ .../geoip2/model/ConnectionTypeResponse.java | 12 ++++++ .../maxmind/geoip2/DatabaseReaderTest.java | 28 ++++++------- 4 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 22dd9664..a0f2c983 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -633,8 +633,12 @@ public ConnectionTypeResponse connectionType(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.getOrThrowException(ipAddress, ConnectionTypeResponse.class, - DatabaseType.CONNECTION_TYPE); + Optional r = getConnectionType(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override @@ -645,8 +649,35 @@ public Optional tryConnectionType(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.get(ipAddress, ConnectionTypeResponse.class, - DatabaseType.CONNECTION_TYPE, 0); + return getConnectionType(ipAddress, 0); + } + + private Optional getConnectionType( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + ConnectionTypeDatabaseModel.class, + DatabaseType.CONNECTION_TYPE, + stackDepth + ); + ConnectionTypeDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new ConnectionTypeResponse( + model, + result.getIpAddress(), + result.getNetwork() + ) + ); } /** diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java new file mode 100644 index 00000000..bc2d346f --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java @@ -0,0 +1,34 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +public class ConnectionTypeDatabaseModel { + private final String connectionType; + + @MaxMindDbConstructor + public ConnectionTypeDatabaseModel ( + @MaxMindDbParameter(name="connection_type") String connectionType + ) { + this.connectionType = connectionType; + } + + public ConnectionTypeResponse.ConnectionType getConnectionType() { + if (this.connectionType == null) { + return null; + } + + switch (this.connectionType) { + case "Dialup": + return ConnectionTypeResponse.ConnectionType.DIALUP; + case "Cable/DSL": + return ConnectionTypeResponse.ConnectionType.CABLE_DSL; + case "Corporate": + return ConnectionTypeResponse.ConnectionType.CORPORATE; + case "Cellular": + return ConnectionTypeResponse.ConnectionType.CELLULAR; + default: + return ConnectionTypeResponse.ConnectionType.valueOf(this.connectionType); + } + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java index 6609311d..bca3f09f 100644 --- a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java @@ -64,6 +64,18 @@ public ConnectionTypeResponse( this.network = network; } + public ConnectionTypeResponse( + ConnectionTypeDatabaseModel model, + String ipAddress, + Network network + ) { + this( + model.getConnectionType(), + ipAddress, + network + ); + } + /** * @return The connection type of the IP address. */ diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index e3b136ea..f0a47190 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -303,23 +303,23 @@ public void testCity() throws Exception { } } - //@Test - //public void testConnectionType() throws Exception { - // try (DatabaseReader reader = new DatabaseReader.Builder( - // this.getFile("GeoIP2-Connection-Type-Test.mmdb")).build() - // ) { - // InetAddress ipAddress = InetAddress.getByName("1.0.1.0"); + @Test + public void testConnectionType() throws Exception { + try (DatabaseReader reader = new DatabaseReader.Builder( + this.getFile("GeoIP2-Connection-Type-Test.mmdb")).build() + ) { + InetAddress ipAddress = InetAddress.getByName("1.0.1.0"); - // ConnectionTypeResponse response = reader.connectionType(ipAddress); + ConnectionTypeResponse response = reader.connectionType(ipAddress); - // assertEquals(ConnectionType.CABLE_DSL, response.getConnectionType()); - // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - // assertEquals("1.0.1.0/24", response.getNetwork().toString()); + assertEquals(ConnectionType.CABLE_DSL, response.getConnectionType()); + assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + assertEquals("1.0.1.0/24", response.getNetwork().toString()); - // ConnectionTypeResponse tryResponse = reader.tryConnectionType(ipAddress).get(); - // assertEquals(response.toJson(), tryResponse.toJson()); - // } - //} + ConnectionTypeResponse tryResponse = reader.tryConnectionType(ipAddress).get(); + assertEquals(response.toJson(), tryResponse.toJson()); + } + } @Test public void testCountry() throws Exception { From 6ab2d56c208082060f72a307c9c95a2eee04fedc Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 13:13:54 -0700 Subject: [PATCH 06/37] Support domain lookups without jackson --- .../com/maxmind/geoip2/DatabaseReader.java | 39 +++++++++++++++++-- .../geoip2/model/DomainDatabaseModel.java | 19 +++++++++ .../maxmind/geoip2/model/DomainResponse.java | 8 ++++ .../maxmind/geoip2/DatabaseReaderTest.java | 28 ++++++------- 4 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index a0f2c983..a3f228f7 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -696,8 +696,12 @@ public DomainResponse domain(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this - .getOrThrowException(ipAddress, DomainResponse.class, DatabaseType.DOMAIN); + Optional r = getDomain(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override @@ -708,8 +712,35 @@ public Optional tryDomain(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this - .get(ipAddress, DomainResponse.class, DatabaseType.DOMAIN, 0); + return getDomain(ipAddress, 0); + } + + private Optional getDomain( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + DomainDatabaseModel.class, + DatabaseType.DOMAIN, + stackDepth + ); + DomainDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new DomainResponse( + model, + result.getIpAddress(), + result.getNetwork() + ) + ); } /** diff --git a/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java new file mode 100644 index 00000000..8300433a --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java @@ -0,0 +1,19 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +public class DomainDatabaseModel { + private final String domain; + + @MaxMindDbConstructor + public DomainDatabaseModel ( + @MaxMindDbParameter(name="domain") String domain + ) { + this.domain = domain; + } + + public String getDomain() { + return this.domain; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/DomainResponse.java b/src/main/java/com/maxmind/geoip2/model/DomainResponse.java index 489354dd..0ca5c48d 100644 --- a/src/main/java/com/maxmind/geoip2/model/DomainResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/DomainResponse.java @@ -38,6 +38,14 @@ public DomainResponse( this.network = network; } + public DomainResponse( + DomainDatabaseModel model, + String ipAddress, + Network network + ) { + this(model.getDomain(), ipAddress, network); + } + /** * @return the The second level domain associated with the IP address. This * will be something like "example.com" or "example.co.uk", not diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index f0a47190..0885117c 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -340,21 +340,21 @@ public void testCountry() throws Exception { } } - //@Test - //public void testDomain() throws Exception { - // try (DatabaseReader reader = new DatabaseReader.Builder( - // this.getFile("GeoIP2-Domain-Test.mmdb")).build() - // ) { - // InetAddress ipAddress = InetAddress.getByName("1.2.0.0"); - // DomainResponse response = reader.domain(ipAddress); - // assertEquals("maxmind.com", response.getDomain()); - // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - // assertEquals("1.2.0.0/16", response.getNetwork().toString()); + @Test + public void testDomain() throws Exception { + try (DatabaseReader reader = new DatabaseReader.Builder( + this.getFile("GeoIP2-Domain-Test.mmdb")).build() + ) { + InetAddress ipAddress = InetAddress.getByName("1.2.0.0"); + DomainResponse response = reader.domain(ipAddress); + assertEquals("maxmind.com", response.getDomain()); + assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + assertEquals("1.2.0.0/16", response.getNetwork().toString()); - // DomainResponse tryResponse = reader.tryDomain(ipAddress).get(); - // assertEquals(response.toJson(), tryResponse.toJson()); - // } - //} + DomainResponse tryResponse = reader.tryDomain(ipAddress).get(); + assertEquals(response.toJson(), tryResponse.toJson()); + } + } //@Test //public void testEnterprise() throws Exception { From 0d2e9e8a2188659aa99538ad2eed6e1603986a75 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 13:23:39 -0700 Subject: [PATCH 07/37] Support ISP lookups without jackson --- .../com/maxmind/geoip2/DatabaseReader.java | 37 ++++++++++++++++- .../geoip2/model/IspDatabaseModel.java | 40 +++++++++++++++++++ .../com/maxmind/geoip2/model/IspResponse.java | 15 +++++++ .../maxmind/geoip2/DatabaseReaderTest.java | 40 +++++++++---------- 4 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index a3f228f7..54c8fc2a 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -790,7 +790,12 @@ public IspResponse isp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.getOrThrowException(ipAddress, IspResponse.class, DatabaseType.ISP); + Optional r = getIsp(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override @@ -801,7 +806,35 @@ public Optional tryIsp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.get(ipAddress, IspResponse.class, DatabaseType.ISP, 0); + return getIsp(ipAddress, 0); + } + + private Optional getIsp( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + IspDatabaseModel.class, + DatabaseType.ISP, + stackDepth + ); + IspDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new IspResponse( + model, + result.getIpAddress(), + result.getNetwork() + ) + ); } /** diff --git a/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java new file mode 100644 index 00000000..c05fe090 --- /dev/null +++ b/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java @@ -0,0 +1,40 @@ +package com.maxmind.geoip2.model; + +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +public class IspDatabaseModel { + private final Long autonomousSystemNumber; + private final String autonomousSystemOrganization; + private final String isp; + private final String organization; + + @MaxMindDbConstructor + public IspDatabaseModel ( + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, + @MaxMindDbParameter(name="isp") String isp, + @MaxMindDbParameter(name="organization") String organization + ) { + this.autonomousSystemNumber = autonomousSystemNumber; + this.autonomousSystemOrganization = autonomousSystemOrganization; + this.isp = isp; + this.organization = organization; + } + + public Long getAutonomousSystemNumber() { + return this.autonomousSystemNumber; + } + + public String getAutonomousSystemOrganization() { + return this.autonomousSystemOrganization; + } + + public String getIsp() { + return this.isp; + } + + public String getOrganization() { + return this.organization; + } +} diff --git a/src/main/java/com/maxmind/geoip2/model/IspResponse.java b/src/main/java/com/maxmind/geoip2/model/IspResponse.java index 5de2c38a..f12bfff8 100644 --- a/src/main/java/com/maxmind/geoip2/model/IspResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/IspResponse.java @@ -41,6 +41,21 @@ public IspResponse( this.organization = organization; } + public IspResponse( + IspDatabaseModel model, + String ipAddress, + Network network + ) { + this( + model.getAutonomousSystemNumber().intValue(), + model.getAutonomousSystemOrganization(), + ipAddress, + model.getIsp(), + model.getOrganization(), + network + ); + } + /** * @return The name of the ISP associated with the IP address. */ diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 0885117c..61850419 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -383,26 +383,26 @@ public void testDomain() throws Exception { // } //} - //@Test - //public void testIsp() throws Exception { - // try (DatabaseReader reader = new DatabaseReader.Builder( - // this.getFile("GeoIP2-ISP-Test.mmdb")).build() - // ) { - // InetAddress ipAddress = InetAddress.getByName("1.128.0.0"); - // IspResponse response = reader.isp(ipAddress); - // assertEquals(1221, response.getAutonomousSystemNumber().intValue()); - // assertEquals("Telstra Pty Ltd", - // response.getAutonomousSystemOrganization()); - // assertEquals("Telstra Internet", response.getIsp()); - // assertEquals("Telstra Internet", response.getOrganization()); - - // assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); - // assertEquals("1.128.0.0/11", response.getNetwork().toString()); - - // IspResponse tryResponse = reader.tryIsp(ipAddress).get(); - // assertEquals(response.toJson(), tryResponse.toJson()); - // } - //} + @Test + public void testIsp() throws Exception { + try (DatabaseReader reader = new DatabaseReader.Builder( + this.getFile("GeoIP2-ISP-Test.mmdb")).build() + ) { + InetAddress ipAddress = InetAddress.getByName("1.128.0.0"); + IspResponse response = reader.isp(ipAddress); + assertEquals(1221, response.getAutonomousSystemNumber().intValue()); + assertEquals("Telstra Pty Ltd", + response.getAutonomousSystemOrganization()); + assertEquals("Telstra Internet", response.getIsp()); + assertEquals("Telstra Internet", response.getOrganization()); + + assertEquals(ipAddress.getHostAddress(), response.getIpAddress()); + assertEquals("1.128.0.0/11", response.getNetwork().toString()); + + IspResponse tryResponse = reader.tryIsp(ipAddress).get(); + assertEquals(response.toJson(), tryResponse.toJson()); + } + } private File getFile(String filename) throws URISyntaxException { URL resource = DatabaseReaderTest.class From 6b78a8c8bf43fb35da0a488cd44784f0c3c30fc1 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:39:52 -0700 Subject: [PATCH 08/37] Fix null pointer dereference --- src/main/java/com/maxmind/geoip2/model/CityResponse.java | 2 +- src/main/java/com/maxmind/geoip2/model/CountryResponse.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index c1abd851..1faae48d 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -101,7 +101,7 @@ public CityResponse( mapSubdivisions(locales, model.getSubdivisions()), model.getTraits() != null ? new Traits( - model.getTraits().getAutonomousSystemNumber(), + model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, model.getTraits().getAutonomousSystemOrganization(), model.getTraits().getConnectionType(), model.getTraits().getDomain(), diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index dfe11c3f..8ccc87ec 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -77,7 +77,7 @@ public CountryResponse( ) : null, model.getTraits() != null ? new Traits( - model.getTraits().getAutonomousSystemNumber(), + model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, model.getTraits().getAutonomousSystemOrganization(), model.getTraits().getConnectionType(), model.getTraits().getDomain(), From 0ddb56b0d933a3bac28975a1864d7953b667a824 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:40:25 -0700 Subject: [PATCH 09/37] Fix types we deserialize to --- .../model/ConnectionTypeDatabaseModel.java | 20 +++---------------- .../geoip2/model/ConnectionTypeResponse.java | 19 ++++++++++++++++++ .../geoip2/record/TraitsDatabaseRecord.java | 12 +++++------ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java index bc2d346f..68ae8784 100644 --- a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java +++ b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java @@ -2,6 +2,7 @@ import com.maxmind.db.MaxMindDbConstructor; import com.maxmind.db.MaxMindDbParameter; +import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; public class ConnectionTypeDatabaseModel { private final String connectionType; @@ -13,22 +14,7 @@ public ConnectionTypeDatabaseModel ( this.connectionType = connectionType; } - public ConnectionTypeResponse.ConnectionType getConnectionType() { - if (this.connectionType == null) { - return null; - } - - switch (this.connectionType) { - case "Dialup": - return ConnectionTypeResponse.ConnectionType.DIALUP; - case "Cable/DSL": - return ConnectionTypeResponse.ConnectionType.CABLE_DSL; - case "Corporate": - return ConnectionTypeResponse.ConnectionType.CORPORATE; - case "Cellular": - return ConnectionTypeResponse.ConnectionType.CELLULAR; - default: - return ConnectionTypeResponse.ConnectionType.valueOf(this.connectionType); - } + public ConnectionType getConnectionType() { + return ConnectionType.fromString(this.connectionType); } } diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java index bca3f09f..54e7d3c1 100644 --- a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java @@ -37,6 +37,25 @@ public enum ConnectionType { public String toString() { return this.name; } + + public static ConnectionType fromString(String s) { + if (s == null) { + return null; + } + + switch (s) { + case "Dialup": + return ConnectionType.DIALUP; + case "Cable/DSL": + return ConnectionType.CABLE_DSL; + case "Corporate": + return ConnectionType.CORPORATE; + case "Cellular": + return ConnectionType.CELLULAR; + default: + return null; + } + } } private final ConnectionType connectionType; diff --git a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java index 558ea127..149c36f0 100644 --- a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java @@ -5,9 +5,9 @@ import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; public final class TraitsDatabaseRecord { - private final Integer autonomousSystemNumber; + private final Long autonomousSystemNumber; private final String autonomousSystemOrganization; - private final ConnectionType connectionType; + private final String connectionType; private final String domain; private final Boolean isAnonymous; private final Boolean isAnonymousProxy; @@ -25,9 +25,9 @@ public final class TraitsDatabaseRecord { @MaxMindDbConstructor public TraitsDatabaseRecord( - @MaxMindDbParameter(name="autonomous_system_number") Integer autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, - @MaxMindDbParameter(name="connection_type") ConnectionType connectionType, + @MaxMindDbParameter(name="connection_type") String connectionType, @MaxMindDbParameter(name="domain") String domain, @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, @MaxMindDbParameter(name="is_anonymous_proxy") Boolean isAnonymousProxy, @@ -62,7 +62,7 @@ public TraitsDatabaseRecord( this.userType = userType; } - public Integer getAutonomousSystemNumber() { + public Long getAutonomousSystemNumber() { return this.autonomousSystemNumber; } @@ -71,7 +71,7 @@ public String getAutonomousSystemOrganization() { } public ConnectionType getConnectionType() { - return this.connectionType; + return ConnectionType.fromString(this.connectionType); } public String getDomain() { From 7ea552b553c866d28d5eaadc8f3c392a288cf755 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:40:43 -0700 Subject: [PATCH 10/37] Support enterprise lookups without jackson --- .../com/maxmind/geoip2/DatabaseReader.java | 37 +++++- .../geoip2/model/EnterpriseResponse.java | 124 ++++++++++++++++++ .../maxmind/geoip2/DatabaseReaderTest.java | 50 ++++--- 3 files changed, 183 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 54c8fc2a..5b279933 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -759,7 +759,12 @@ public EnterpriseResponse enterprise(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.getOrThrowException(ipAddress, EnterpriseResponse.class, DatabaseType.ENTERPRISE); + Optional r = getEnterprise(ipAddress, 1); + if (!r.isPresent()) { + throw new AddressNotFoundException("The address " + + ipAddress.getHostAddress() + " is not in the database."); + } + return r.get(); } @Override @@ -770,9 +775,37 @@ public Optional tryEnterprise(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return this.get(ipAddress, EnterpriseResponse.class, DatabaseType.ENTERPRISE, 0); + return getEnterprise(ipAddress, 0); } + private Optional getEnterprise( + InetAddress ipAddress, + int stackDepth + ) throws IOException, + GeoIp2Exception, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { + LookupResult result = this.get2( + ipAddress, + CityDatabaseModel.class, + DatabaseType.ENTERPRISE, + stackDepth + ); + CityDatabaseModel model = result.getModel(); + if (model == null) { + return Optional.empty(); + } + return Optional.of( + new EnterpriseResponse( + model, + result.getIpAddress(), + result.getNetwork(), + locales + ) + ); + } /** * Look up an IP address in a GeoIP2 ISP database. diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index bad4104f..dd96be45 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -2,9 +2,11 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; import java.util.ArrayList; +import java.util.List; /** *

@@ -29,4 +31,126 @@ public EnterpriseResponse( super(city, continent, country, location, maxmind, postal, registeredCountry, representedCountry, subdivisions, traits); } + + public EnterpriseResponse( + CityDatabaseModel model, + String ipAddress, + Network network, + List locales + ) { + this( + model.getCity() != null ? + new City( + locales, + null, + model.getCity().getGeoNameId().intValue(), + model.getCity().getNames() + ) : null, + model.getContinent() != null ? + new Continent( + locales, + model.getContinent().getCode(), + model.getContinent().getGeoNameId().intValue(), + model.getContinent().getNames() + ) : null, + model.getCountry() != null ? + new Country( + locales, + null, + model.getCountry().getGeoNameId().intValue(), + model.getCountry().getIsInEuropeanUnion(), + model.getCountry().getIsoCode(), + model.getCountry().getNames() + ) : null, + model.getLocation(), + null, + model.getPostal(), + model.getRegisteredCountry() != null ? + new Country( + locales, + null, + model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getIsInEuropeanUnion(), + model.getRegisteredCountry().getIsoCode(), + model.getRegisteredCountry().getNames() + ) : null, + model.getRepresentedCountry() != null ? + new RepresentedCountry( + locales, + null, + model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getIsInEuropeanUnion(), + model.getRepresentedCountry().getIsoCode(), + model.getRepresentedCountry().getNames(), + model.getRepresentedCountry().getType() + ) : null, + mapSubdivisions(locales, model.getSubdivisions()), + model.getTraits() != null ? + new Traits( + model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, + model.getTraits().getAutonomousSystemOrganization(), + model.getTraits().getConnectionType(), + model.getTraits().getDomain(), + ipAddress, + model.getTraits().isAnonymous(), + model.getTraits().isAnonymousProxy(), + model.getTraits().isAnonymousVpn(), + model.getTraits().isHostingProvider(), + model.getTraits().isLegitimateProxy(), + model.getTraits().isPublicProxy(), + model.getTraits().isSatelliteProvider(), + model.getTraits().isTorExitNode(), + model.getTraits().getIsp(), + network, + model.getTraits().getOrganization(), + model.getTraits().getUserType(), + model.getTraits().getUserCount(), + model.getTraits().getStaticIpScore() + ) : + new Traits( + null, + null, + null, + null, + ipAddress, + false, + false, + false, + false, + false, + false, + false, + false, + null, + network, + null, + null, + null, + null + ) + ); + } + + private static ArrayList mapSubdivisions( + List locales, + ArrayList subdivisions + ) { + if (subdivisions == null) { + return null; + } + + ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); + for (SubdivisionDatabaseRecord subdivision : subdivisions) { + subdivisions2.add( + new Subdivision( + locales, + null, + subdivision.getGeoNameId().intValue(), + subdivision.getIsoCode(), + subdivision.getNames() + ) + ); + } + return subdivisions2; + } } diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 61850419..d8f671d9 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -356,32 +356,30 @@ public void testDomain() throws Exception { } } - //@Test - //public void testEnterprise() throws Exception { - // try (DatabaseReader reader = new DatabaseReader.Builder( - // getFile("GeoIP2-Enterprise-Test.mmdb")).build() - // ) { - // InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); - - // EnterpriseResponse response = reader.enterprise(ipAddress); - // assertEquals(11, response.getCity().getConfidence().intValue()); - // assertEquals(99, response.getCountry().getConfidence().intValue()); - // assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); - // assertEquals(27, response.getLocation().getAccuracyRadius().intValue()); - // assertEquals(ConnectionType.CABLE_DSL, response.getTraits().getConnectionType()); - // assertTrue(response.getTraits().isLegitimateProxy()); - // assertEquals(ipAddress.getHostAddress(), response.getTraits().getIpAddress()); - // assertEquals("74.209.16.0/20", response.getTraits().getNetwork().toString()); - - // EnterpriseResponse tryResponse = reader.tryEnterprise(ipAddress).get(); - // assertEquals(response.toJson(), tryResponse.toJson()); - - // // Test that the city and country methods can be called without - // // an exception - // reader.city(ipAddress); - // reader.country(ipAddress); - // } - //} + @Test + public void testEnterprise() throws Exception { + try (DatabaseReader reader = new DatabaseReader.Builder( + getFile("GeoIP2-Enterprise-Test.mmdb")).build() + ) { + InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); + + EnterpriseResponse response = reader.enterprise(ipAddress); + assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); + assertEquals(27, response.getLocation().getAccuracyRadius().intValue()); + assertEquals(ConnectionType.CABLE_DSL, response.getTraits().getConnectionType()); + assertTrue(response.getTraits().isLegitimateProxy()); + assertEquals(ipAddress.getHostAddress(), response.getTraits().getIpAddress()); + assertEquals("74.209.16.0/20", response.getTraits().getNetwork().toString()); + + EnterpriseResponse tryResponse = reader.tryEnterprise(ipAddress).get(); + assertEquals(response.toJson(), tryResponse.toJson()); + + // Test that the city and country methods can be called without + // an exception + reader.city(ipAddress); + reader.country(ipAddress); + } + } @Test public void testIsp() throws Exception { From 2e5c13b503dabac08f0079f314bb8c363da94c6f Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:46:16 -0700 Subject: [PATCH 11/37] Fix some null pointer exceptions --- .../java/com/maxmind/geoip2/model/AsnResponse.java | 2 +- .../java/com/maxmind/geoip2/model/CityResponse.java | 12 ++++++------ .../com/maxmind/geoip2/model/CountryResponse.java | 8 ++++---- .../com/maxmind/geoip2/model/EnterpriseResponse.java | 12 ++++++------ .../java/com/maxmind/geoip2/model/IspResponse.java | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java index 7e888fd5..0a5db153 100644 --- a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java @@ -48,7 +48,7 @@ public AsnResponse( Network network ) { this( - model.getAutonomousSystemNumber().intValue(), + model.getAutonomousSystemNumber() != null ? model.getAutonomousSystemNumber().intValue() : null, model.getAutonomousSystemOrganization(), ipAddress, network diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index 1faae48d..ff0faa5a 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -57,21 +57,21 @@ public CityResponse( new City( locales, null, - model.getCity().getGeoNameId().intValue(), + model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, model.getCity().getNames() ) : null, model.getContinent() != null ? new Continent( locales, model.getContinent().getCode(), - model.getContinent().getGeoNameId().intValue(), + model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, model.getContinent().getNames() ) : null, model.getCountry() != null ? new Country( locales, null, - model.getCountry().getGeoNameId().intValue(), + model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), model.getCountry().getNames() @@ -83,7 +83,7 @@ public CityResponse( new Country( locales, null, - model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), model.getRegisteredCountry().getNames() @@ -92,7 +92,7 @@ public CityResponse( new RepresentedCountry( locales, null, - model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), model.getRepresentedCountry().getNames(), @@ -159,7 +159,7 @@ private static ArrayList mapSubdivisions( new Subdivision( locales, null, - subdivision.getGeoNameId().intValue(), + subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, subdivision.getIsoCode(), subdivision.getNames() ) diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index 8ccc87ec..a96aaa26 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -43,14 +43,14 @@ public CountryResponse( new Continent( locales, model.getContinent().getCode(), - model.getContinent().getGeoNameId().intValue(), + model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, model.getContinent().getNames() ) : null, model.getCountry() != null ? new Country( locales, null, - model.getCountry().getGeoNameId().intValue(), + model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), model.getCountry().getNames() @@ -60,7 +60,7 @@ public CountryResponse( new Country( locales, null, - model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), model.getRegisteredCountry().getNames() @@ -69,7 +69,7 @@ public CountryResponse( new RepresentedCountry( locales, null, - model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), model.getRepresentedCountry().getNames(), diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index dd96be45..22279848 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -43,21 +43,21 @@ public EnterpriseResponse( new City( locales, null, - model.getCity().getGeoNameId().intValue(), + model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, model.getCity().getNames() ) : null, model.getContinent() != null ? new Continent( locales, model.getContinent().getCode(), - model.getContinent().getGeoNameId().intValue(), + model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, model.getContinent().getNames() ) : null, model.getCountry() != null ? new Country( locales, null, - model.getCountry().getGeoNameId().intValue(), + model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), model.getCountry().getNames() @@ -69,7 +69,7 @@ public EnterpriseResponse( new Country( locales, null, - model.getRegisteredCountry().getGeoNameId().intValue(), + model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), model.getRegisteredCountry().getNames() @@ -78,7 +78,7 @@ public EnterpriseResponse( new RepresentedCountry( locales, null, - model.getRepresentedCountry().getGeoNameId().intValue(), + model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), model.getRepresentedCountry().getNames(), @@ -145,7 +145,7 @@ private static ArrayList mapSubdivisions( new Subdivision( locales, null, - subdivision.getGeoNameId().intValue(), + subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, subdivision.getIsoCode(), subdivision.getNames() ) diff --git a/src/main/java/com/maxmind/geoip2/model/IspResponse.java b/src/main/java/com/maxmind/geoip2/model/IspResponse.java index f12bfff8..fa9925f2 100644 --- a/src/main/java/com/maxmind/geoip2/model/IspResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/IspResponse.java @@ -47,7 +47,7 @@ public IspResponse( Network network ) { this( - model.getAutonomousSystemNumber().intValue(), + model.getAutonomousSystemNumber() != null ? model.getAutonomousSystemNumber().intValue() : null, model.getAutonomousSystemOrganization(), ipAddress, model.getIsp(), From 5e61fa4f4729cff823c8f2a99c74ed72e285f8f5 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:48:11 -0700 Subject: [PATCH 12/37] Remove unused functions --- .../com/maxmind/geoip2/DatabaseReader.java | 82 +++---------------- 1 file changed, 10 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 5b279933..e3a67d96 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -263,31 +263,6 @@ Network getNetwork() { } } - /** - * @param ipAddress IPv4 or IPv6 address to lookup. - * @param cls The class to deserialize to. - * @param expectedType The expected database type. - * @return A object with the data for the IP address - * @throws IOException if there is an error opening or reading from the file. - * @throws AddressNotFoundException if the IP address is not in our database - */ - private T getOrThrowException(InetAddress ipAddress, Class cls, - DatabaseType expectedType) - throws IOException, - AddressNotFoundException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { - Optional t = get(ipAddress, cls, expectedType, 1); - if (!t.isPresent()) { - throw new AddressNotFoundException("The address " - + ipAddress.getHostAddress() + " is not in the database."); - } - - return t.get(); - } - /** * @param ipAddress IPv4 or IPv6 address to lookup. * @param cls The class to deserialize to. @@ -296,38 +271,10 @@ private T getOrThrowException(InetAddress ipAddress, Class cls, * we should use to report back to the user when in error. If this is called directly from the * method to report to the use set to zero, if this is called indirectly then it is the number of * methods between this method and the method to report the name of. - * @return A object with the data for the IP address or null if the IP address is not in our database + * @return A LookupResult object with the data for the IP address * @throws IOException if there is an error opening or reading from the file. */ - private Optional get(InetAddress ipAddress, Class cls, - DatabaseType expectedType, int stackDepth) - throws IOException, - AddressNotFoundException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { - - if ((databaseType & expectedType.type) == 0) { - String caller = Thread.currentThread().getStackTrace()[2 + stackDepth] - .getMethodName(); - throw new UnsupportedOperationException( - "Invalid attempt to open a " + getMetadata().getDatabaseType() - + " database using the " + caller + " method"); - } - - // We are using the fully qualified name as otherwise it is ambiguous - // on Java 14 due to the new java.lang.Record. - com.maxmind.db.Record record = reader.getRecord(ipAddress, cls); - - T o = cls.cast(record.getData()); - if (o == null) { - return Optional.empty(); - } - return Optional.of(o); - } - - private LookupResult get2(InetAddress ipAddress, Class cls, + private LookupResult get(InetAddress ipAddress, Class cls, DatabaseType expectedType, int stackDepth) throws IOException, AddressNotFoundException, @@ -353,15 +300,6 @@ private LookupResult get2(InetAddress ipAddress, Class cls, return new LookupResult<>(o, ipAddress.getHostAddress(), record.getNetwork()); } - private ObjectNode jsonNodeToObjectNode(JsonNode node) - throws InvalidDatabaseException { - if (node == null || node instanceof ObjectNode) { - return (ObjectNode) node; - } - throw new InvalidDatabaseException( - "Unexpected data type returned. The GeoIP2 database may be corrupt."); - } - /** *

* Closes the database. @@ -417,7 +355,7 @@ private Optional getCountry( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, CountryDatabaseModel.class, DatabaseType.COUNTRY, @@ -473,7 +411,7 @@ private Optional getCity( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, CityDatabaseModel.class, DatabaseType.CITY, @@ -537,7 +475,7 @@ private Optional getAnonymousIp( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, AnonymousIpDatabaseModel.class, DatabaseType.ANONYMOUS_IP, @@ -598,7 +536,7 @@ private Optional getAsn(InetAddress ipAddress, int stackDepth) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, AsnDatabaseModel.class, DatabaseType.ASN, @@ -661,7 +599,7 @@ private Optional getConnectionType( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, ConnectionTypeDatabaseModel.class, DatabaseType.CONNECTION_TYPE, @@ -724,7 +662,7 @@ private Optional getDomain( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, DomainDatabaseModel.class, DatabaseType.DOMAIN, @@ -787,7 +725,7 @@ private Optional getEnterprise( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, CityDatabaseModel.class, DatabaseType.ENTERPRISE, @@ -851,7 +789,7 @@ private Optional getIsp( IllegalAccessException, InvocationTargetException, NoSuchMethodException { - LookupResult result = this.get2( + LookupResult result = this.get( ipAddress, IspDatabaseModel.class, DatabaseType.ISP, From c1c9452e557035c0ae74380b8402c0842a804611 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:50:27 -0700 Subject: [PATCH 13/37] Remove unused jackson code --- src/main/java/com/maxmind/geoip2/DatabaseReader.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index e3a67d96..a5e6fc81 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -1,7 +1,5 @@ package com.maxmind.geoip2; -import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.maxmind.db.*; import com.maxmind.db.Reader.FileMode; import com.maxmind.geoip2.exception.AddressNotFoundException; @@ -67,8 +65,6 @@ public class DatabaseReader implements DatabaseProvider, Closeable { private final Reader reader; - private final ObjectMapper om; - private final List locales; private final int databaseType; @@ -106,12 +102,6 @@ private DatabaseReader(Builder builder) throw new IllegalArgumentException( "Unsupported Builder configuration: expected either File or URL"); } - this.om = new ObjectMapper(); - this.om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, false); - this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, - false); - this.om.configure( - DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); this.locales = builder.locales; databaseType = getDatabaseType(); From b71f17236c89a7915aeb24be2d9db2de7df9ba8e Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 14:51:54 -0700 Subject: [PATCH 14/37] Remove unnecessary import --- src/main/java/com/maxmind/geoip2/DatabaseReader.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index a5e6fc81..6d4852ef 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -10,7 +10,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.InstantiationException; import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.util.Collections; From 57c40b443d54a78a13e3ae446203c2312798a7dc Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 15:57:38 -0700 Subject: [PATCH 15/37] Support confidence again --- .../java/com/maxmind/geoip2/model/CityResponse.java | 10 +++++----- .../java/com/maxmind/geoip2/model/CountryResponse.java | 6 +++--- .../com/maxmind/geoip2/model/EnterpriseResponse.java | 10 +++++----- .../com/maxmind/geoip2/record/CityDatabaseRecord.java | 7 +++++++ .../maxmind/geoip2/record/CountryDatabaseRecord.java | 7 +++++++ .../record/RepresentedCountryDatabaseRecord.java | 7 +++++++ .../geoip2/record/SubdivisionDatabaseRecord.java | 7 +++++++ .../java/com/maxmind/geoip2/DatabaseReaderTest.java | 2 ++ 8 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index ff0faa5a..baad66f0 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -56,7 +56,7 @@ public CityResponse( model.getCity() != null ? new City( locales, - null, + model.getCity().getConfidence(), model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, model.getCity().getNames() ) : null, @@ -70,7 +70,7 @@ public CityResponse( model.getCountry() != null ? new Country( locales, - null, + model.getCountry().getConfidence(), model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), @@ -82,7 +82,7 @@ public CityResponse( model.getRegisteredCountry() != null ? new Country( locales, - null, + model.getRegisteredCountry().getConfidence(), model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), @@ -91,7 +91,7 @@ public CityResponse( model.getRepresentedCountry() != null ? new RepresentedCountry( locales, - null, + model.getRepresentedCountry().getConfidence(), model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), @@ -158,7 +158,7 @@ private static ArrayList mapSubdivisions( subdivisions2.add( new Subdivision( locales, - null, + subdivision.getConfidence(), subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, subdivision.getIsoCode(), subdivision.getNames() diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index a96aaa26..e78a683f 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -49,7 +49,7 @@ public CountryResponse( model.getCountry() != null ? new Country( locales, - null, + model.getCountry().getConfidence(), model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), @@ -59,7 +59,7 @@ public CountryResponse( model.getRegisteredCountry() != null ? new Country( locales, - null, + model.getRegisteredCountry().getConfidence(), model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), @@ -68,7 +68,7 @@ public CountryResponse( model.getRepresentedCountry() != null ? new RepresentedCountry( locales, - null, + model.getRepresentedCountry().getConfidence(), model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index 22279848..4f726fb7 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -42,7 +42,7 @@ public EnterpriseResponse( model.getCity() != null ? new City( locales, - null, + model.getCity().getConfidence(), model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, model.getCity().getNames() ) : null, @@ -56,7 +56,7 @@ public EnterpriseResponse( model.getCountry() != null ? new Country( locales, - null, + model.getCountry().getConfidence(), model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, model.getCountry().getIsInEuropeanUnion(), model.getCountry().getIsoCode(), @@ -68,7 +68,7 @@ public EnterpriseResponse( model.getRegisteredCountry() != null ? new Country( locales, - null, + model.getRegisteredCountry().getConfidence(), model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, model.getRegisteredCountry().getIsInEuropeanUnion(), model.getRegisteredCountry().getIsoCode(), @@ -77,7 +77,7 @@ public EnterpriseResponse( model.getRepresentedCountry() != null ? new RepresentedCountry( locales, - null, + model.getRepresentedCountry().getConfidence(), model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, model.getRepresentedCountry().getIsInEuropeanUnion(), model.getRepresentedCountry().getIsoCode(), @@ -144,7 +144,7 @@ private static ArrayList mapSubdivisions( subdivisions2.add( new Subdivision( locales, - null, + subdivision.getConfidence(), subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, subdivision.getIsoCode(), subdivision.getNames() diff --git a/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java index 9bb82fe3..81cb994e 100644 --- a/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java @@ -6,18 +6,25 @@ import java.util.Map; public final class CityDatabaseRecord { + private final Integer confidence; private final Long geoNameId; private final Map names; @MaxMindDbConstructor public CityDatabaseRecord( + @MaxMindDbParameter(name="confidence") Integer confidence, @MaxMindDbParameter(name="geoname_id") Long geoNameId, @MaxMindDbParameter(name="names") Map names ) { + this.confidence = confidence; this.geoNameId = geoNameId; this.names = names; } + public Integer getConfidence() { + return this.confidence; + } + public Long getGeoNameId() { return this.geoNameId; } diff --git a/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java index 7fb3e226..ad00e0ea 100644 --- a/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java @@ -6,6 +6,7 @@ import java.util.Map; public final class CountryDatabaseRecord { + private final Integer confidence; private final Long geoNameId; private final Boolean isInEuropeanUnion; private final String isoCode; @@ -13,17 +14,23 @@ public final class CountryDatabaseRecord { @MaxMindDbConstructor public CountryDatabaseRecord( + @MaxMindDbParameter(name="confidence") Integer confidence, @MaxMindDbParameter(name="geoname_id") Long geoNameId, @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, @MaxMindDbParameter(name="iso_code") String isoCode, @MaxMindDbParameter(name="names") Map names ) { + this.confidence = confidence; this.geoNameId = geoNameId; this.isInEuropeanUnion = isInEuropeanUnion; this.isoCode = isoCode; this.names = names; } + public Integer getConfidence() { + return this.confidence; + } + public Long getGeoNameId() { return this.geoNameId; } diff --git a/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java index e73c037c..2315485c 100644 --- a/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java @@ -6,6 +6,7 @@ import java.util.Map; public final class RepresentedCountryDatabaseRecord { + private final Integer confidence; private final Long geoNameId; private final Boolean isInEuropeanUnion; private final String isoCode; @@ -14,12 +15,14 @@ public final class RepresentedCountryDatabaseRecord { @MaxMindDbConstructor public RepresentedCountryDatabaseRecord( + @MaxMindDbParameter(name="confidence") Integer confidence, @MaxMindDbParameter(name="geoname_id") Long geoNameId, @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, @MaxMindDbParameter(name="iso_code") String isoCode, @MaxMindDbParameter(name="names") Map names, @MaxMindDbParameter(name="type") String type ) { + this.confidence = confidence; this.geoNameId = geoNameId; this.isInEuropeanUnion = isInEuropeanUnion; this.isoCode = isoCode; @@ -27,6 +30,10 @@ public RepresentedCountryDatabaseRecord( this.type = type; } + public Integer getConfidence() { + return this.confidence; + } + public Long getGeoNameId() { return this.geoNameId; } diff --git a/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java index e1182eed..992729cb 100644 --- a/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java @@ -6,21 +6,28 @@ import java.util.Map; public final class SubdivisionDatabaseRecord { + private final Integer confidence; private final Long geoNameId; private final String isoCode; private final Map names; @MaxMindDbConstructor public SubdivisionDatabaseRecord( + @MaxMindDbParameter(name="confidence") Integer confidence, @MaxMindDbParameter(name="geoname_id") Long geoNameId, @MaxMindDbParameter(name="iso_code") String isoCode, @MaxMindDbParameter(name="names") Map names ) { + this.confidence = confidence; this.geoNameId = geoNameId; this.isoCode = isoCode; this.names = names; } + public Integer getConfidence() { + return this.confidence; + } + public Long getGeoNameId() { return this.geoNameId; } diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index d8f671d9..8d53c6fc 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -364,6 +364,8 @@ public void testEnterprise() throws Exception { InetAddress ipAddress = InetAddress.getByName("74.209.24.0"); EnterpriseResponse response = reader.enterprise(ipAddress); + assertEquals(11, response.getCity().getConfidence().intValue()); + assertEquals(99, response.getCountry().getConfidence().intValue()); assertEquals(6252001, response.getCountry().getGeoNameId().intValue()); assertEquals(27, response.getLocation().getAccuracyRadius().intValue()); assertEquals(ConnectionType.CABLE_DSL, response.getTraits().getConnectionType()); From af08ceb2a9e427ab3bb036a0278569cb652f5bd3 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 15:59:15 -0700 Subject: [PATCH 16/37] Drop user_count from database traits since it is web service only --- src/main/java/com/maxmind/geoip2/model/CityResponse.java | 2 +- .../java/com/maxmind/geoip2/model/CountryResponse.java | 2 +- .../java/com/maxmind/geoip2/model/EnterpriseResponse.java | 2 +- .../com/maxmind/geoip2/record/TraitsDatabaseRecord.java | 7 ------- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index baad66f0..cae7969a 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -118,7 +118,7 @@ public CityResponse( network, model.getTraits().getOrganization(), model.getTraits().getUserType(), - model.getTraits().getUserCount(), + null, model.getTraits().getStaticIpScore() ) : new Traits( diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index e78a683f..f88241e4 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -94,7 +94,7 @@ public CountryResponse( network, model.getTraits().getOrganization(), model.getTraits().getUserType(), - model.getTraits().getUserCount(), + null, model.getTraits().getStaticIpScore() ) : new Traits( diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index 4f726fb7..7a379e83 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -104,7 +104,7 @@ public EnterpriseResponse( network, model.getTraits().getOrganization(), model.getTraits().getUserType(), - model.getTraits().getUserCount(), + null, model.getTraits().getStaticIpScore() ) : new Traits( diff --git a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java index 149c36f0..81c15bc1 100644 --- a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java +++ b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java @@ -20,7 +20,6 @@ public final class TraitsDatabaseRecord { private final String isp; private final String organization; private final String userType; - private final Integer userCount; private final Double staticIpScore; @MaxMindDbConstructor @@ -40,7 +39,6 @@ public TraitsDatabaseRecord( @MaxMindDbParameter(name="isp") String isp, @MaxMindDbParameter(name="organization") String organization, @MaxMindDbParameter(name="static_ip_score") Double staticIpScore, - @MaxMindDbParameter(name="user_count") Integer userCount, @MaxMindDbParameter(name="user_type") String userType ) { this.autonomousSystemNumber = autonomousSystemNumber; @@ -58,7 +56,6 @@ public TraitsDatabaseRecord( this.isp = isp; this.organization = organization; this.staticIpScore = staticIpScore; - this.userCount = userCount; this.userType = userType; } @@ -146,10 +143,6 @@ public Double getStaticIpScore() { return this.staticIpScore; } - public Integer getUserCount() { - return this.userCount; - } - public String getUserType() { return this.userType; } From 73a9cbea9ae9fc142b179781d5f036e7bc4184ad Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 16:04:35 -0700 Subject: [PATCH 17/37] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eb4aba4..c0086205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ CHANGELOG ------------------- * The HTTP client now allows up to 20 connections to be active at once. Previously the limit was 2. +* Update `maxmind-db` dependency to a new version that no longer uses + Jackson. This improves database lookup performance. 2.14.0 (2020-06-15) ------------------- From 2527287cfbf71f4ae1fd8ceaccf49a160b9a7911 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 17 Aug 2020 16:12:17 -0700 Subject: [PATCH 18/37] Fix Benchmark --- sample/Benchmark.java | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/sample/Benchmark.java b/sample/Benchmark.java index 1f5d34cf..2151fb49 100644 --- a/sample/Benchmark.java +++ b/sample/Benchmark.java @@ -1,5 +1,6 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Random; @@ -20,7 +21,13 @@ public class Benchmark { private final static int BENCHMARKS = 5; private final static boolean TRACE = false; - public static void main(String[] args) throws GeoIp2Exception, IOException { + public static void main(String[] args) + throws GeoIp2Exception, + IOException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb"); System.out.println("No caching"); loop("Warming up", file, WARMUPS, NoCache.getInstance()); @@ -31,7 +38,13 @@ public static void main(String[] args) throws GeoIp2Exception, IOException { loop("Benchmarking", file, BENCHMARKS, new CHMCache()); } - private static void loop(String msg, File file, int loops, NodeCache cache) throws GeoIp2Exception, IOException { + private static void loop(String msg, File file, int loops, NodeCache cache) + throws GeoIp2Exception, + IOException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { System.out.println(msg); for (int i = 0; i < loops; i++) { DatabaseReader r = new DatabaseReader.Builder(file).fileMode(FileMode.MEMORY_MAPPED).withCache(cache).build(); @@ -40,7 +53,13 @@ private static void loop(String msg, File file, int loops, NodeCache cache) thro System.out.println(); } - private static void bench(DatabaseReader r, int count, int seed) throws GeoIp2Exception, UnknownHostException { + private static void bench(DatabaseReader r, int count, int seed) + throws GeoIp2Exception, + UnknownHostException, + InstantiationException, + IllegalAccessException, + InvocationTargetException, + NoSuchMethodException { Random random = new Random(seed); long startTime = System.nanoTime(); byte[] address = new byte[4]; @@ -65,4 +84,4 @@ private static void bench(DatabaseReader r, int count, int seed) throws GeoIp2Ex long qps = count * 1000000000L / duration; System.out.println("Requests per second: " + qps); } -} \ No newline at end of file +} From e6d4905d44c0dc8e20db52bb24470202508c3ff8 Mon Sep 17 00:00:00 2001 From: William Storey Date: Wed, 19 Aug 2020 08:24:28 -0700 Subject: [PATCH 19/37] Remove stackDepth parameter since it is always constant now --- .../com/maxmind/geoip2/DatabaseReader.java | 87 ++++++++----------- 1 file changed, 34 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 6d4852ef..122cd372 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -256,15 +256,11 @@ Network getNetwork() { * @param ipAddress IPv4 or IPv6 address to lookup. * @param cls The class to deserialize to. * @param expectedType The expected database type. - * @param stackDepth Used to work out how far down the stack we should look, for the method name - * we should use to report back to the user when in error. If this is called directly from the - * method to report to the use set to zero, if this is called indirectly then it is the number of - * methods between this method and the method to report the name of. * @return A LookupResult object with the data for the IP address * @throws IOException if there is an error opening or reading from the file. */ private LookupResult get(InetAddress ipAddress, Class cls, - DatabaseType expectedType, int stackDepth) + DatabaseType expectedType) throws IOException, AddressNotFoundException, InstantiationException, @@ -273,7 +269,7 @@ private LookupResult get(InetAddress ipAddress, Class cls, NoSuchMethodException { if ((databaseType & expectedType.type) == 0) { - String caller = Thread.currentThread().getStackTrace()[2 + stackDepth] + String caller = Thread.currentThread().getStackTrace()[3] .getMethodName(); throw new UnsupportedOperationException( "Invalid attempt to open a " + getMetadata().getDatabaseType() @@ -316,7 +312,7 @@ public CountryResponse country(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getCountry(ipAddress, 1); + Optional r = getCountry(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -332,12 +328,11 @@ public Optional tryCountry(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getCountry(ipAddress, 0); + return getCountry(ipAddress); } private Optional getCountry( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -347,8 +342,7 @@ private Optional getCountry( LookupResult result = this.get( ipAddress, CountryDatabaseModel.class, - DatabaseType.COUNTRY, - stackDepth + DatabaseType.COUNTRY ); CountryDatabaseModel model = result.getModel(); if (model == null) { @@ -372,7 +366,7 @@ public CityResponse city(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getCity(ipAddress, 1); + Optional r = getCity(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -388,12 +382,11 @@ public Optional tryCity(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getCity(ipAddress, 0); + return getCity(ipAddress); } private Optional getCity( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -403,8 +396,7 @@ private Optional getCity( LookupResult result = this.get( ipAddress, CityDatabaseModel.class, - DatabaseType.CITY, - stackDepth + DatabaseType.CITY ); CityDatabaseModel model = result.getModel(); if (model == null) { @@ -436,7 +428,7 @@ public AnonymousIpResponse anonymousIp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getAnonymousIp(ipAddress, 1); + Optional r = getAnonymousIp(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -452,12 +444,11 @@ public Optional tryAnonymousIp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getAnonymousIp(ipAddress, 0); + return getAnonymousIp(ipAddress); } private Optional getAnonymousIp( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -467,8 +458,7 @@ private Optional getAnonymousIp( LookupResult result = this.get( ipAddress, AnonymousIpDatabaseModel.class, - DatabaseType.ANONYMOUS_IP, - stackDepth + DatabaseType.ANONYMOUS_IP ); AnonymousIpDatabaseModel model = result.getModel(); if (model == null) { @@ -499,7 +489,7 @@ public AsnResponse asn(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getAsn(ipAddress, 1); + Optional r = getAsn(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -515,10 +505,10 @@ public Optional tryAsn(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getAsn(ipAddress, 0); + return getAsn(ipAddress); } - private Optional getAsn(InetAddress ipAddress, int stackDepth) + private Optional getAsn(InetAddress ipAddress) throws IOException, GeoIp2Exception, InstantiationException, @@ -528,8 +518,7 @@ private Optional getAsn(InetAddress ipAddress, int stackDepth) LookupResult result = this.get( ipAddress, AsnDatabaseModel.class, - DatabaseType.ASN, - stackDepth + DatabaseType.ASN ); AsnDatabaseModel model = result.getModel(); if (model == null) { @@ -560,7 +549,7 @@ public ConnectionTypeResponse connectionType(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getConnectionType(ipAddress, 1); + Optional r = getConnectionType(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -576,12 +565,11 @@ public Optional tryConnectionType(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getConnectionType(ipAddress, 0); + return getConnectionType(ipAddress); } private Optional getConnectionType( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -591,8 +579,7 @@ private Optional getConnectionType( LookupResult result = this.get( ipAddress, ConnectionTypeDatabaseModel.class, - DatabaseType.CONNECTION_TYPE, - stackDepth + DatabaseType.CONNECTION_TYPE ); ConnectionTypeDatabaseModel model = result.getModel(); if (model == null) { @@ -623,7 +610,7 @@ public DomainResponse domain(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getDomain(ipAddress, 1); + Optional r = getDomain(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -639,12 +626,11 @@ public Optional tryDomain(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getDomain(ipAddress, 0); + return getDomain(ipAddress); } private Optional getDomain( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -654,8 +640,7 @@ private Optional getDomain( LookupResult result = this.get( ipAddress, DomainDatabaseModel.class, - DatabaseType.DOMAIN, - stackDepth + DatabaseType.DOMAIN ); DomainDatabaseModel model = result.getModel(); if (model == null) { @@ -686,7 +671,7 @@ public EnterpriseResponse enterprise(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getEnterprise(ipAddress, 1); + Optional r = getEnterprise(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -702,12 +687,11 @@ public Optional tryEnterprise(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getEnterprise(ipAddress, 0); + return getEnterprise(ipAddress); } private Optional getEnterprise( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -717,8 +701,7 @@ private Optional getEnterprise( LookupResult result = this.get( ipAddress, CityDatabaseModel.class, - DatabaseType.ENTERPRISE, - stackDepth + DatabaseType.ENTERPRISE ); CityDatabaseModel model = result.getModel(); if (model == null) { @@ -750,7 +733,7 @@ public IspResponse isp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Optional r = getIsp(ipAddress, 1); + Optional r = getIsp(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); @@ -766,12 +749,11 @@ public Optional tryIsp(InetAddress ipAddress) IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return getIsp(ipAddress, 0); + return getIsp(ipAddress); } private Optional getIsp( - InetAddress ipAddress, - int stackDepth + InetAddress ipAddress ) throws IOException, GeoIp2Exception, InstantiationException, @@ -781,8 +763,7 @@ private Optional getIsp( LookupResult result = this.get( ipAddress, IspDatabaseModel.class, - DatabaseType.ISP, - stackDepth + DatabaseType.ISP ); IspDatabaseModel model = result.getModel(); if (model == null) { From 09cb6372822d8809be8d3706dea7ac25efd28397 Mon Sep 17 00:00:00 2001 From: William Storey Date: Fri, 21 Aug 2020 15:58:17 -0700 Subject: [PATCH 20/37] Remove unnecessary cast --- src/main/java/com/maxmind/geoip2/DatabaseReader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 122cd372..3be0c6cc 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -280,7 +280,7 @@ private LookupResult get(InetAddress ipAddress, Class cls, // on Java 14 due to the new java.lang.Record. com.maxmind.db.Record record = reader.getRecord(ipAddress, cls); - T o = cls.cast(record.getData()); + T o = record.getData(); return new LookupResult<>(o, ipAddress.getHostAddress(), record.getNetwork()); } From 6a5cc502ff5a72d80aedaa4846cbeaf849b63e80 Mon Sep 17 00:00:00 2001 From: William Storey Date: Sun, 23 Aug 2020 11:49:14 -0700 Subject: [PATCH 21/37] Do not add new exceptions to the API --- sample/Benchmark.java | 25 +- .../com/maxmind/geoip2/DatabaseProvider.java | 123 +++------- .../com/maxmind/geoip2/DatabaseReader.java | 218 ++++-------------- .../com/maxmind/geoip2/GeoIp2Provider.java | 19 +- .../maxmind/geoip2/DatabaseReaderTest.java | 62 +---- 5 files changed, 86 insertions(+), 361 deletions(-) diff --git a/sample/Benchmark.java b/sample/Benchmark.java index 2151fb49..2d760803 100644 --- a/sample/Benchmark.java +++ b/sample/Benchmark.java @@ -1,6 +1,5 @@ import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Random; @@ -21,13 +20,7 @@ public class Benchmark { private final static int BENCHMARKS = 5; private final static boolean TRACE = false; - public static void main(String[] args) - throws GeoIp2Exception, - IOException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public static void main(String[] args) throws GeoIp2Exception, IOException { File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb"); System.out.println("No caching"); loop("Warming up", file, WARMUPS, NoCache.getInstance()); @@ -38,13 +31,7 @@ public static void main(String[] args) loop("Benchmarking", file, BENCHMARKS, new CHMCache()); } - private static void loop(String msg, File file, int loops, NodeCache cache) - throws GeoIp2Exception, - IOException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private static void loop(String msg, File file, int loops, NodeCache cache) throws GeoIp2Exception, IOException { System.out.println(msg); for (int i = 0; i < loops; i++) { DatabaseReader r = new DatabaseReader.Builder(file).fileMode(FileMode.MEMORY_MAPPED).withCache(cache).build(); @@ -53,13 +40,7 @@ private static void loop(String msg, File file, int loops, NodeCache cache) System.out.println(); } - private static void bench(DatabaseReader r, int count, int seed) - throws GeoIp2Exception, - UnknownHostException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private static void bench(DatabaseReader r, int count, int seed) throws GeoIp2Exception, UnknownHostException { Random random = new Random(seed); long startTime = System.nanoTime(); byte[] address = new byte[4]; diff --git a/src/main/java/com/maxmind/geoip2/DatabaseProvider.java b/src/main/java/com/maxmind/geoip2/DatabaseProvider.java index 955a8855..3fcab05e 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseProvider.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseProvider.java @@ -6,7 +6,6 @@ import java.util.Optional; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; public interface DatabaseProvider extends GeoIp2Provider { @@ -17,13 +16,8 @@ public interface DatabaseProvider extends GeoIp2Provider { * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - Optional tryCountry(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryCountry(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * @param ipAddress IPv4 or IPv6 address to lookup. @@ -31,13 +25,8 @@ Optional tryCountry(InetAddress ipAddress) * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - Optional tryCity(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryCity(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Anonymous IP. @@ -47,13 +36,8 @@ Optional tryCity(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - AnonymousIpResponse anonymousIp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Anonymous IP. @@ -63,13 +47,8 @@ AnonymousIpResponse anonymousIp(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryAnonymousIp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryAnonymousIp(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoLite2 ASN database. @@ -79,13 +58,8 @@ Optional tryAnonymousIp(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - AsnResponse asn(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + AsnResponse asn(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoLite2 ASN database. @@ -95,13 +69,8 @@ AsnResponse asn(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryAsn(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryAsn(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Connection Type database. @@ -112,12 +81,7 @@ Optional tryAsn(InetAddress ipAddress) * @throws java.io.IOException if there is an IO error */ ConnectionTypeResponse connectionType(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + throws IOException, GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Connection Type database. @@ -128,12 +92,7 @@ ConnectionTypeResponse connectionType(InetAddress ipAddress) * @throws java.io.IOException if there is an IO error */ Optional tryConnectionType(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + throws IOException, GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Domain database. @@ -143,13 +102,8 @@ Optional tryConnectionType(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - DomainResponse domain(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + DomainResponse domain(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Domain database. @@ -159,13 +113,8 @@ DomainResponse domain(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryDomain(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryDomain(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Enterprise database. @@ -175,13 +124,8 @@ Optional tryDomain(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - EnterpriseResponse enterprise(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 Enterprise database. @@ -191,13 +135,8 @@ EnterpriseResponse enterprise(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryEnterprise(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryEnterprise(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 ISP database. @@ -207,13 +146,8 @@ Optional tryEnterprise(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - IspResponse isp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + IspResponse isp(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * Look up an IP address in a GeoIP2 ISP database. @@ -223,11 +157,6 @@ IspResponse isp(InetAddress ipAddress) * @throws com.maxmind.geoip2.exception.GeoIp2Exception if there is an error looking up the IP * @throws java.io.IOException if there is an IO error */ - Optional tryIsp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + Optional tryIsp(InetAddress ipAddress) throws IOException, + GeoIp2Exception; } diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 3be0c6cc..5bf28553 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -10,7 +10,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.util.Collections; import java.util.List; @@ -85,12 +84,7 @@ private enum DatabaseType { } } - private DatabaseReader(Builder builder) - throws IOException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private DatabaseReader(Builder builder) throws IOException { if (builder.stream != null) { this.reader = new Reader(builder.stream, builder.cache); } else if (builder.database != null) { @@ -218,12 +212,7 @@ public Builder fileMode(FileMode val) { * fields set on this builder. * @throws IOException if there is an error reading the database */ - public DatabaseReader build() - throws IOException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public DatabaseReader build() throws IOException { return new DatabaseReader(this); } } @@ -261,12 +250,7 @@ Network getNetwork() { */ private LookupResult get(InetAddress ipAddress, Class cls, DatabaseType expectedType) - throws IOException, - AddressNotFoundException, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + throws IOException, AddressNotFoundException { if ((databaseType & expectedType.type) == 0) { String caller = Thread.currentThread().getStackTrace()[3] @@ -305,13 +289,8 @@ public void close() throws IOException { } @Override - public CountryResponse country(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public CountryResponse country(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getCountry(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -321,24 +300,14 @@ public CountryResponse country(InetAddress ipAddress) } @Override - public Optional tryCountry(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryCountry(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getCountry(ipAddress); } private Optional getCountry( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, CountryDatabaseModel.class, @@ -359,13 +328,8 @@ private Optional getCountry( } @Override - public CityResponse city(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public CityResponse city(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getCity(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -375,24 +339,14 @@ public CityResponse city(InetAddress ipAddress) } @Override - public Optional tryCity(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryCity(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getCity(ipAddress); } private Optional getCity( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, CityDatabaseModel.class, @@ -421,13 +375,8 @@ private Optional getCity( * @throws IOException if there is an IO error */ @Override - public AnonymousIpResponse anonymousIp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getAnonymousIp(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -437,24 +386,14 @@ public AnonymousIpResponse anonymousIp(InetAddress ipAddress) } @Override - public Optional tryAnonymousIp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryAnonymousIp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getAnonymousIp(ipAddress); } private Optional getAnonymousIp( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, AnonymousIpDatabaseModel.class, @@ -482,13 +421,8 @@ private Optional getAnonymousIp( * @throws IOException if there is an IO error */ @Override - public AsnResponse asn(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public AsnResponse asn(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getAsn(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -498,23 +432,13 @@ public AsnResponse asn(InetAddress ipAddress) } @Override - public Optional tryAsn(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryAsn(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getAsn(ipAddress); } private Optional getAsn(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, AsnDatabaseModel.class, @@ -543,12 +467,7 @@ private Optional getAsn(InetAddress ipAddress) */ @Override public ConnectionTypeResponse connectionType(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + throws IOException, GeoIp2Exception { Optional r = getConnectionType(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -559,23 +478,13 @@ public ConnectionTypeResponse connectionType(InetAddress ipAddress) @Override public Optional tryConnectionType(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + throws IOException, GeoIp2Exception { return getConnectionType(ipAddress); } private Optional getConnectionType( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, ConnectionTypeDatabaseModel.class, @@ -603,13 +512,8 @@ private Optional getConnectionType( * @throws IOException if there is an IO error */ @Override - public DomainResponse domain(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public DomainResponse domain(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getDomain(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -619,24 +523,14 @@ public DomainResponse domain(InetAddress ipAddress) } @Override - public Optional tryDomain(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryDomain(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getDomain(ipAddress); } private Optional getDomain( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, DomainDatabaseModel.class, @@ -664,13 +558,8 @@ private Optional getDomain( * @throws IOException if there is an IO error */ @Override - public EnterpriseResponse enterprise(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getEnterprise(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -680,24 +569,14 @@ public EnterpriseResponse enterprise(InetAddress ipAddress) } @Override - public Optional tryEnterprise(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryEnterprise(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getEnterprise(ipAddress); } private Optional getEnterprise( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, CityDatabaseModel.class, @@ -726,13 +605,8 @@ private Optional getEnterprise( * @throws IOException if there is an IO error */ @Override - public IspResponse isp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public IspResponse isp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { Optional r = getIsp(ipAddress); if (!r.isPresent()) { throw new AddressNotFoundException("The address " @@ -742,24 +616,14 @@ public IspResponse isp(InetAddress ipAddress) } @Override - public Optional tryIsp(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public Optional tryIsp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { return getIsp(ipAddress); } private Optional getIsp( InetAddress ipAddress - ) throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + ) throws IOException, GeoIp2Exception { LookupResult result = this.get( ipAddress, IspDatabaseModel.class, diff --git a/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java b/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java index 8ebbb3a3..9243d203 100644 --- a/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java +++ b/src/main/java/com/maxmind/geoip2/GeoIp2Provider.java @@ -5,7 +5,6 @@ import com.maxmind.geoip2.model.CountryResponse; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; public interface GeoIp2Provider { @@ -16,13 +15,8 @@ public interface GeoIp2Provider { * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - CountryResponse country(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + CountryResponse country(InetAddress ipAddress) throws IOException, + GeoIp2Exception; /** * @param ipAddress IPv4 or IPv6 address to lookup. @@ -30,11 +24,6 @@ CountryResponse country(InetAddress ipAddress) * @throws GeoIp2Exception if there is an error looking up the IP * @throws IOException if there is an IO error */ - CityResponse city(InetAddress ipAddress) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException; + CityResponse city(InetAddress ipAddress) throws IOException, + GeoIp2Exception; } diff --git a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java index 8d53c6fc..40cb8081 100644 --- a/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java +++ b/src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java @@ -13,7 +13,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.URISyntaxException; import java.net.URL; @@ -55,13 +54,8 @@ public void testDefaultLocaleURL() throws Exception { } } - private void testDefaultLocale(DatabaseReader reader) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private void testDefaultLocale(DatabaseReader reader) throws IOException, + GeoIp2Exception { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("London", city.getCity().getName()); } @@ -97,13 +91,8 @@ public void testLocaleListURL() throws Exception { } } - private void testLocaleList(DatabaseReader reader) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private void testLocaleList(DatabaseReader reader) throws IOException, + GeoIp2Exception { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("Лондон", city.getCity().getName()); } @@ -126,26 +115,15 @@ public void testMemoryModeURL() throws Exception { } } - private void testMemoryMode(DatabaseReader reader) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private void testMemoryMode(DatabaseReader reader) throws IOException, + GeoIp2Exception { CityResponse city = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("London", city.getCity().getName()); assertEquals(100, city.getLocation().getAccuracyRadius().longValue()); } @Test - public void metadata() - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public void metadata() throws IOException { DatabaseReader reader = new DatabaseReader.Builder(this.geoipFile) .fileMode(Reader.FileMode.MEMORY).build(); assertEquals("GeoIP2-City", reader.getMetadata().getDatabaseType()); @@ -169,13 +147,8 @@ public void hasIpAddressURL() throws Exception { } } - private void hasIpInfo(DatabaseReader reader) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private void hasIpInfo(DatabaseReader reader) throws IOException, + GeoIp2Exception { CityResponse cio = reader.city(InetAddress.getByName("81.2.69.160")); assertEquals("81.2.69.160", cio.getTraits().getIpAddress()); assertEquals("81.2.69.160/27", cio.getTraits().getNetwork().toString()); @@ -199,13 +172,8 @@ public void unknownAddressURL() throws Exception { } } - private void unknownAddress(DatabaseReader reader) - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + private void unknownAddress(DatabaseReader reader) throws IOException, + GeoIp2Exception { assertFalse(reader.tryCity(InetAddress.getByName("10.10.10.10")).isPresent()); this.exception.expect(AddressNotFoundException.class); @@ -216,13 +184,7 @@ private void unknownAddress(DatabaseReader reader) } @Test - public void testUnsupportedFileMode() - throws IOException, - GeoIp2Exception, - InstantiationException, - IllegalAccessException, - InvocationTargetException, - NoSuchMethodException { + public void testUnsupportedFileMode() throws IOException { this.exception.expect(IllegalArgumentException.class); this.exception.expectMessage(containsString("Only FileMode.MEMORY")); try (DatabaseReader db = new DatabaseReader.Builder(this.geoipStream).fileMode( From 1d500e24884a6fe6b6e8ba4daf13c8b14680f197 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 09:52:50 -0700 Subject: [PATCH 22/37] Use new record class name --- src/main/java/com/maxmind/geoip2/DatabaseReader.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 5bf28553..1a81432f 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -260,9 +260,7 @@ private LookupResult get(InetAddress ipAddress, Class cls, + " database using the " + caller + " method"); } - // We are using the fully qualified name as otherwise it is ambiguous - // on Java 14 due to the new java.lang.Record. - com.maxmind.db.Record record = reader.getRecord(ipAddress, cls); + DatabaseRecord record = reader.getRecord(ipAddress, cls); T o = record.getData(); From 96913f52785b61cc98b0f55b39247072a15bd095 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 09:50:28 -0700 Subject: [PATCH 23/37] Use original classes for ASN deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 ++++---- .../com/maxmind/geoip2/model/AsnResponse.java | 23 +++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 1a81432f..1fd1c1ee 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -437,18 +437,18 @@ public Optional tryAsn(InetAddress ipAddress) throws IOException, private Optional getAsn(InetAddress ipAddress) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - AsnDatabaseModel.class, + AsnResponse.class, DatabaseType.ASN ); - AsnDatabaseModel model = result.getModel(); - if (model == null) { + AsnResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new AsnResponse( - model, + response, result.getIpAddress(), result.getNetwork() ) diff --git a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java index 0a5db153..5c55af42 100644 --- a/src/main/java/com/maxmind/geoip2/model/AsnResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AsnResponse.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; @@ -19,7 +21,7 @@ public class AsnResponse extends AbstractResponse { private final Network network; AsnResponse() { - this(null, null, null, null); + this((Integer) null, null, null, null); } public AsnResponse( @@ -42,14 +44,27 @@ public AsnResponse( this.network = network; } + @MaxMindDbConstructor public AsnResponse( - AsnDatabaseModel model, + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, + @MaxMindDbParameter(name="ip_address") String ipAddress, + @MaxMindDbParameter(name="network") Network network + ) { + this.autonomousSystemNumber = autonomousSystemNumber != null ? autonomousSystemNumber.intValue() : null; + this.autonomousSystemOrganization = autonomousSystemOrganization; + this.ipAddress = ipAddress; + this.network = network; + } + + public AsnResponse( + AsnResponse response, String ipAddress, Network network ) { this( - model.getAutonomousSystemNumber() != null ? model.getAutonomousSystemNumber().intValue() : null, - model.getAutonomousSystemOrganization(), + response.getAutonomousSystemNumber(), + response.getAutonomousSystemOrganization(), ipAddress, network ); From ac48d4f4bb56e3b5ff58c16a24a4479f47735553 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 09:52:16 -0700 Subject: [PATCH 24/37] Use original classes for Country deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +- .../maxmind/geoip2/model/CityResponse.java | 2 +- .../maxmind/geoip2/model/CountryResponse.java | 99 ++++++++++--------- .../geoip2/model/EnterpriseResponse.java | 2 +- .../com/maxmind/geoip2/record/Continent.java | 19 +++- .../com/maxmind/geoip2/record/Country.java | 23 ++++- .../com/maxmind/geoip2/record/MaxMind.java | 5 +- .../geoip2/record/RepresentedCountry.java | 25 ++++- .../com/maxmind/geoip2/record/Traits.java | 49 ++++++++- 9 files changed, 174 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 1fd1c1ee..7e1773a0 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -306,18 +306,18 @@ public Optional tryCountry(InetAddress ipAddress) throws IOExce private Optional getCountry( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - CountryDatabaseModel.class, + CountryResponse.class, DatabaseType.COUNTRY ); - CountryDatabaseModel model = result.getModel(); - if (model == null) { + CountryResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new CountryResponse( - model, + response, result.getIpAddress(), result.getNetwork(), locales diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index cae7969a..a95a95e1 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -122,7 +122,7 @@ public CityResponse( model.getTraits().getStaticIpScore() ) : new Traits( - null, + (Integer) null, null, null, null, diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index f88241e4..93b12b46 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; @@ -21,84 +23,85 @@ public final class CountryResponse extends AbstractCountryResponse { this(null, null, null, null, null, null); } + @MaxMindDbConstructor public CountryResponse( - @JsonProperty("continent") Continent continent, - @JsonProperty("country") Country country, - @JsonProperty("maxmind") MaxMind maxmind, - @JsonProperty("registered_country") Country registeredCountry, - @JsonProperty("represented_country") RepresentedCountry representedCountry, - @JacksonInject("traits") @JsonProperty("traits") Traits traits + @JsonProperty("continent") @MaxMindDbParameter(name="continent") Continent continent, + @JsonProperty("country") @MaxMindDbParameter(name="country") Country country, + @JsonProperty("maxmind") @MaxMindDbParameter(name="maxmind") MaxMind maxmind, + @JsonProperty("registered_country") @MaxMindDbParameter(name="registered_country") Country registeredCountry, + @JsonProperty("represented_country") @MaxMindDbParameter(name="represented_country") RepresentedCountry representedCountry, + @JacksonInject("traits") @JsonProperty("traits") @MaxMindDbParameter(name="traits") Traits traits ) { super(continent, country, maxmind, registeredCountry, representedCountry, traits); } public CountryResponse( - CountryDatabaseModel model, + CountryResponse response, String ipAddress, Network network, List locales ) { this( - model.getContinent() != null ? + response.getContinent() != null ? new Continent( locales, - model.getContinent().getCode(), - model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, - model.getContinent().getNames() + response.getContinent().getCode(), + response.getContinent().getGeoNameId(), + response.getContinent().getNames() ) : null, - model.getCountry() != null ? + response.getCountry() != null ? new Country( locales, - model.getCountry().getConfidence(), - model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, - model.getCountry().getIsInEuropeanUnion(), - model.getCountry().getIsoCode(), - model.getCountry().getNames() + response.getCountry().getConfidence(), + response.getCountry().getGeoNameId(), + response.getCountry().isInEuropeanUnion(), + response.getCountry().getIsoCode(), + response.getCountry().getNames() ) : null, null, - model.getRegisteredCountry() != null ? + response.getRegisteredCountry() != null ? new Country( locales, - model.getRegisteredCountry().getConfidence(), - model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, - model.getRegisteredCountry().getIsInEuropeanUnion(), - model.getRegisteredCountry().getIsoCode(), - model.getRegisteredCountry().getNames() + response.getRegisteredCountry().getConfidence(), + response.getRegisteredCountry().getGeoNameId(), + response.getRegisteredCountry().isInEuropeanUnion(), + response.getRegisteredCountry().getIsoCode(), + response.getRegisteredCountry().getNames() ) : null, - model.getRepresentedCountry() != null ? + response.getRepresentedCountry() != null ? new RepresentedCountry( locales, - model.getRepresentedCountry().getConfidence(), - model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, - model.getRepresentedCountry().getIsInEuropeanUnion(), - model.getRepresentedCountry().getIsoCode(), - model.getRepresentedCountry().getNames(), - model.getRepresentedCountry().getType() + response.getRepresentedCountry().getConfidence(), + response.getRepresentedCountry().getGeoNameId(), + response.getRepresentedCountry().isInEuropeanUnion(), + response.getRepresentedCountry().getIsoCode(), + response.getRepresentedCountry().getNames(), + response.getRepresentedCountry().getType() ) : null, - model.getTraits() != null ? + response.getTraits() != null ? new Traits( - model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, - model.getTraits().getAutonomousSystemOrganization(), - model.getTraits().getConnectionType(), - model.getTraits().getDomain(), + response.getTraits().getAutonomousSystemNumber(), + response.getTraits().getAutonomousSystemOrganization(), + response.getTraits().getConnectionType(), + response.getTraits().getDomain(), ipAddress, - model.getTraits().isAnonymous(), - model.getTraits().isAnonymousProxy(), - model.getTraits().isAnonymousVpn(), - model.getTraits().isHostingProvider(), - model.getTraits().isLegitimateProxy(), - model.getTraits().isPublicProxy(), - model.getTraits().isSatelliteProvider(), - model.getTraits().isTorExitNode(), - model.getTraits().getIsp(), + response.getTraits().isAnonymous(), + response.getTraits().isAnonymousProxy(), + response.getTraits().isAnonymousVpn(), + response.getTraits().isHostingProvider(), + response.getTraits().isLegitimateProxy(), + response.getTraits().isPublicProxy(), + response.getTraits().isSatelliteProvider(), + response.getTraits().isTorExitNode(), + response.getTraits().getIsp(), network, - model.getTraits().getOrganization(), - model.getTraits().getUserType(), + response.getTraits().getOrganization(), + response.getTraits().getUserType(), null, - model.getTraits().getStaticIpScore() + response.getTraits().getStaticIpScore() ) : new Traits( - null, + (Integer) null, null, null, null, diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index 7a379e83..ee37783e 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -108,7 +108,7 @@ public EnterpriseResponse( model.getTraits().getStaticIpScore() ) : new Traits( - null, + (Integer) null, null, null, null, diff --git a/src/main/java/com/maxmind/geoip2/record/Continent.java b/src/main/java/com/maxmind/geoip2/record/Continent.java index 9aaecae3..1bc2ddb2 100644 --- a/src/main/java/com/maxmind/geoip2/record/Continent.java +++ b/src/main/java/com/maxmind/geoip2/record/Continent.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import java.util.List; import java.util.Map; @@ -23,7 +25,7 @@ public final class Continent extends AbstractNamedRecord { private final String code; public Continent() { - this(null, null, null, null); + this(null, null, (Integer) null, null); } public Continent( @@ -36,6 +38,21 @@ public Continent( this.code = code; } + @MaxMindDbConstructor + public Continent( + @MaxMindDbParameter(name="locales") List locales, + @MaxMindDbParameter(name="code") String code, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="names") Map names + ) { + this( + locales, + code, + geoNameId != null ? geoNameId.intValue() : null, + names + ); + } + /** * @return A two character continent code like "NA" (North America) or "OC" * (Oceania). This attribute is returned by all end points. diff --git a/src/main/java/com/maxmind/geoip2/record/Country.java b/src/main/java/com/maxmind/geoip2/record/Country.java index 42126c22..06a08adc 100644 --- a/src/main/java/com/maxmind/geoip2/record/Country.java +++ b/src/main/java/com/maxmind/geoip2/record/Country.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import java.util.List; import java.util.Map; @@ -25,7 +27,7 @@ public class Country extends AbstractNamedRecord { private final String isoCode; public Country() { - this(null, null, null, false, null, null); + this(null, null, (Integer) null, false, null, null); } // This method is for backwards compatibility. We should remove it when we @@ -54,6 +56,25 @@ public Country( this.isoCode = isoCode; } + @MaxMindDbConstructor + public Country( + @MaxMindDbParameter(name="locales") List locales, + @MaxMindDbParameter(name="confidence") Integer confidence, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names + ) { + this( + locales, + confidence, + geoNameId != null ? geoNameId.intValue() : null, + isInEuropeanUnion != null ? isInEuropeanUnion : false, + isoCode, + names + ); + } + /** * @return A value from 0-100 indicating MaxMind's confidence that the * country is correct. This attribute is only available from the diff --git a/src/main/java/com/maxmind/geoip2/record/MaxMind.java b/src/main/java/com/maxmind/geoip2/record/MaxMind.java index 2473b608..62e914e7 100644 --- a/src/main/java/com/maxmind/geoip2/record/MaxMind.java +++ b/src/main/java/com/maxmind/geoip2/record/MaxMind.java @@ -1,6 +1,8 @@ package com.maxmind.geoip2.record; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; /** *

@@ -18,7 +20,8 @@ public MaxMind() { this(null); } - public MaxMind(@JsonProperty("queries_remaining") Integer queriesRemaining) { + @MaxMindDbConstructor + public MaxMind(@JsonProperty("queries_remaining") @MaxMindDbParameter(name="queries_remaining") Integer queriesRemaining) { this.queriesRemaining = queriesRemaining; } diff --git a/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java b/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java index c6147bd9..8873318e 100644 --- a/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java +++ b/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import java.util.List; import java.util.Map; @@ -25,7 +27,7 @@ public final class RepresentedCountry extends Country { private final String type; public RepresentedCountry() { - this(null, null, null, false, null, null, null); + this(null, null, (Integer) null, false, null, null, null); } // This method is for backwards compatibility. We should remove it when we @@ -55,6 +57,27 @@ public RepresentedCountry( this.type = type; } + @MaxMindDbConstructor + public RepresentedCountry( + @MaxMindDbParameter(name="locales") List locales, + @MaxMindDbParameter(name="confidence") Integer confidence, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names, + @MaxMindDbParameter(name="type") String type + ) { + this( + locales, + confidence, + geoNameId != null ? geoNameId.intValue() : null, + isInEuropeanUnion != null ? isInEuropeanUnion : false, + isoCode, + names, + type + ); + } + /** * @return A string indicating the type of entity that is representing the * country. Currently we only return {@code military} but this could diff --git a/src/main/java/com/maxmind/geoip2/record/Traits.java b/src/main/java/com/maxmind/geoip2/record/Traits.java index afef58b9..1a2e621b 100644 --- a/src/main/java/com/maxmind/geoip2/record/Traits.java +++ b/src/main/java/com/maxmind/geoip2/record/Traits.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; @@ -48,7 +50,7 @@ public Traits(String ipAddress) { } public Traits(String ipAddress, Network network) { - this(null, null, null, null, + this((Integer) null, null, null, null, ipAddress, false, false, false, false, false, false, false, false, null, network, null, null, null, null); @@ -160,6 +162,51 @@ public Traits( this.staticIpScore = staticIpScore; } + @MaxMindDbConstructor + public Traits( + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, + @MaxMindDbParameter(name="connection_type") String connectionType, + @MaxMindDbParameter(name="domain") String domain, + @MaxMindDbParameter(name="ip_address") String ipAddress, + @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, + @MaxMindDbParameter(name="is_anonymous_proxy") Boolean isAnonymousProxy, + @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, + @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, + @MaxMindDbParameter(name="is_legitimate_proxy") Boolean isLegitimateProxy, + @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, + @MaxMindDbParameter(name="is_satellite_provider") Boolean isSatelliteProvider, + @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode, + @MaxMindDbParameter(name="isp") String isp, + @MaxMindDbParameter(name="network") Network network, + @MaxMindDbParameter(name="organization") String organization, + @MaxMindDbParameter(name="user_type") String userType, + @MaxMindDbParameter(name="user_count") Integer userCount, + @MaxMindDbParameter(name="static_ip_score") Double staticIpScore + ) { + this( + autonomousSystemNumber != null ? autonomousSystemNumber.intValue() : null, + autonomousSystemOrganization, + ConnectionType.fromString(connectionType), + domain, + ipAddress, + isAnonymous != null ? isAnonymous : false, + isAnonymousProxy != null ? isAnonymousProxy : false, + isAnonymousVpn != null ? isAnonymousVpn : false, + isHostingProvider != null ? isHostingProvider : false, + isLegitimateProxy != null ? isLegitimateProxy : false, + isPublicProxy != null ? isPublicProxy : false, + isSatelliteProvider != null ? isSatelliteProvider : false, + isTorExitNode != null ? isTorExitNode : false, + isp, + network, + organization, + userType, + userCount, + staticIpScore + ); + } + /** * @return The Date: Mon, 24 Aug 2020 10:34:09 -0700 Subject: [PATCH 25/37] Use original classes for City deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +- .../maxmind/geoip2/model/CityResponse.java | 125 +++++++++--------- .../java/com/maxmind/geoip2/record/City.java | 19 ++- .../maxmind/geoip2/record/Subdivision.java | 21 ++- 4 files changed, 107 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 7e1773a0..ccf8a353 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -345,18 +345,18 @@ public Optional tryCity(InetAddress ipAddress) throws IOException, private Optional getCity( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - CityDatabaseModel.class, + CityResponse.class, DatabaseType.CITY ); - CityDatabaseModel model = result.getModel(); - if (model == null) { + CityResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new CityResponse( - model, + response, result.getIpAddress(), result.getNetwork(), locales diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index a95a95e1..6fb5a369 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; @@ -30,96 +32,97 @@ public final class CityResponse extends AbstractCityResponse { this(null, null, null, null, null, null, null, null, null, null); } + @MaxMindDbConstructor public CityResponse( - @JsonProperty("city") City city, - @JsonProperty("continent") Continent continent, - @JsonProperty("country") Country country, - @JsonProperty("location") Location location, - @JsonProperty("maxmind") MaxMind maxmind, - @JsonProperty("postal") Postal postal, - @JsonProperty("registered_country") Country registeredCountry, - @JsonProperty("represented_country") RepresentedCountry representedCountry, - @JsonProperty("subdivisions") ArrayList subdivisions, - @JacksonInject("traits") @JsonProperty("traits") Traits traits + @JsonProperty("city") @MaxMindDbParameter(name="city") City city, + @JsonProperty("continent") @MaxMindDbParameter(name="continent") Continent continent, + @JsonProperty("country") @MaxMindDbParameter(name="country") Country country, + @JsonProperty("location") @MaxMindDbParameter(name="location") Location location, + @JsonProperty("maxmind") @MaxMindDbParameter(name="maxmind") MaxMind maxmind, + @JsonProperty("postal") @MaxMindDbParameter(name="postal") Postal postal, + @JsonProperty("registered_country") @MaxMindDbParameter(name="registered_country") Country registeredCountry, + @JsonProperty("represented_country") @MaxMindDbParameter(name="represented_country") RepresentedCountry representedCountry, + @JsonProperty("subdivisions") @MaxMindDbParameter(name="subdivisions") ArrayList subdivisions, + @JacksonInject("traits") @JsonProperty("traits") @MaxMindDbParameter(name="traits") Traits traits ) { super(city, continent, country, location, maxmind, postal, registeredCountry, representedCountry, subdivisions, traits); } public CityResponse( - CityDatabaseModel model, + CityResponse response, String ipAddress, Network network, List locales ) { this( - model.getCity() != null ? + response.getCity() != null ? new City( locales, - model.getCity().getConfidence(), - model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, - model.getCity().getNames() + response.getCity().getConfidence(), + response.getCity().getGeoNameId(), + response.getCity().getNames() ) : null, - model.getContinent() != null ? + response.getContinent() != null ? new Continent( locales, - model.getContinent().getCode(), - model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, - model.getContinent().getNames() + response.getContinent().getCode(), + response.getContinent().getGeoNameId(), + response.getContinent().getNames() ) : null, - model.getCountry() != null ? + response.getCountry() != null ? new Country( locales, - model.getCountry().getConfidence(), - model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, - model.getCountry().getIsInEuropeanUnion(), - model.getCountry().getIsoCode(), - model.getCountry().getNames() + response.getCountry().getConfidence(), + response.getCountry().getGeoNameId(), + response.getCountry().isInEuropeanUnion(), + response.getCountry().getIsoCode(), + response.getCountry().getNames() ) : null, - model.getLocation(), + response.getLocation(), null, - model.getPostal(), - model.getRegisteredCountry() != null ? + response.getPostal(), + response.getRegisteredCountry() != null ? new Country( locales, - model.getRegisteredCountry().getConfidence(), - model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, - model.getRegisteredCountry().getIsInEuropeanUnion(), - model.getRegisteredCountry().getIsoCode(), - model.getRegisteredCountry().getNames() + response.getRegisteredCountry().getConfidence(), + response.getRegisteredCountry().getGeoNameId(), + response.getRegisteredCountry().isInEuropeanUnion(), + response.getRegisteredCountry().getIsoCode(), + response.getRegisteredCountry().getNames() ) : null, - model.getRepresentedCountry() != null ? + response.getRepresentedCountry() != null ? new RepresentedCountry( locales, - model.getRepresentedCountry().getConfidence(), - model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, - model.getRepresentedCountry().getIsInEuropeanUnion(), - model.getRepresentedCountry().getIsoCode(), - model.getRepresentedCountry().getNames(), - model.getRepresentedCountry().getType() + response.getRepresentedCountry().getConfidence(), + response.getRepresentedCountry().getGeoNameId(), + response.getRepresentedCountry().isInEuropeanUnion(), + response.getRepresentedCountry().getIsoCode(), + response.getRepresentedCountry().getNames(), + response.getRepresentedCountry().getType() ) : null, - mapSubdivisions(locales, model.getSubdivisions()), - model.getTraits() != null ? + mapSubdivisions(locales, response.getSubdivisions()), + response.getTraits() != null ? new Traits( - model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, - model.getTraits().getAutonomousSystemOrganization(), - model.getTraits().getConnectionType(), - model.getTraits().getDomain(), + response.getTraits().getAutonomousSystemNumber(), + response.getTraits().getAutonomousSystemOrganization(), + response.getTraits().getConnectionType(), + response.getTraits().getDomain(), ipAddress, - model.getTraits().isAnonymous(), - model.getTraits().isAnonymousProxy(), - model.getTraits().isAnonymousVpn(), - model.getTraits().isHostingProvider(), - model.getTraits().isLegitimateProxy(), - model.getTraits().isPublicProxy(), - model.getTraits().isSatelliteProvider(), - model.getTraits().isTorExitNode(), - model.getTraits().getIsp(), + response.getTraits().isAnonymous(), + response.getTraits().isAnonymousProxy(), + response.getTraits().isAnonymousVpn(), + response.getTraits().isHostingProvider(), + response.getTraits().isLegitimateProxy(), + response.getTraits().isPublicProxy(), + response.getTraits().isSatelliteProvider(), + response.getTraits().isTorExitNode(), + response.getTraits().getIsp(), network, - model.getTraits().getOrganization(), - model.getTraits().getUserType(), + response.getTraits().getOrganization(), + response.getTraits().getUserType(), null, - model.getTraits().getStaticIpScore() + response.getTraits().getStaticIpScore() ) : new Traits( (Integer) null, @@ -147,19 +150,19 @@ public CityResponse( private static ArrayList mapSubdivisions( List locales, - ArrayList subdivisions + List subdivisions ) { if (subdivisions == null) { return null; } ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); - for (SubdivisionDatabaseRecord subdivision : subdivisions) { + for (Subdivision subdivision : subdivisions) { subdivisions2.add( new Subdivision( locales, subdivision.getConfidence(), - subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, + subdivision.getGeoNameId(), subdivision.getIsoCode(), subdivision.getNames() ) diff --git a/src/main/java/com/maxmind/geoip2/record/City.java b/src/main/java/com/maxmind/geoip2/record/City.java index 00ddff6d..6fbed435 100644 --- a/src/main/java/com/maxmind/geoip2/record/City.java +++ b/src/main/java/com/maxmind/geoip2/record/City.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import java.util.List; import java.util.Map; @@ -23,7 +25,7 @@ public final class City extends AbstractNamedRecord { private final Integer confidence; public City() { - this(null, null, null, null); + this(null, null, (Integer) null, null); } public City( @@ -36,6 +38,21 @@ public City( this.confidence = confidence; } + @MaxMindDbConstructor + public City( + @MaxMindDbParameter(name="locales") List locales, + @MaxMindDbParameter(name="confidence") Integer confidence, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="names") Map names + ) { + this( + locales, + confidence, + geoNameId != null ? geoNameId.intValue() : null, + names + ); + } + /** * @return A value from 0-100 indicating MaxMind's confidence that the city * is correct. This attribute is only available from the Insights diff --git a/src/main/java/com/maxmind/geoip2/record/Subdivision.java b/src/main/java/com/maxmind/geoip2/record/Subdivision.java index d4b71e61..1fbe1aeb 100644 --- a/src/main/java/com/maxmind/geoip2/record/Subdivision.java +++ b/src/main/java/com/maxmind/geoip2/record/Subdivision.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import java.util.List; import java.util.Map; @@ -24,7 +26,7 @@ public final class Subdivision extends AbstractNamedRecord { private final String isoCode; public Subdivision() { - this(null, null, null, null, null); + this(null, null, (Integer) null, null, null); } public Subdivision( @@ -39,6 +41,23 @@ public Subdivision( this.isoCode = isoCode; } + @MaxMindDbConstructor + public Subdivision( + @MaxMindDbParameter(name="locales") List locales, + @MaxMindDbParameter(name="confidence") Integer confidence, + @MaxMindDbParameter(name="geoname_id") Long geoNameId, + @MaxMindDbParameter(name="iso_code") String isoCode, + @MaxMindDbParameter(name="names") Map names + ) { + this( + locales, + confidence, + geoNameId != null ? geoNameId.intValue() : null, + isoCode, + names + ); + } + /** * @return This is a value from 0-100 indicating MaxMind's confidence that * the subdivision is correct. This attribute is only available from From 72341985da49c10cc06ba89a85a59c07b6e2badc Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:39:01 -0700 Subject: [PATCH 26/37] Use original classes for Anonymous IP deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +++--- .../geoip2/model/AnonymousIpResponse.java | 35 +++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index ccf8a353..51f8ea04 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -392,18 +392,18 @@ public Optional tryAnonymousIp(InetAddress ipAddress) throw private Optional getAnonymousIp( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - AnonymousIpDatabaseModel.class, + AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP ); - AnonymousIpDatabaseModel model = result.getModel(); - if (model == null) { + AnonymousIpResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new AnonymousIpResponse( - model, + response, result.getIpAddress(), result.getNetwork() ) diff --git a/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java b/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java index e1e947c0..c06976a9 100644 --- a/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AnonymousIpResponse.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; @@ -55,18 +57,39 @@ public AnonymousIpResponse( this.network = network; } + @MaxMindDbConstructor public AnonymousIpResponse( - AnonymousIpDatabaseModel model, + @MaxMindDbParameter(name="ip_address") String ipAddress, + @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, + @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, + @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, + @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, + @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode, + @MaxMindDbParameter(name="network") Network network + ) { + this( + ipAddress, + isAnonymous != null ? isAnonymous : false, + isAnonymousVpn != null ? isAnonymousVpn : false, + isHostingProvider != null ? isHostingProvider : false, + isPublicProxy != null ? isPublicProxy : false, + isTorExitNode != null ? isTorExitNode : false, + network + ); + } + + public AnonymousIpResponse( + AnonymousIpResponse response, String ipAddress, Network network ) { this( ipAddress, - model.getIsAnonymous(), - model.getIsAnonymousVpn(), - model.getIsHostingProvider(), - model.getIsPublicProxy(), - model.getIsTorExitNode(), + response.isAnonymous(), + response.isAnonymousVpn(), + response.isHostingProvider(), + response.isPublicProxy(), + response.isTorExitNode(), network ); } From 19ac33e50d5b7156920e5fd9a5a6c3d54ce507b6 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:41:51 -0700 Subject: [PATCH 27/37] Use original classes for Connection Type deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +++++----- .../geoip2/model/ConnectionTypeResponse.java | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 51f8ea04..3727c80d 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -483,18 +483,18 @@ public Optional tryConnectionType(InetAddress ipAddress) private Optional getConnectionType( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - ConnectionTypeDatabaseModel.class, + ConnectionTypeResponse.class, DatabaseType.CONNECTION_TYPE ); - ConnectionTypeDatabaseModel model = result.getModel(); - if (model == null) { + ConnectionTypeResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new ConnectionTypeResponse( - model, + response, result.getIpAddress(), result.getNetwork() ) diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java index 54e7d3c1..b39cd9c8 100644 --- a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeResponse.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; @@ -83,13 +85,26 @@ public ConnectionTypeResponse( this.network = network; } + @MaxMindDbConstructor public ConnectionTypeResponse( - ConnectionTypeDatabaseModel model, + @MaxMindDbParameter(name="connection_type") String connectionType, + @MaxMindDbParameter(name="ip_address") String ipAddress, + @MaxMindDbParameter(name="network") Network network + ) { + this( + ConnectionType.fromString(connectionType), + ipAddress, + network + ); + } + + public ConnectionTypeResponse( + ConnectionTypeResponse response, String ipAddress, Network network ) { this( - model.getConnectionType(), + response.getConnectionType(), ipAddress, network ); From eecb52afb1378f952f655f30c0f37b3ea8596225 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:46:47 -0700 Subject: [PATCH 28/37] Use original classes for Domain deserializing --- .../java/com/maxmind/geoip2/DatabaseReader.java | 10 +++++----- .../com/maxmind/geoip2/model/DomainResponse.java | 13 ++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 3727c80d..3e1ff276 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -529,18 +529,18 @@ public Optional tryDomain(InetAddress ipAddress) throws IOExcept private Optional getDomain( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - DomainDatabaseModel.class, + DomainResponse.class, DatabaseType.DOMAIN ); - DomainDatabaseModel model = result.getModel(); - if (model == null) { + DomainResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new DomainResponse( - model, + response, result.getIpAddress(), result.getNetwork() ) diff --git a/src/main/java/com/maxmind/geoip2/model/DomainResponse.java b/src/main/java/com/maxmind/geoip2/model/DomainResponse.java index 0ca5c48d..0e7f4fe7 100644 --- a/src/main/java/com/maxmind/geoip2/model/DomainResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/DomainResponse.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; @@ -28,10 +30,11 @@ public DomainResponse( this(domain, ipAddress, null); } + @MaxMindDbConstructor public DomainResponse( - @JsonProperty("domain") String domain, - @JacksonInject("ip_address") @JsonProperty("ip_address") String ipAddress, - @JacksonInject("network") @JsonProperty("network") @JsonDeserialize(using = NetworkDeserializer.class) Network network + @JsonProperty("domain") @MaxMindDbParameter(name="domain") String domain, + @JacksonInject("ip_address") @JsonProperty("ip_address") @MaxMindDbParameter(name="ip_address") String ipAddress, + @JacksonInject("network") @JsonProperty("network") @JsonDeserialize(using = NetworkDeserializer.class) @MaxMindDbParameter(name="network") Network network ) { this.domain = domain; this.ipAddress = ipAddress; @@ -39,11 +42,11 @@ public DomainResponse( } public DomainResponse( - DomainDatabaseModel model, + DomainResponse response, String ipAddress, Network network ) { - this(model.getDomain(), ipAddress, network); + this(response.getDomain(), ipAddress, network); } /** From 19c7d880bb642e62d07ca775582195f2e8767393 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:50:14 -0700 Subject: [PATCH 29/37] Use original classes for Enterprise deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +- .../geoip2/model/EnterpriseResponse.java | 125 +++++++++--------- 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index 3e1ff276..de06366b 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -575,18 +575,18 @@ public Optional tryEnterprise(InetAddress ipAddress) throws private Optional getEnterprise( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - CityDatabaseModel.class, + EnterpriseResponse.class, DatabaseType.ENTERPRISE ); - CityDatabaseModel model = result.getModel(); - if (model == null) { + EnterpriseResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new EnterpriseResponse( - model, + response, result.getIpAddress(), result.getNetwork(), locales diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index ee37783e..7766b7ee 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; @@ -16,96 +18,97 @@ */ public final class EnterpriseResponse extends AbstractCityResponse { + @MaxMindDbConstructor public EnterpriseResponse( - @JsonProperty("city") City city, - @JsonProperty("continent") Continent continent, - @JsonProperty("country") Country country, - @JsonProperty("location") Location location, - @JsonProperty("maxmind") MaxMind maxmind, - @JsonProperty("postal") Postal postal, - @JsonProperty("registered_country") Country registeredCountry, - @JsonProperty("represented_country") RepresentedCountry representedCountry, - @JsonProperty("subdivisions") ArrayList subdivisions, - @JacksonInject("traits") @JsonProperty("traits") Traits traits + @JsonProperty("city") @MaxMindDbParameter(name="city") City city, + @JsonProperty("continent") @MaxMindDbParameter(name="continent") Continent continent, + @JsonProperty("country") @MaxMindDbParameter(name="country") Country country, + @JsonProperty("location") @MaxMindDbParameter(name="location") Location location, + @JsonProperty("maxmind") @MaxMindDbParameter(name="maxmind") MaxMind maxmind, + @JsonProperty("postal") @MaxMindDbParameter(name="postal") Postal postal, + @JsonProperty("registered_country") @MaxMindDbParameter(name="registered_country") Country registeredCountry, + @JsonProperty("represented_country") @MaxMindDbParameter(name="represented_country") RepresentedCountry representedCountry, + @JsonProperty("subdivisions") @MaxMindDbParameter(name="subdivisions") ArrayList subdivisions, + @JacksonInject("traits") @JsonProperty("traits") @MaxMindDbParameter(name="traits") Traits traits ) { super(city, continent, country, location, maxmind, postal, registeredCountry, representedCountry, subdivisions, traits); } public EnterpriseResponse( - CityDatabaseModel model, + EnterpriseResponse response, String ipAddress, Network network, List locales ) { this( - model.getCity() != null ? + response.getCity() != null ? new City( locales, - model.getCity().getConfidence(), - model.getCity().getGeoNameId() != null ? model.getCity().getGeoNameId().intValue() : null, - model.getCity().getNames() + response.getCity().getConfidence(), + response.getCity().getGeoNameId(), + response.getCity().getNames() ) : null, - model.getContinent() != null ? + response.getContinent() != null ? new Continent( locales, - model.getContinent().getCode(), - model.getContinent().getGeoNameId() != null ? model.getContinent().getGeoNameId().intValue() : null, - model.getContinent().getNames() + response.getContinent().getCode(), + response.getContinent().getGeoNameId(), + response.getContinent().getNames() ) : null, - model.getCountry() != null ? + response.getCountry() != null ? new Country( locales, - model.getCountry().getConfidence(), - model.getCountry().getGeoNameId() != null ? model.getCountry().getGeoNameId().intValue() : null, - model.getCountry().getIsInEuropeanUnion(), - model.getCountry().getIsoCode(), - model.getCountry().getNames() + response.getCountry().getConfidence(), + response.getCountry().getGeoNameId(), + response.getCountry().isInEuropeanUnion(), + response.getCountry().getIsoCode(), + response.getCountry().getNames() ) : null, - model.getLocation(), + response.getLocation(), null, - model.getPostal(), - model.getRegisteredCountry() != null ? + response.getPostal(), + response.getRegisteredCountry() != null ? new Country( locales, - model.getRegisteredCountry().getConfidence(), - model.getRegisteredCountry().getGeoNameId() != null ? model.getRegisteredCountry().getGeoNameId().intValue() : null, - model.getRegisteredCountry().getIsInEuropeanUnion(), - model.getRegisteredCountry().getIsoCode(), - model.getRegisteredCountry().getNames() + response.getRegisteredCountry().getConfidence(), + response.getRegisteredCountry().getGeoNameId(), + response.getRegisteredCountry().isInEuropeanUnion(), + response.getRegisteredCountry().getIsoCode(), + response.getRegisteredCountry().getNames() ) : null, - model.getRepresentedCountry() != null ? + response.getRepresentedCountry() != null ? new RepresentedCountry( locales, - model.getRepresentedCountry().getConfidence(), - model.getRepresentedCountry().getGeoNameId() != null ? model.getRepresentedCountry().getGeoNameId().intValue() : null, - model.getRepresentedCountry().getIsInEuropeanUnion(), - model.getRepresentedCountry().getIsoCode(), - model.getRepresentedCountry().getNames(), - model.getRepresentedCountry().getType() + response.getRepresentedCountry().getConfidence(), + response.getRepresentedCountry().getGeoNameId(), + response.getRepresentedCountry().isInEuropeanUnion(), + response.getRepresentedCountry().getIsoCode(), + response.getRepresentedCountry().getNames(), + response.getRepresentedCountry().getType() ) : null, - mapSubdivisions(locales, model.getSubdivisions()), - model.getTraits() != null ? + mapSubdivisions(locales, response.getSubdivisions()), + response.getTraits() != null ? new Traits( - model.getTraits().getAutonomousSystemNumber() != null ? model.getTraits().getAutonomousSystemNumber().intValue() : null, - model.getTraits().getAutonomousSystemOrganization(), - model.getTraits().getConnectionType(), - model.getTraits().getDomain(), + response.getTraits().getAutonomousSystemNumber(), + response.getTraits().getAutonomousSystemOrganization(), + response.getTraits().getConnectionType(), + response.getTraits().getDomain(), ipAddress, - model.getTraits().isAnonymous(), - model.getTraits().isAnonymousProxy(), - model.getTraits().isAnonymousVpn(), - model.getTraits().isHostingProvider(), - model.getTraits().isLegitimateProxy(), - model.getTraits().isPublicProxy(), - model.getTraits().isSatelliteProvider(), - model.getTraits().isTorExitNode(), - model.getTraits().getIsp(), + response.getTraits().isAnonymous(), + response.getTraits().isAnonymousProxy(), + response.getTraits().isAnonymousVpn(), + response.getTraits().isHostingProvider(), + response.getTraits().isLegitimateProxy(), + response.getTraits().isPublicProxy(), + response.getTraits().isSatelliteProvider(), + response.getTraits().isTorExitNode(), + response.getTraits().getIsp(), network, - model.getTraits().getOrganization(), - model.getTraits().getUserType(), + response.getTraits().getOrganization(), + response.getTraits().getUserType(), null, - model.getTraits().getStaticIpScore() + response.getTraits().getStaticIpScore() ) : new Traits( (Integer) null, @@ -133,19 +136,19 @@ public EnterpriseResponse( private static ArrayList mapSubdivisions( List locales, - ArrayList subdivisions + List subdivisions ) { if (subdivisions == null) { return null; } ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); - for (SubdivisionDatabaseRecord subdivision : subdivisions) { + for (Subdivision subdivision : subdivisions) { subdivisions2.add( new Subdivision( locales, subdivision.getConfidence(), - subdivision.getGeoNameId() != null ? subdivision.getGeoNameId().intValue() : null, + subdivision.getGeoNameId(), subdivision.getIsoCode(), subdivision.getNames() ) From 6cbf8327eb7340eb452997fa96f28d7c9f016025 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:53:19 -0700 Subject: [PATCH 30/37] Use original classes for ISP deserializing --- .../com/maxmind/geoip2/DatabaseReader.java | 10 +++--- .../com/maxmind/geoip2/model/IspResponse.java | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/DatabaseReader.java b/src/main/java/com/maxmind/geoip2/DatabaseReader.java index de06366b..ba9f9903 100644 --- a/src/main/java/com/maxmind/geoip2/DatabaseReader.java +++ b/src/main/java/com/maxmind/geoip2/DatabaseReader.java @@ -622,18 +622,18 @@ public Optional tryIsp(InetAddress ipAddress) throws IOException, private Optional getIsp( InetAddress ipAddress ) throws IOException, GeoIp2Exception { - LookupResult result = this.get( + LookupResult result = this.get( ipAddress, - IspDatabaseModel.class, + IspResponse.class, DatabaseType.ISP ); - IspDatabaseModel model = result.getModel(); - if (model == null) { + IspResponse response = result.getModel(); + if (response == null) { return Optional.empty(); } return Optional.of( new IspResponse( - model, + response, result.getIpAddress(), result.getNetwork() ) diff --git a/src/main/java/com/maxmind/geoip2/model/IspResponse.java b/src/main/java/com/maxmind/geoip2/model/IspResponse.java index fa9925f2..c244801e 100644 --- a/src/main/java/com/maxmind/geoip2/model/IspResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/IspResponse.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; import com.maxmind.db.Network; import com.maxmind.geoip2.NetworkDeserializer; @@ -41,17 +43,36 @@ public IspResponse( this.organization = organization; } + @MaxMindDbConstructor public IspResponse( - IspDatabaseModel model, + @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, + @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, + @MaxMindDbParameter(name="ip_address") String ipAddress, + @MaxMindDbParameter(name="isp") String isp, + @MaxMindDbParameter(name="organization") String organization, + @MaxMindDbParameter(name="network") Network network + ) { + this( + autonomousSystemNumber != null ? autonomousSystemNumber.intValue() : null, + autonomousSystemOrganization, + ipAddress, + isp, + organization, + network + ); + } + + public IspResponse( + IspResponse response, String ipAddress, Network network ) { this( - model.getAutonomousSystemNumber() != null ? model.getAutonomousSystemNumber().intValue() : null, - model.getAutonomousSystemOrganization(), + response.getAutonomousSystemNumber(), + response.getAutonomousSystemOrganization(), ipAddress, - model.getIsp(), - model.getOrganization(), + response.getIsp(), + response.getOrganization(), network ); } From c1f1fb797d6d3de195f397bea6f07d778b3f447b Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 10:56:25 -0700 Subject: [PATCH 31/37] Remove database only classes --- .../model/AnonymousIpDatabaseModel.java | 62 -------- .../geoip2/model/AsnDatabaseModel.java | 26 --- .../geoip2/model/CityDatabaseModel.java | 85 ---------- .../model/ConnectionTypeDatabaseModel.java | 20 --- .../geoip2/model/CountryDatabaseModel.java | 51 ------ .../geoip2/model/DomainDatabaseModel.java | 19 --- .../geoip2/model/IspDatabaseModel.java | 40 ----- .../geoip2/record/CityDatabaseRecord.java | 35 ---- .../record/ContinentDatabaseRecord.java | 35 ---- .../geoip2/record/CountryDatabaseRecord.java | 52 ------ .../RepresentedCountryDatabaseRecord.java | 59 ------- .../record/SubdivisionDatabaseRecord.java | 42 ----- .../geoip2/record/TraitsDatabaseRecord.java | 149 ------------------ 13 files changed, 675 deletions(-) delete mode 100644 src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java delete mode 100644 src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java diff --git a/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java deleted file mode 100644 index 815c9a0e..00000000 --- a/src/main/java/com/maxmind/geoip2/model/AnonymousIpDatabaseModel.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -public class AnonymousIpDatabaseModel { - private final Boolean isAnonymous; - private final Boolean isAnonymousVpn; - private final Boolean isHostingProvider; - private final Boolean isPublicProxy; - private final Boolean isTorExitNode; - - @MaxMindDbConstructor - public AnonymousIpDatabaseModel ( - @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, - @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, - @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, - @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, - @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode - ) { - this.isAnonymous = isAnonymous; - this.isAnonymousVpn = isAnonymousVpn; - this.isHostingProvider = isHostingProvider; - this.isPublicProxy = isPublicProxy; - this.isTorExitNode = isTorExitNode; - } - - public boolean getIsAnonymous() { - if (this.isAnonymous == null) { - return false; - } - return this.isAnonymous; - } - - public boolean getIsAnonymousVpn() { - if (this.isAnonymousVpn == null) { - return false; - } - return this.isAnonymousVpn; - } - - public boolean getIsHostingProvider() { - if (this.isHostingProvider == null) { - return false; - } - return this.isHostingProvider; - } - - public boolean getIsPublicProxy() { - if (this.isPublicProxy == null) { - return false; - } - return this.isPublicProxy; - } - - public boolean getIsTorExitNode() { - if (this.isTorExitNode == null) { - return false; - } - return this.isTorExitNode; - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java deleted file mode 100644 index 9ab98a43..00000000 --- a/src/main/java/com/maxmind/geoip2/model/AsnDatabaseModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -public class AsnDatabaseModel { - private final Long autonomousSystemNumber; - private final String autonomousSystemOrganization; - - @MaxMindDbConstructor - public AsnDatabaseModel ( - @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, - @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization - ) { - this.autonomousSystemNumber = autonomousSystemNumber; - this.autonomousSystemOrganization = autonomousSystemOrganization; - } - - public Long getAutonomousSystemNumber() { - return this.autonomousSystemNumber; - } - - public String getAutonomousSystemOrganization() { - return this.autonomousSystemOrganization; - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java deleted file mode 100644 index 6674ca45..00000000 --- a/src/main/java/com/maxmind/geoip2/model/CityDatabaseModel.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; -import com.maxmind.geoip2.record.CityDatabaseRecord; -import com.maxmind.geoip2.record.ContinentDatabaseRecord; -import com.maxmind.geoip2.record.CountryDatabaseRecord; -import com.maxmind.geoip2.record.Location; -import com.maxmind.geoip2.record.Postal; -import com.maxmind.geoip2.record.RepresentedCountryDatabaseRecord; -import com.maxmind.geoip2.record.SubdivisionDatabaseRecord; -import com.maxmind.geoip2.record.TraitsDatabaseRecord; - -import java.util.ArrayList; - -public class CityDatabaseModel { - private final CityDatabaseRecord city; - private final ContinentDatabaseRecord continent; - private final CountryDatabaseRecord country; - private final Location location; - private final Postal postal; - private final CountryDatabaseRecord registeredCountry; - private final RepresentedCountryDatabaseRecord representedCountry; - private final ArrayList subdivisions; - private final TraitsDatabaseRecord traits; - - @MaxMindDbConstructor - public CityDatabaseModel( - @MaxMindDbParameter(name="city") CityDatabaseRecord city, - @MaxMindDbParameter(name="continent") ContinentDatabaseRecord continent, - @MaxMindDbParameter(name="country") CountryDatabaseRecord country, - @MaxMindDbParameter(name="location") Location location, - @MaxMindDbParameter(name="postal") Postal postal, - @MaxMindDbParameter(name="registered_country") CountryDatabaseRecord registeredCountry, - @MaxMindDbParameter(name="represented_country") RepresentedCountryDatabaseRecord representedCountry, - @MaxMindDbParameter(name="subdivisions") ArrayList subdivisions, - @MaxMindDbParameter(name="traits") TraitsDatabaseRecord traits - ) { - this.city = city; - this.continent = continent; - this.country = country; - this.location = location; - this.postal = postal; - this.registeredCountry = registeredCountry; - this.representedCountry = representedCountry; - this.subdivisions = subdivisions; - this.traits = traits; - } - - public CityDatabaseRecord getCity() { - return this.city; - } - - public ContinentDatabaseRecord getContinent() { - return this.continent; - } - - public CountryDatabaseRecord getCountry() { - return this.country; - } - - public Location getLocation() { - return this.location; - } - - public Postal getPostal() { - return this.postal; - } - - public CountryDatabaseRecord getRegisteredCountry() { - return this.registeredCountry; - } - - public RepresentedCountryDatabaseRecord getRepresentedCountry() { - return this.representedCountry; - } - - public ArrayList getSubdivisions() { - return this.subdivisions; - } - - public TraitsDatabaseRecord getTraits() { - return this.traits; - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java deleted file mode 100644 index 68ae8784..00000000 --- a/src/main/java/com/maxmind/geoip2/model/ConnectionTypeDatabaseModel.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; -import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; - -public class ConnectionTypeDatabaseModel { - private final String connectionType; - - @MaxMindDbConstructor - public ConnectionTypeDatabaseModel ( - @MaxMindDbParameter(name="connection_type") String connectionType - ) { - this.connectionType = connectionType; - } - - public ConnectionType getConnectionType() { - return ConnectionType.fromString(this.connectionType); - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java deleted file mode 100644 index 36b50fd4..00000000 --- a/src/main/java/com/maxmind/geoip2/model/CountryDatabaseModel.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; -import com.maxmind.geoip2.record.ContinentDatabaseRecord; -import com.maxmind.geoip2.record.CountryDatabaseRecord; -import com.maxmind.geoip2.record.RepresentedCountryDatabaseRecord; -import com.maxmind.geoip2.record.TraitsDatabaseRecord; - -public class CountryDatabaseModel { - private final ContinentDatabaseRecord continent; - private final CountryDatabaseRecord country; - private final CountryDatabaseRecord registeredCountry; - private final RepresentedCountryDatabaseRecord representedCountry; - private final TraitsDatabaseRecord traits; - - @MaxMindDbConstructor - public CountryDatabaseModel( - @MaxMindDbParameter(name="continent") ContinentDatabaseRecord continent, - @MaxMindDbParameter(name="country") CountryDatabaseRecord country, - @MaxMindDbParameter(name="registered_country") CountryDatabaseRecord registeredCountry, - @MaxMindDbParameter(name="represented_country") RepresentedCountryDatabaseRecord representedCountry, - @MaxMindDbParameter(name="traits") TraitsDatabaseRecord traits - ) { - this.continent = continent; - this.country = country; - this.registeredCountry = registeredCountry; - this.representedCountry = representedCountry; - this.traits = traits; - } - - public ContinentDatabaseRecord getContinent() { - return this.continent; - } - - public CountryDatabaseRecord getCountry() { - return this.country; - } - - public CountryDatabaseRecord getRegisteredCountry() { - return this.registeredCountry; - } - - public RepresentedCountryDatabaseRecord getRepresentedCountry() { - return this.representedCountry; - } - - public TraitsDatabaseRecord getTraits() { - return this.traits; - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java deleted file mode 100644 index 8300433a..00000000 --- a/src/main/java/com/maxmind/geoip2/model/DomainDatabaseModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -public class DomainDatabaseModel { - private final String domain; - - @MaxMindDbConstructor - public DomainDatabaseModel ( - @MaxMindDbParameter(name="domain") String domain - ) { - this.domain = domain; - } - - public String getDomain() { - return this.domain; - } -} diff --git a/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java b/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java deleted file mode 100644 index c05fe090..00000000 --- a/src/main/java/com/maxmind/geoip2/model/IspDatabaseModel.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.maxmind.geoip2.model; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -public class IspDatabaseModel { - private final Long autonomousSystemNumber; - private final String autonomousSystemOrganization; - private final String isp; - private final String organization; - - @MaxMindDbConstructor - public IspDatabaseModel ( - @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, - @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, - @MaxMindDbParameter(name="isp") String isp, - @MaxMindDbParameter(name="organization") String organization - ) { - this.autonomousSystemNumber = autonomousSystemNumber; - this.autonomousSystemOrganization = autonomousSystemOrganization; - this.isp = isp; - this.organization = organization; - } - - public Long getAutonomousSystemNumber() { - return this.autonomousSystemNumber; - } - - public String getAutonomousSystemOrganization() { - return this.autonomousSystemOrganization; - } - - public String getIsp() { - return this.isp; - } - - public String getOrganization() { - return this.organization; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java deleted file mode 100644 index 81cb994e..00000000 --- a/src/main/java/com/maxmind/geoip2/record/CityDatabaseRecord.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -import java.util.Map; - -public final class CityDatabaseRecord { - private final Integer confidence; - private final Long geoNameId; - private final Map names; - - @MaxMindDbConstructor - public CityDatabaseRecord( - @MaxMindDbParameter(name="confidence") Integer confidence, - @MaxMindDbParameter(name="geoname_id") Long geoNameId, - @MaxMindDbParameter(name="names") Map names - ) { - this.confidence = confidence; - this.geoNameId = geoNameId; - this.names = names; - } - - public Integer getConfidence() { - return this.confidence; - } - - public Long getGeoNameId() { - return this.geoNameId; - } - - public Map getNames() { - return this.names; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java deleted file mode 100644 index 57f4fbf5..00000000 --- a/src/main/java/com/maxmind/geoip2/record/ContinentDatabaseRecord.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -import java.util.Map; - -public final class ContinentDatabaseRecord { - private final String code; - private final Long geoNameId; - private final Map names; - - @MaxMindDbConstructor - public ContinentDatabaseRecord( - @MaxMindDbParameter(name="code") String code, - @MaxMindDbParameter(name="geoname_id") Long geoNameId, - @MaxMindDbParameter(name="names") Map names - ) { - this.code = code; - this.geoNameId = geoNameId; - this.names = names; - } - - public String getCode() { - return this.code; - } - - public Long getGeoNameId() { - return this.geoNameId; - } - - public Map getNames() { - return this.names; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java deleted file mode 100644 index ad00e0ea..00000000 --- a/src/main/java/com/maxmind/geoip2/record/CountryDatabaseRecord.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -import java.util.Map; - -public final class CountryDatabaseRecord { - private final Integer confidence; - private final Long geoNameId; - private final Boolean isInEuropeanUnion; - private final String isoCode; - private final Map names; - - @MaxMindDbConstructor - public CountryDatabaseRecord( - @MaxMindDbParameter(name="confidence") Integer confidence, - @MaxMindDbParameter(name="geoname_id") Long geoNameId, - @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, - @MaxMindDbParameter(name="iso_code") String isoCode, - @MaxMindDbParameter(name="names") Map names - ) { - this.confidence = confidence; - this.geoNameId = geoNameId; - this.isInEuropeanUnion = isInEuropeanUnion; - this.isoCode = isoCode; - this.names = names; - } - - public Integer getConfidence() { - return this.confidence; - } - - public Long getGeoNameId() { - return this.geoNameId; - } - - public boolean getIsInEuropeanUnion() { - if (this.isInEuropeanUnion == null) { - return false; - } - return this.isInEuropeanUnion; - } - - public String getIsoCode() { - return this.isoCode; - } - - public Map getNames() { - return this.names; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java deleted file mode 100644 index 2315485c..00000000 --- a/src/main/java/com/maxmind/geoip2/record/RepresentedCountryDatabaseRecord.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -import java.util.Map; - -public final class RepresentedCountryDatabaseRecord { - private final Integer confidence; - private final Long geoNameId; - private final Boolean isInEuropeanUnion; - private final String isoCode; - private final Map names; - private final String type; - - @MaxMindDbConstructor - public RepresentedCountryDatabaseRecord( - @MaxMindDbParameter(name="confidence") Integer confidence, - @MaxMindDbParameter(name="geoname_id") Long geoNameId, - @MaxMindDbParameter(name="is_in_european_union") Boolean isInEuropeanUnion, - @MaxMindDbParameter(name="iso_code") String isoCode, - @MaxMindDbParameter(name="names") Map names, - @MaxMindDbParameter(name="type") String type - ) { - this.confidence = confidence; - this.geoNameId = geoNameId; - this.isInEuropeanUnion = isInEuropeanUnion; - this.isoCode = isoCode; - this.names = names; - this.type = type; - } - - public Integer getConfidence() { - return this.confidence; - } - - public Long getGeoNameId() { - return this.geoNameId; - } - - public boolean getIsInEuropeanUnion() { - if (this.isInEuropeanUnion == null) { - return false; - } - return this.isInEuropeanUnion; - } - - public String getIsoCode() { - return this.isoCode; - } - - public Map getNames() { - return this.names; - } - - public String getType() { - return this.type; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java deleted file mode 100644 index 992729cb..00000000 --- a/src/main/java/com/maxmind/geoip2/record/SubdivisionDatabaseRecord.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -import java.util.Map; - -public final class SubdivisionDatabaseRecord { - private final Integer confidence; - private final Long geoNameId; - private final String isoCode; - private final Map names; - - @MaxMindDbConstructor - public SubdivisionDatabaseRecord( - @MaxMindDbParameter(name="confidence") Integer confidence, - @MaxMindDbParameter(name="geoname_id") Long geoNameId, - @MaxMindDbParameter(name="iso_code") String isoCode, - @MaxMindDbParameter(name="names") Map names - ) { - this.confidence = confidence; - this.geoNameId = geoNameId; - this.isoCode = isoCode; - this.names = names; - } - - public Integer getConfidence() { - return this.confidence; - } - - public Long getGeoNameId() { - return this.geoNameId; - } - - public String getIsoCode() { - return this.isoCode; - } - - public Map getNames() { - return this.names; - } -} diff --git a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java b/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java deleted file mode 100644 index 81c15bc1..00000000 --- a/src/main/java/com/maxmind/geoip2/record/TraitsDatabaseRecord.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.maxmind.geoip2.record; - -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; -import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; - -public final class TraitsDatabaseRecord { - private final Long autonomousSystemNumber; - private final String autonomousSystemOrganization; - private final String connectionType; - private final String domain; - private final Boolean isAnonymous; - private final Boolean isAnonymousProxy; - private final Boolean isAnonymousVpn; - private final Boolean isHostingProvider; - private final Boolean isLegitimateProxy; - private final Boolean isPublicProxy; - private final Boolean isSatelliteProvider; - private final Boolean isTorExitNode; - private final String isp; - private final String organization; - private final String userType; - private final Double staticIpScore; - - @MaxMindDbConstructor - public TraitsDatabaseRecord( - @MaxMindDbParameter(name="autonomous_system_number") Long autonomousSystemNumber, - @MaxMindDbParameter(name="autonomous_system_organization") String autonomousSystemOrganization, - @MaxMindDbParameter(name="connection_type") String connectionType, - @MaxMindDbParameter(name="domain") String domain, - @MaxMindDbParameter(name="is_anonymous") Boolean isAnonymous, - @MaxMindDbParameter(name="is_anonymous_proxy") Boolean isAnonymousProxy, - @MaxMindDbParameter(name="is_anonymous_vpn") Boolean isAnonymousVpn, - @MaxMindDbParameter(name="is_hosting_provider") Boolean isHostingProvider, - @MaxMindDbParameter(name="is_legitimate_proxy") Boolean isLegitimateProxy, - @MaxMindDbParameter(name="is_public_proxy") Boolean isPublicProxy, - @MaxMindDbParameter(name="is_satellite_provider") Boolean isSatelliteProvider, - @MaxMindDbParameter(name="is_tor_exit_node") Boolean isTorExitNode, - @MaxMindDbParameter(name="isp") String isp, - @MaxMindDbParameter(name="organization") String organization, - @MaxMindDbParameter(name="static_ip_score") Double staticIpScore, - @MaxMindDbParameter(name="user_type") String userType - ) { - this.autonomousSystemNumber = autonomousSystemNumber; - this.autonomousSystemOrganization = autonomousSystemOrganization; - this.connectionType = connectionType; - this.domain = domain; - this.isAnonymous = isAnonymous; - this.isAnonymousProxy = isAnonymousProxy; - this.isAnonymousVpn = isAnonymousVpn; - this.isHostingProvider = isHostingProvider; - this.isLegitimateProxy = isLegitimateProxy; - this.isPublicProxy = isPublicProxy; - this.isSatelliteProvider = isSatelliteProvider; - this.isTorExitNode = isTorExitNode; - this.isp = isp; - this.organization = organization; - this.staticIpScore = staticIpScore; - this.userType = userType; - } - - public Long getAutonomousSystemNumber() { - return this.autonomousSystemNumber; - } - - public String getAutonomousSystemOrganization() { - return this.autonomousSystemOrganization; - } - - public ConnectionType getConnectionType() { - return ConnectionType.fromString(this.connectionType); - } - - public String getDomain() { - return this.domain; - } - - public boolean isAnonymous() { - if (this.isAnonymous == null) { - return false; - } - return this.isAnonymous; - } - - public boolean isAnonymousProxy() { - if (this.isAnonymousProxy == null) { - return false; - } - return this.isAnonymousProxy; - } - - public boolean isAnonymousVpn() { - if (this.isAnonymousVpn == null) { - return false; - } - return this.isAnonymousVpn; - } - - public boolean isHostingProvider() { - if (this.isHostingProvider == null) { - return false; - } - return this.isHostingProvider; - } - - public boolean isLegitimateProxy() { - if (this.isLegitimateProxy == null) { - return false; - } - return this.isLegitimateProxy; - } - - public boolean isPublicProxy() { - if (this.isPublicProxy == null) { - return false; - } - return this.isPublicProxy; - } - - public boolean isSatelliteProvider() { - if (this.isSatelliteProvider == null) { - return false; - } - return this.isSatelliteProvider; - } - - public boolean isTorExitNode() { - if (this.isTorExitNode == null) { - return false; - } - return this.isTorExitNode; - } - - public String getIsp() { - return this.isp; - } - - public String getOrganization() { - return this.organization; - } - - public Double getStaticIpScore() { - return this.staticIpScore; - } - - public String getUserType() { - return this.userType; - } -} From 7cb0504890965eeb30410d31e7647c173d418aae Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 11:06:11 -0700 Subject: [PATCH 32/37] Share mapSubdivisions() --- .../geoip2/model/AbstractCityResponse.java | 23 +++++++++++++++++++ .../maxmind/geoip2/model/CityResponse.java | 23 ------------------- .../geoip2/model/EnterpriseResponse.java | 23 ------------------- 3 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java index 8782e3f4..d3bc868a 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java @@ -36,6 +36,29 @@ public abstract class AbstractCityResponse extends AbstractCountryResponse { this.subdivisions = subdivisions != null ? subdivisions : new ArrayList<>(); } + protected static ArrayList mapSubdivisions( + List locales, + List subdivisions + ) { + if (subdivisions == null) { + return null; + } + + ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); + for (Subdivision subdivision : subdivisions) { + subdivisions2.add( + new Subdivision( + locales, + subdivision.getConfidence(), + subdivision.getGeoNameId(), + subdivision.getIsoCode(), + subdivision.getNames() + ) + ); + } + return subdivisions2; + } + /** * @return City record for the requested IP address. */ diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index 6fb5a369..d7027a1d 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -147,27 +147,4 @@ public CityResponse( ) ); } - - private static ArrayList mapSubdivisions( - List locales, - List subdivisions - ) { - if (subdivisions == null) { - return null; - } - - ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); - for (Subdivision subdivision : subdivisions) { - subdivisions2.add( - new Subdivision( - locales, - subdivision.getConfidence(), - subdivision.getGeoNameId(), - subdivision.getIsoCode(), - subdivision.getNames() - ) - ); - } - return subdivisions2; - } } diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index 7766b7ee..51f76f6c 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -133,27 +133,4 @@ public EnterpriseResponse( ) ); } - - private static ArrayList mapSubdivisions( - List locales, - List subdivisions - ) { - if (subdivisions == null) { - return null; - } - - ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); - for (Subdivision subdivision : subdivisions) { - subdivisions2.add( - new Subdivision( - locales, - subdivision.getConfidence(), - subdivision.getGeoNameId(), - subdivision.getIsoCode(), - subdivision.getNames() - ) - ); - } - return subdivisions2; - } } From 2b662aea935c6810feee667a03fbd42fe6a1adf4 Mon Sep 17 00:00:00 2001 From: William Storey Date: Mon, 24 Aug 2020 11:34:53 -0700 Subject: [PATCH 33/37] Reduce repeated code in location constructors --- .../geoip2/model/AbstractCityResponse.java | 10 +- .../maxmind/geoip2/model/CityResponse.java | 93 ++----------------- .../maxmind/geoip2/model/CountryResponse.java | 85 ++--------------- .../geoip2/model/EnterpriseResponse.java | 93 ++----------------- .../java/com/maxmind/geoip2/record/City.java | 12 +++ .../com/maxmind/geoip2/record/Continent.java | 12 +++ .../com/maxmind/geoip2/record/Country.java | 14 +++ .../geoip2/record/RepresentedCountry.java | 15 +++ .../maxmind/geoip2/record/Subdivision.java | 13 +++ .../com/maxmind/geoip2/record/Traits.java | 28 ++++++ 10 files changed, 115 insertions(+), 260 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java index d3bc868a..54abfac2 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java @@ -46,15 +46,7 @@ protected static ArrayList mapSubdivisions( ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); for (Subdivision subdivision : subdivisions) { - subdivisions2.add( - new Subdivision( - locales, - subdivision.getConfidence(), - subdivision.getGeoNameId(), - subdivision.getIsoCode(), - subdivision.getNames() - ) - ); + subdivisions2.add(new Subdivision(subdivision, locales)); } return subdivisions2; } diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index d7027a1d..998b2c00 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -56,95 +56,16 @@ public CityResponse( List locales ) { this( - response.getCity() != null ? - new City( - locales, - response.getCity().getConfidence(), - response.getCity().getGeoNameId(), - response.getCity().getNames() - ) : null, - response.getContinent() != null ? - new Continent( - locales, - response.getContinent().getCode(), - response.getContinent().getGeoNameId(), - response.getContinent().getNames() - ) : null, - response.getCountry() != null ? - new Country( - locales, - response.getCountry().getConfidence(), - response.getCountry().getGeoNameId(), - response.getCountry().isInEuropeanUnion(), - response.getCountry().getIsoCode(), - response.getCountry().getNames() - ) : null, + response.getCity() != null ? new City(response.getCity(), locales) : null, + response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, + response.getCountry() != null ? new Country(response.getCountry(), locales) : null, response.getLocation(), - null, + response.getMaxMind(), response.getPostal(), - response.getRegisteredCountry() != null ? - new Country( - locales, - response.getRegisteredCountry().getConfidence(), - response.getRegisteredCountry().getGeoNameId(), - response.getRegisteredCountry().isInEuropeanUnion(), - response.getRegisteredCountry().getIsoCode(), - response.getRegisteredCountry().getNames() - ) : null, - response.getRepresentedCountry() != null ? - new RepresentedCountry( - locales, - response.getRepresentedCountry().getConfidence(), - response.getRepresentedCountry().getGeoNameId(), - response.getRepresentedCountry().isInEuropeanUnion(), - response.getRepresentedCountry().getIsoCode(), - response.getRepresentedCountry().getNames(), - response.getRepresentedCountry().getType() - ) : null, + response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, + response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, mapSubdivisions(locales, response.getSubdivisions()), - response.getTraits() != null ? - new Traits( - response.getTraits().getAutonomousSystemNumber(), - response.getTraits().getAutonomousSystemOrganization(), - response.getTraits().getConnectionType(), - response.getTraits().getDomain(), - ipAddress, - response.getTraits().isAnonymous(), - response.getTraits().isAnonymousProxy(), - response.getTraits().isAnonymousVpn(), - response.getTraits().isHostingProvider(), - response.getTraits().isLegitimateProxy(), - response.getTraits().isPublicProxy(), - response.getTraits().isSatelliteProvider(), - response.getTraits().isTorExitNode(), - response.getTraits().getIsp(), - network, - response.getTraits().getOrganization(), - response.getTraits().getUserType(), - null, - response.getTraits().getStaticIpScore() - ) : - new Traits( - (Integer) null, - null, - null, - null, - ipAddress, - false, - false, - false, - false, - false, - false, - false, - false, - null, - network, - null, - null, - null, - null - ) + response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null ); } } diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index 93b12b46..16f57e48 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -42,85 +42,12 @@ public CountryResponse( List locales ) { this( - response.getContinent() != null ? - new Continent( - locales, - response.getContinent().getCode(), - response.getContinent().getGeoNameId(), - response.getContinent().getNames() - ) : null, - response.getCountry() != null ? - new Country( - locales, - response.getCountry().getConfidence(), - response.getCountry().getGeoNameId(), - response.getCountry().isInEuropeanUnion(), - response.getCountry().getIsoCode(), - response.getCountry().getNames() - ) : null, - null, - response.getRegisteredCountry() != null ? - new Country( - locales, - response.getRegisteredCountry().getConfidence(), - response.getRegisteredCountry().getGeoNameId(), - response.getRegisteredCountry().isInEuropeanUnion(), - response.getRegisteredCountry().getIsoCode(), - response.getRegisteredCountry().getNames() - ) : null, - response.getRepresentedCountry() != null ? - new RepresentedCountry( - locales, - response.getRepresentedCountry().getConfidence(), - response.getRepresentedCountry().getGeoNameId(), - response.getRepresentedCountry().isInEuropeanUnion(), - response.getRepresentedCountry().getIsoCode(), - response.getRepresentedCountry().getNames(), - response.getRepresentedCountry().getType() - ) : null, - response.getTraits() != null ? - new Traits( - response.getTraits().getAutonomousSystemNumber(), - response.getTraits().getAutonomousSystemOrganization(), - response.getTraits().getConnectionType(), - response.getTraits().getDomain(), - ipAddress, - response.getTraits().isAnonymous(), - response.getTraits().isAnonymousProxy(), - response.getTraits().isAnonymousVpn(), - response.getTraits().isHostingProvider(), - response.getTraits().isLegitimateProxy(), - response.getTraits().isPublicProxy(), - response.getTraits().isSatelliteProvider(), - response.getTraits().isTorExitNode(), - response.getTraits().getIsp(), - network, - response.getTraits().getOrganization(), - response.getTraits().getUserType(), - null, - response.getTraits().getStaticIpScore() - ) : - new Traits( - (Integer) null, - null, - null, - null, - ipAddress, - false, - false, - false, - false, - false, - false, - false, - false, - null, - network, - null, - null, - null, - null - ) + response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, + response.getCountry() != null ? new Country(response.getCountry(), locales) : null, + response.getMaxMind(), + response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, + response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, + response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null ); } } diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index 51f76f6c..aff723a9 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -42,95 +42,16 @@ public EnterpriseResponse( List locales ) { this( - response.getCity() != null ? - new City( - locales, - response.getCity().getConfidence(), - response.getCity().getGeoNameId(), - response.getCity().getNames() - ) : null, - response.getContinent() != null ? - new Continent( - locales, - response.getContinent().getCode(), - response.getContinent().getGeoNameId(), - response.getContinent().getNames() - ) : null, - response.getCountry() != null ? - new Country( - locales, - response.getCountry().getConfidence(), - response.getCountry().getGeoNameId(), - response.getCountry().isInEuropeanUnion(), - response.getCountry().getIsoCode(), - response.getCountry().getNames() - ) : null, + response.getCity() != null ? new City(response.getCity(), locales) : null, + response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, + response.getCountry() != null ? new Country(response.getCountry(), locales) : null, response.getLocation(), - null, + response.getMaxMind(), response.getPostal(), - response.getRegisteredCountry() != null ? - new Country( - locales, - response.getRegisteredCountry().getConfidence(), - response.getRegisteredCountry().getGeoNameId(), - response.getRegisteredCountry().isInEuropeanUnion(), - response.getRegisteredCountry().getIsoCode(), - response.getRegisteredCountry().getNames() - ) : null, - response.getRepresentedCountry() != null ? - new RepresentedCountry( - locales, - response.getRepresentedCountry().getConfidence(), - response.getRepresentedCountry().getGeoNameId(), - response.getRepresentedCountry().isInEuropeanUnion(), - response.getRepresentedCountry().getIsoCode(), - response.getRepresentedCountry().getNames(), - response.getRepresentedCountry().getType() - ) : null, + response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, + response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, mapSubdivisions(locales, response.getSubdivisions()), - response.getTraits() != null ? - new Traits( - response.getTraits().getAutonomousSystemNumber(), - response.getTraits().getAutonomousSystemOrganization(), - response.getTraits().getConnectionType(), - response.getTraits().getDomain(), - ipAddress, - response.getTraits().isAnonymous(), - response.getTraits().isAnonymousProxy(), - response.getTraits().isAnonymousVpn(), - response.getTraits().isHostingProvider(), - response.getTraits().isLegitimateProxy(), - response.getTraits().isPublicProxy(), - response.getTraits().isSatelliteProvider(), - response.getTraits().isTorExitNode(), - response.getTraits().getIsp(), - network, - response.getTraits().getOrganization(), - response.getTraits().getUserType(), - null, - response.getTraits().getStaticIpScore() - ) : - new Traits( - (Integer) null, - null, - null, - null, - ipAddress, - false, - false, - false, - false, - false, - false, - false, - false, - null, - network, - null, - null, - null, - null - ) + response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null ); } } diff --git a/src/main/java/com/maxmind/geoip2/record/City.java b/src/main/java/com/maxmind/geoip2/record/City.java index 6fbed435..ceb4c1eb 100644 --- a/src/main/java/com/maxmind/geoip2/record/City.java +++ b/src/main/java/com/maxmind/geoip2/record/City.java @@ -53,6 +53,18 @@ public City( ); } + public City( + City city, + List locales + ) { + this( + locales, + city.getConfidence(), + city.getGeoNameId(), + city.getNames() + ); + } + /** * @return A value from 0-100 indicating MaxMind's confidence that the city * is correct. This attribute is only available from the Insights diff --git a/src/main/java/com/maxmind/geoip2/record/Continent.java b/src/main/java/com/maxmind/geoip2/record/Continent.java index 1bc2ddb2..5d207959 100644 --- a/src/main/java/com/maxmind/geoip2/record/Continent.java +++ b/src/main/java/com/maxmind/geoip2/record/Continent.java @@ -53,6 +53,18 @@ public Continent( ); } + public Continent( + Continent continent, + List locales + ) { + this( + locales, + continent.getCode(), + continent.getGeoNameId(), + continent.getNames() + ); + } + /** * @return A two character continent code like "NA" (North America) or "OC" * (Oceania). This attribute is returned by all end points. diff --git a/src/main/java/com/maxmind/geoip2/record/Country.java b/src/main/java/com/maxmind/geoip2/record/Country.java index 06a08adc..dacf09b8 100644 --- a/src/main/java/com/maxmind/geoip2/record/Country.java +++ b/src/main/java/com/maxmind/geoip2/record/Country.java @@ -75,6 +75,20 @@ public Country( ); } + public Country( + Country country, + List locales + ) { + this( + locales, + country.getConfidence(), + country.getGeoNameId(), + country.isInEuropeanUnion(), + country.getIsoCode(), + country.getNames() + ); + } + /** * @return A value from 0-100 indicating MaxMind's confidence that the * country is correct. This attribute is only available from the diff --git a/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java b/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java index 8873318e..e832a61e 100644 --- a/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java +++ b/src/main/java/com/maxmind/geoip2/record/RepresentedCountry.java @@ -78,6 +78,21 @@ public RepresentedCountry( ); } + public RepresentedCountry( + RepresentedCountry country, + List locales + ) { + this( + locales, + country.getConfidence(), + country.getGeoNameId(), + country.isInEuropeanUnion(), + country.getIsoCode(), + country.getNames(), + country.getType() + ); + } + /** * @return A string indicating the type of entity that is representing the * country. Currently we only return {@code military} but this could diff --git a/src/main/java/com/maxmind/geoip2/record/Subdivision.java b/src/main/java/com/maxmind/geoip2/record/Subdivision.java index 1fbe1aeb..2249b450 100644 --- a/src/main/java/com/maxmind/geoip2/record/Subdivision.java +++ b/src/main/java/com/maxmind/geoip2/record/Subdivision.java @@ -58,6 +58,19 @@ public Subdivision( ); } + public Subdivision( + Subdivision subdivision, + List locales + ) { + this( + locales, + subdivision.getConfidence(), + subdivision.getGeoNameId(), + subdivision.getIsoCode(), + subdivision.getNames() + ); + } + /** * @return This is a value from 0-100 indicating MaxMind's confidence that * the subdivision is correct. This attribute is only available from diff --git a/src/main/java/com/maxmind/geoip2/record/Traits.java b/src/main/java/com/maxmind/geoip2/record/Traits.java index 1a2e621b..9558ce77 100644 --- a/src/main/java/com/maxmind/geoip2/record/Traits.java +++ b/src/main/java/com/maxmind/geoip2/record/Traits.java @@ -207,6 +207,34 @@ public Traits( ); } + public Traits( + Traits traits, + String ipAddress, + Network network + ) { + this( + traits.getAutonomousSystemNumber(), + traits.getAutonomousSystemOrganization(), + traits.getConnectionType(), + traits.getDomain(), + ipAddress, + traits.isAnonymous(), + traits.isAnonymousProxy(), + traits.isAnonymousVpn(), + traits.isHostingProvider(), + traits.isLegitimateProxy(), + traits.isPublicProxy(), + traits.isSatelliteProvider(), + traits.isTorExitNode(), + traits.getIsp(), + network, + traits.getOrganization(), + traits.getUserType(), + traits.getUserCount(), + traits.getStaticIpScore() + ); + } + /** * @return The Date: Tue, 25 Aug 2020 07:22:53 -0700 Subject: [PATCH 34/37] Remove unused import --- src/main/java/com/maxmind/geoip2/model/CityResponse.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index 998b2c00..f086af37 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -7,7 +7,6 @@ import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; -import java.net.InetAddress; import java.util.ArrayList; import java.util.List; From 64e25f9b151c2e7dc5d49d75bfd38fdcdac25396 Mon Sep 17 00:00:00 2001 From: William Storey Date: Tue, 25 Aug 2020 07:23:08 -0700 Subject: [PATCH 35/37] Share city constructor logic --- .../geoip2/model/AbstractCityResponse.java | 23 ++++++++++++++++++- .../maxmind/geoip2/model/CityResponse.java | 13 +---------- .../geoip2/model/EnterpriseResponse.java | 13 +---------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java index 54abfac2..f51c4f34 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java @@ -1,6 +1,7 @@ package com.maxmind.geoip2.model; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; import java.util.ArrayList; @@ -36,7 +37,27 @@ public abstract class AbstractCityResponse extends AbstractCountryResponse { this.subdivisions = subdivisions != null ? subdivisions : new ArrayList<>(); } - protected static ArrayList mapSubdivisions( + AbstractCityResponse( + AbstractCityResponse response, + String ipAddress, + Network network, + List locales + ) { + this( + response.getCity() != null ? new City(response.getCity(), locales) : null, + response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, + response.getCountry() != null ? new Country(response.getCountry(), locales) : null, + response.getLocation(), + response.getMaxMind(), + response.getPostal(), + response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, + response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, + mapSubdivisions(locales, response.getSubdivisions()), + response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null + ); + } + + private static ArrayList mapSubdivisions( List locales, List subdivisions ) { diff --git a/src/main/java/com/maxmind/geoip2/model/CityResponse.java b/src/main/java/com/maxmind/geoip2/model/CityResponse.java index f086af37..002d3499 100644 --- a/src/main/java/com/maxmind/geoip2/model/CityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CityResponse.java @@ -54,17 +54,6 @@ public CityResponse( Network network, List locales ) { - this( - response.getCity() != null ? new City(response.getCity(), locales) : null, - response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, - response.getCountry() != null ? new Country(response.getCountry(), locales) : null, - response.getLocation(), - response.getMaxMind(), - response.getPostal(), - response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, - response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, - mapSubdivisions(locales, response.getSubdivisions()), - response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null - ); + super(response, ipAddress, network, locales); } } diff --git a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java index aff723a9..67eb21c4 100644 --- a/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/EnterpriseResponse.java @@ -41,17 +41,6 @@ public EnterpriseResponse( Network network, List locales ) { - this( - response.getCity() != null ? new City(response.getCity(), locales) : null, - response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, - response.getCountry() != null ? new Country(response.getCountry(), locales) : null, - response.getLocation(), - response.getMaxMind(), - response.getPostal(), - response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, - response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, - mapSubdivisions(locales, response.getSubdivisions()), - response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null - ); + super(response, ipAddress, network, locales); } } From 94d7d05b4e34c8be7f6b3cedd3ef80396474c276 Mon Sep 17 00:00:00 2001 From: William Storey Date: Tue, 25 Aug 2020 09:10:35 -0700 Subject: [PATCH 36/37] Share more constructor logic Also make it clearer that we create empty records when we have nulls. --- .../geoip2/model/AbstractCityResponse.java | 17 +++++------------ .../geoip2/model/AbstractCountryResponse.java | 17 +++++++++++++++++ .../maxmind/geoip2/model/CountryResponse.java | 9 +-------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java index f51c4f34..f4c38753 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java @@ -43,18 +43,11 @@ public abstract class AbstractCityResponse extends AbstractCountryResponse { Network network, List locales ) { - this( - response.getCity() != null ? new City(response.getCity(), locales) : null, - response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, - response.getCountry() != null ? new Country(response.getCountry(), locales) : null, - response.getLocation(), - response.getMaxMind(), - response.getPostal(), - response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, - response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, - mapSubdivisions(locales, response.getSubdivisions()), - response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null - ); + super(response, ipAddress, network, locales); + this.city = response.getCity() != null ? new City(response.getCity(), locales) : new City(); + this.location = response.getLocation() != null ? response.getLocation() : new Location(); + this.postal = response.getPostal() != null ? response.getPostal() : new Postal(); + this.subdivisions = response.getSubdivisions() != null ? mapSubdivisions(locales, response.getSubdivisions()) : new ArrayList<>(); } private static ArrayList mapSubdivisions( diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java index 57977f24..1c7a61c6 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java @@ -1,8 +1,11 @@ package com.maxmind.geoip2.model; import com.fasterxml.jackson.annotation.JsonProperty; +import com.maxmind.db.Network; import com.maxmind.geoip2.record.*; +import java.util.List; + public abstract class AbstractCountryResponse extends AbstractResponse { private final Continent continent; @@ -32,6 +35,20 @@ public abstract class AbstractCountryResponse extends AbstractResponse { this.traits = traits != null ? traits : new Traits(); } + AbstractCountryResponse( + AbstractCountryResponse response, + String ipAddress, + Network network, + List locales + ) { + this.continent = response.getContinent() != null ? new Continent(response.getContinent(), locales) : new Continent(); + this.country = response.getCountry() != null ? new Country(response.getCountry(), locales) : new Country(); + this.maxmind = response.getMaxMind() != null ? response.getMaxMind() : new MaxMind(); + this.registeredCountry = response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : new Country(); + this.representedCountry = response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : new RepresentedCountry(); + this.traits = response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : new Traits(); + } + /** * @return MaxMind record containing data related to your account. */ diff --git a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java index 16f57e48..3b43042f 100644 --- a/src/main/java/com/maxmind/geoip2/model/CountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/CountryResponse.java @@ -41,13 +41,6 @@ public CountryResponse( Network network, List locales ) { - this( - response.getContinent() != null ? new Continent(response.getContinent(), locales) : null, - response.getCountry() != null ? new Country(response.getCountry(), locales) : null, - response.getMaxMind(), - response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : null, - response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : null, - response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : null - ); + super(response, ipAddress, network, locales); } } From 77aa07d9623d9118adc342acab06d78f8f75e391 Mon Sep 17 00:00:00 2001 From: William Storey Date: Tue, 25 Aug 2020 09:42:12 -0700 Subject: [PATCH 37/37] Do not handle fields being null if they never will be --- .../geoip2/model/AbstractCityResponse.java | 18 ++++++++---------- .../geoip2/model/AbstractCountryResponse.java | 14 ++++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java index f4c38753..8ea76544 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java @@ -44,20 +44,18 @@ public abstract class AbstractCityResponse extends AbstractCountryResponse { List locales ) { super(response, ipAddress, network, locales); - this.city = response.getCity() != null ? new City(response.getCity(), locales) : new City(); - this.location = response.getLocation() != null ? response.getLocation() : new Location(); - this.postal = response.getPostal() != null ? response.getPostal() : new Postal(); - this.subdivisions = response.getSubdivisions() != null ? mapSubdivisions(locales, response.getSubdivisions()) : new ArrayList<>(); + // The response fields will be non-null because of the above + // constructor used during deserializing. + this.city = new City(response.getCity(), locales); + this.location = response.getLocation(); + this.postal = response.getPostal(); + this.subdivisions = mapSubdivisions(response.getSubdivisions(), locales); } private static ArrayList mapSubdivisions( - List locales, - List subdivisions + List subdivisions, + List locales ) { - if (subdivisions == null) { - return null; - } - ArrayList subdivisions2 = new ArrayList<>(subdivisions.size()); for (Subdivision subdivision : subdivisions) { subdivisions2.add(new Subdivision(subdivision, locales)); diff --git a/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java b/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java index 1c7a61c6..727740b0 100644 --- a/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java +++ b/src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java @@ -41,12 +41,14 @@ public abstract class AbstractCountryResponse extends AbstractResponse { Network network, List locales ) { - this.continent = response.getContinent() != null ? new Continent(response.getContinent(), locales) : new Continent(); - this.country = response.getCountry() != null ? new Country(response.getCountry(), locales) : new Country(); - this.maxmind = response.getMaxMind() != null ? response.getMaxMind() : new MaxMind(); - this.registeredCountry = response.getRegisteredCountry() != null ? new Country(response.getRegisteredCountry(), locales) : new Country(); - this.representedCountry = response.getRepresentedCountry() != null ? new RepresentedCountry(response.getRepresentedCountry(), locales) : new RepresentedCountry(); - this.traits = response.getTraits() != null ? new Traits(response.getTraits(), ipAddress, network) : new Traits(); + // The response fields will be non-null because of the above + // constructor used during deserializing. + this.continent = new Continent(response.getContinent(), locales); + this.country = new Country(response.getCountry(), locales); + this.maxmind = response.getMaxMind(); + this.registeredCountry = new Country(response.getRegisteredCountry(), locales); + this.representedCountry = new RepresentedCountry(response.getRepresentedCountry(), locales); + this.traits = new Traits(response.getTraits(), ipAddress, network); } /**