Changeset 21ca75d for router


Ignore:
Timestamp:
Nov 20, 2018 11:05:46 AM (19 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f1297e7c
Parents:
3923db0
Message:

Modify GeoIP2-java
to remove the dependency on the large com.fasterxml.jackson.databind JSON package,
and use POJOs instead.
Add main() for testing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/com/maxmind/geoip2/DatabaseReader.java

    r3923db0 r21ca75d  
    11package com.maxmind.geoip2;
    22
    3 import com.fasterxml.jackson.databind.*;
    4 import com.fasterxml.jackson.databind.node.ObjectNode;
    53import com.maxmind.db.*;
    64import com.maxmind.db.Reader.FileMode;
    7 import com.maxmind.geoip2.exception.AddressNotFoundException;
    8 import com.maxmind.geoip2.exception.GeoIp2Exception;
    9 import com.maxmind.geoip2.model.*;
    105
    116import java.io.Closeable;
     
    1611import java.util.Collections;
    1712import java.util.List;
     13import java.util.Map;
    1814
    1915/**
     16 * Modified and simplified for I2P
     17 *
    2018 * <p>
    2119 * The class {@code DatabaseReader} provides a reader for the GeoIP2 database
     
    5149 * significantly improved at the cost of a small (~2MB) memory overhead.
    5250 * </p>
     51 *
     52 * @since 0.9.38
    5353 */
    54 public class DatabaseReader implements DatabaseProvider, Closeable {
     54public class DatabaseReader implements Closeable {
    5555
    5656    private final Reader reader;
    57 
    58     private final ObjectMapper om;
    5957
    6058    private final List<String> locales;
     
    7169                    "Unsupported Builder configuration: expected either File or URL");
    7270        }
    73         this.om = new ObjectMapper();
    74         this.om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, false);
    75         this.om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
    76                 false);
    77         this.om.configure(
    78                 DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    7971        this.locales = builder.locales;
    8072    }
     
    163155
    164156    /**
     157     * Returns a map containing:
     158     *
     159     *<ul><li>continent: Map containing:
     160     *  <ul><li>code: String
     161     *      <li>names: Map of lang to translated name
     162     *      <li>geoname_id: Long
     163     *  </ul>
     164     *<ul><li>country: Map containing:
     165     *  <ul><li>iso_code: String
     166     *      <li>names: Map of lang to translated name
     167     *      <li>geoname_id: Long
     168     *  </ul>
     169     *<ul><li>registered_country: Map containing:
     170     *  <ul><li>iso_code: String
     171     *      <li>names: Map of lang to translated name
     172     *      <li>geoname_id: Long
     173     *  </ul>
     174     *</ul>
     175     *
    165176     * @param ipAddress IPv4 or IPv6 address to lookup.
    166      * @return A <T> object with the data for the IP address
     177     * @return A Map with the data for the IP address
    167178     * @throws IOException              if there is an error opening or reading from the file.
    168      * @throws AddressNotFoundException if the IP address is not in our database
    169      */
    170     private <T> T get(InetAddress ipAddress, Class<T> cls,
    171                       String type) throws IOException, AddressNotFoundException {
     179     */
     180    private Object get(InetAddress ipAddress,
     181                      String type) throws IOException {
    172182
    173183        String databaseType = this.getMetadata().getDatabaseType();
     
    180190        }
    181191
    182         ObjectNode node = jsonNodeToObjectNode(reader.get(ipAddress));
    183 
    184         // We throw the same exception as the web service when an IP is not in
    185         // the database
    186         if (node == null) {
    187             throw new AddressNotFoundException("The address "
    188                     + ipAddress.getHostAddress() + " is not in the database.");
    189         }
    190 
    191         InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress());
    192 
    193         return this.om.reader(inject).treeToValue(node, cls);
    194     }
    195 
    196     private ObjectNode jsonNodeToObjectNode(JsonNode node)
    197             throws InvalidDatabaseException {
    198         if (node == null || node instanceof ObjectNode) {
    199             return (ObjectNode) node;
    200         }
    201         throw new InvalidDatabaseException(
    202                 "Unexpected data type returned. The GeoIP2 database may be corrupt.");
     192        return reader.get(ipAddress);
    203193    }
    204194
     
    222212    }
    223213
    224     @Override
    225     public CountryResponse country(InetAddress ipAddress) throws IOException,
    226             GeoIp2Exception {
    227         return this.get(ipAddress, CountryResponse.class, "Country");
    228     }
    229 
    230     @Override
    231     public CityResponse city(InetAddress ipAddress) throws IOException,
    232             GeoIp2Exception {
    233         return this.get(ipAddress, CityResponse.class, "City");
    234     }
    235 
    236     /**
    237      * Look up an IP address in a GeoIP2 Anonymous IP.
    238      *
    239      * @param ipAddress IPv4 or IPv6 address to lookup.
    240      * @return a AnonymousIpResponse for the requested IP address.
    241      * @throws GeoIp2Exception if there is an error looking up the IP
    242      * @throws IOException     if there is an IO error
    243      */
    244     @Override
    245     public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException,
    246             GeoIp2Exception {
    247         return this.get(ipAddress, AnonymousIpResponse.class, "GeoIP2-Anonymous-IP");
    248     }
    249 
    250     /**
    251      * Look up an IP address in a GeoLite2 ASN database.
    252      *
    253      * @param ipAddress IPv4 or IPv6 address to lookup.
    254      * @return an AsnResponse for the requested IP address.
    255      * @throws GeoIp2Exception if there is an error looking up the IP
    256      * @throws IOException     if there is an IO error
    257      */
    258     @Override
    259     public AsnResponse asn(InetAddress ipAddress) throws IOException,
    260             GeoIp2Exception {
    261         return this.get(ipAddress, AsnResponse.class, "GeoLite2-ASN");
    262     }
    263 
    264     /**
    265      * Look up an IP address in a GeoIP2 Connection Type database.
    266      *
    267      * @param ipAddress IPv4 or IPv6 address to lookup.
    268      * @return a ConnectTypeResponse for the requested IP address.
    269      * @throws GeoIp2Exception if there is an error looking up the IP
    270      * @throws IOException     if there is an IO error
    271      */
    272     @Override
    273     public ConnectionTypeResponse connectionType(InetAddress ipAddress)
    274             throws IOException, GeoIp2Exception {
    275         return this.get(ipAddress, ConnectionTypeResponse.class,
    276                 "GeoIP2-Connection-Type");
    277     }
    278 
    279     /**
    280      * Look up an IP address in a GeoIP2 Domain database.
    281      *
    282      * @param ipAddress IPv4 or IPv6 address to lookup.
    283      * @return a DomainResponse for the requested IP address.
    284      * @throws GeoIp2Exception if there is an error looking up the IP
    285      * @throws IOException     if there is an IO error
    286      */
    287     @Override
    288     public DomainResponse domain(InetAddress ipAddress) throws IOException,
    289             GeoIp2Exception {
    290         return this
    291                 .get(ipAddress, DomainResponse.class, "GeoIP2-Domain");
    292     }
    293 
    294     /**
    295      * Look up an IP address in a GeoIP2 Enterprise database.
    296      *
    297      * @param ipAddress IPv4 or IPv6 address to lookup.
    298      * @return an EnterpriseResponse for the requested IP address.
    299      * @throws GeoIp2Exception if there is an error looking up the IP
    300      * @throws IOException     if there is an IO error
    301      */
    302     @Override
    303     public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException,
    304             GeoIp2Exception {
    305         return this.get(ipAddress, EnterpriseResponse.class, "Enterprise");
    306     }
    307 
    308 
    309     /**
    310      * Look up an IP address in a GeoIP2 ISP database.
    311      *
    312      * @param ipAddress IPv4 or IPv6 address to lookup.
    313      * @return an IspResponse for the requested IP address.
    314      * @throws GeoIp2Exception if there is an error looking up the IP
    315      * @throws IOException     if there is an IO error
    316      */
    317     @Override
    318     public IspResponse isp(InetAddress ipAddress) throws IOException,
    319             GeoIp2Exception {
    320         return this.get(ipAddress, IspResponse.class, "GeoIP2-ISP");
     214    public String country(String ipAddress) throws IOException {
     215        InetAddress ia = InetAddress.getByName(ipAddress);
     216        Object o = get(ia, "Country");
     217        if (!(o instanceof Map))
     218            return null;
     219        Map m = (Map) o;
     220        o = m.get("country");
     221        if (!(o instanceof Map))
     222            return null;
     223        m = (Map) o;
     224        o = m.get("iso_code");
     225        if (!(o instanceof String))
     226            return null;
     227        return (String) o;
     228
    321229    }
    322230
     
    327235        return this.reader.getMetadata();
    328236    }
     237
     238    public static void main(String[] args) throws Exception {
     239        if (args.length != 2) {
     240            System.err.println("Usage: DatabaseReader geoip2-file.mmdb ip");
     241            System.exit(1);
     242        }
     243        File f = new File(args[0]);
     244        Builder b = new Builder(f);
     245        b.withCache(new CHMCache(256));
     246        DatabaseReader r = b.build();
     247        System.out.println("Database Metadata: " + r.getMetadata());
     248        String c = r.country(args[1]);
     249        System.out.println("IP: " + args[1] + " country: " + c);
     250    }
    329251}
Note: See TracChangeset for help on using the changeset viewer.