public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: DatabaseReader geoip2-file.mmdb ip"); System.exit(1); } File f = new File(args[0]); Builder b = new Builder(f); b.withCache(new CHMCache(256)); DatabaseReader r = b.build(); System.out.println("Database Metadata: " + r.getMetadata()); String c = r.country(args[1]); System.out.println("IP: " + args[1] + " country: " + c); } }
String type) throws IOException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2]
/** * Open a GeoIP2 database * @since 0.9.38 */ private DatabaseReader openGeoIP2(File geoFile) throws IOException { DatabaseReader.Builder b = new DatabaseReader.Builder(geoFile); b.withCache(new CHMCache(256)); DatabaseReader rv = b.build(); if (_log.shouldDebug()) _log.debug("Opened GeoIP2 Database, Metadata: " + rv.getMetadata()); return rv; }
/** * @param ipAddress IPv4 or IPv6 address to lookup. * @return A <T> 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> T get(InetAddress ipAddress, Class<T> cls, String type) throws IOException, AddressNotFoundException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2] .getMethodName(); throw new UnsupportedOperationException( "Invalid attempt to open a " + databaseType + " database using the " + caller + " method"); } ObjectNode node = jsonNodeToObjectNode(reader.get(ipAddress)); // We throw the same exception as the web service when an IP is not in // the database if (node == null) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); } InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress()); return this.om.reader(inject).treeToValue(node, cls); }
@Override public void initDatabase(LocationDatabaseType type, String pathToDatabase) { Objects.requireNonNull(type); Objects.requireNonNull(pathToDatabase); LOG.info("Init {} as type {} with file {}", getClass().toString(), type.toString(), pathToDatabase); dbType = type; try { File f = new File(pathToDatabase); reader = new DatabaseReader.Builder(f).fileMode(FileMode.MEMORY_MAPPED).build(); // mismatch if (!type.getGeoLite2Name().equals(reader.getMetadata().getDatabaseType())) { LOG.error("DatabaseType {} not match with the databasefile {}. Exiting.", type.toString(), pathToDatabase); destroy(); } } catch (Throwable e) { LOG.error("Couldn't initation geolocation database ", e); reader = null; } }
/** * @param ipAddress IPv4 or IPv6 address to lookup. * @return A <T> 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> T get(InetAddress ipAddress, Class<T> cls, String type) throws IOException, AddressNotFoundException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2] .getMethodName(); throw new UnsupportedOperationException( "Invalid attempt to open a " + databaseType + " database using the " + caller + " method"); } ObjectNode node = jsonNodeToObjectNode(reader.get(ipAddress)); // We throw the same exception as the web service when an IP is not in // the database if (node == null) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); } InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress()); return this.om.reader(inject).treeToValue(node, cls); }
switch(reader.getMetadata().getDatabaseType()) { case "GeoIP2-City": case "GeoLite2-City": { throw event.buildException("Unknown database type: " + reader.getMetadata().getDatabaseType());