diff --git a/router/java/src/com/maxmind/geoip2/DatabaseReader.java b/router/java/src/com/maxmind/geoip2/DatabaseReader.java new file mode 100644 index 0000000000..c3b97cca50 --- /dev/null +++ b/router/java/src/com/maxmind/geoip2/DatabaseReader.java @@ -0,0 +1,329 @@ +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; +import com.maxmind.geoip2.exception.GeoIp2Exception; +import com.maxmind.geoip2.model.*; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.util.Collections; +import java.util.List; + +/** + *
+ * The class {@code DatabaseReader} provides a reader for the GeoIP2 database + * format. + *
+ *+ * To use the database API, you must create a new {@code DatabaseReader} using + * the {@code DatabaseReader.Builder}. You must provide the {@code Builder} + * constructor either an {@code InputStream} or {@code File} for your GeoIP2 + * database. You may also specify the {@code fileMode} and the {@code locales} + * fallback order using the methods on the {@code Builder} object. After you + * have created the {@code DatabaseReader}, you may then call the appropriate + * method (e.g., {@code city}) for your database, passing it the IP address + * you want to look up. + *
+ *+ * If the lookup succeeds, the method call will return a response class for + * the GeoIP2 lookup. The class in turn contains multiple record classes, + * each of which represents part of the data returned by the database. + *
+ *+ * We recommend reusing the {@code DatabaseReader} object rather than creating + * a new one for each lookup. The creation of this object is relatively + * expensive as it must read in metadata for the file. It is safe to share the + * object across threads. + *
+ *+ * The database API supports pluggable caching (by default, no caching is + * performed). A simple implementation is provided by + * {@code com.maxmind.db.CHMCache}. Using this cache, lookup performance is + * significantly improved at the cost of a small (~2MB) memory overhead. + *
+ */ +public class DatabaseReader implements DatabaseProvider, Closeable { + + private final Reader reader; + + private final ObjectMapper om; + + private final List+ * Constructs a Builder for the {@code DatabaseReader}. The file passed to + * it must be a valid GeoIP2 database file. + *
+ *+ * {@code Builder} creates instances of {@code DatabaseReader} + * from values set by the methods. + *
+ *+ * Only the values set in the {@code Builder} constructor are required. + *
+ */ + public static final class Builder { + final File database; + final InputStream stream; + + List+ * Closes the database. + *
+ *+ * If you are using {@code FileMode.MEMORY_MAPPED}, this will + * not unmap the underlying file due to a limitation in Java's + * {@code MappedByteBuffer}. It will however set the reference to + * the buffer to {@code null}, allowing the garbage collector to + * collect it. + *
+ * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + this.reader.close(); + } + + @Override + public CountryResponse country(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, CountryResponse.class, "Country"); + } + + @Override + public CityResponse city(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, CityResponse.class, "City"); + } + + /** + * Look up an IP address in a GeoIP2 Anonymous IP. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return a AnonymousIpResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, AnonymousIpResponse.class, "GeoIP2-Anonymous-IP"); + } + + /** + * Look up an IP address in a GeoLite2 ASN database. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return an AsnResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public AsnResponse asn(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, AsnResponse.class, "GeoLite2-ASN"); + } + + /** + * Look up an IP address in a GeoIP2 Connection Type database. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return a ConnectTypeResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public ConnectionTypeResponse connectionType(InetAddress ipAddress) + throws IOException, GeoIp2Exception { + return this.get(ipAddress, ConnectionTypeResponse.class, + "GeoIP2-Connection-Type"); + } + + /** + * Look up an IP address in a GeoIP2 Domain database. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return a DomainResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public DomainResponse domain(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this + .get(ipAddress, DomainResponse.class, "GeoIP2-Domain"); + } + + /** + * Look up an IP address in a GeoIP2 Enterprise database. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return an EnterpriseResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, EnterpriseResponse.class, "Enterprise"); + } + + + /** + * Look up an IP address in a GeoIP2 ISP database. + * + * @param ipAddress IPv4 or IPv6 address to lookup. + * @return an IspResponse for the requested IP address. + * @throws GeoIp2Exception if there is an error looking up the IP + * @throws IOException if there is an IO error + */ + @Override + public IspResponse isp(InetAddress ipAddress) throws IOException, + GeoIp2Exception { + return this.get(ipAddress, IspResponse.class, "GeoIP2-ISP"); + } + + /** + * @return the metadata for the open MaxMind DB file. + */ + public Metadata getMetadata() { + return this.reader.getMetadata(); + } +}