private DatabaseReader loadReader(File file) throws IOException { return new DatabaseReader.Builder(file).build(); }
@Override protected void doStop() throws Exception { final DatabaseReader databaseReader = this.databaseReader.get(); if (databaseReader != null) { databaseReader.close(); } }
private DatabaseReader openGeoIpDb(File geoDb) { try { DatabaseReader reader = new DatabaseReader(geoDb); LOG.info("Using geo ip database at [%s].", geoDb); return reader; } catch (IOException e) { throw new RuntimeException("Could not open geo db at [" + geoDb.getAbsolutePath() + "].", e); } }
try { java.io.InputStream inputFile = GeoLocationImpl.class.getClassLoader().getResourceAsStream("reference/GeoLite2-City.mmdb"); reader = new DatabaseReader.Builder(inputFile).build(); } catch(Exception e) { LOGGER.error("Cannot instantiate IP database",e); CityResponse response = reader.city(InetAddress.getByName(ipAddress)); address.setCountry(response.getCountry().getIsoCode()); address.setPostalCode(response.getPostal().getCode()); address.setZone(response.getMostSpecificSubdivision().getIsoCode()); address.setCity(response.getCity().getName()); LOGGER.debug("Address not fount in DB " + ne.getMessage()); } catch(Exception e) { throw new ServiceException(e);
case MAXMIND_CITY: try { final CityResponse city = reader.city(addr); if (city == null) { LOG.debug("No city data for IP address {}, returning empty result.", addr); final Location location = city.getLocation(); final Map<Object, Object> map = new HashMap<>(); map.put("city", city.getCity()); map.put("continent", city.getContinent()); map.put("country", city.getCountry()); map.put("location", location); map.put("postal", city.getPostal()); map.put("registered_country", city.getRegisteredCountry()); map.put("represented_country", city.getRepresentedCountry()); map.put("subdivisions", city.getSubdivisions()); map.put("traits", city.getTraits()); if (location == null || location.getLatitude() == null || location.getLongitude() == null) { singleValue = null; } else { singleValue = location.getLatitude() + "," + location.getLongitude(); map.put("coordinates", singleValue); final CountryResponse countryResponse = reader.country(addr); if (countryResponse == null) { LOG.debug("No country data for IP address {}, returning empty result.", addr); final Country country = countryResponse.getCountry();
@VisibleForTesting Optional<GeoLocationInformation> extractGeoLocationInformation(Object fieldValue) { final InetAddress ipAddress; if (fieldValue instanceof InetAddress) { ipAddress = (InetAddress) fieldValue; } else if (fieldValue instanceof String) { ipAddress = getIpFromFieldValue((String) fieldValue); } else { ipAddress = null; } GeoLocationInformation geoLocationInformation = null; if (ipAddress != null) { try (Timer.Context ignored = resolveTime.time()) { final CityResponse response = databaseReader.city(ipAddress); final Location location = response.getLocation(); final Country country = response.getCountry(); final City city = response.getCity(); geoLocationInformation = GeoLocationInformation.create( location.getLatitude(), location.getLongitude(), country.getGeoNameId() != null ? country.getIsoCode() : "N/A", city.getGeoNameId() != null ? city.getName() : "N/A" // calling to .getName() may throw a NPE ); } catch (Exception e) { LOG.debug("Could not get location from IP {}", ipAddress.getHostAddress(), e); } } return Optional.ofNullable(geoLocationInformation); }
city = cityLookup.city(address); } catch (AddressNotFoundException e) { return; switch (attribute) { case "country_code": put(properties,"_country_code", city.getCountry().getIsoCode()); break; case "region": put(properties,"_region", city.getContinent().getName()); break; case "city": put(properties,"_city", city.getCity().getName()); break; case "latitude": put(properties,"_latitude", city.getLocation().getLatitude()); break; case "longitude": put(properties,"_longitude", city.getLocation().getLongitude()); break; case "timezone": put(properties,"_timezone", city.getLocation().getTimeZone()); break;
/** * 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; }
/** * Reads a new Database from a given HDFS file * @param reader The DatabaseReader to use to read the file * @param hdfsFile The HDFS file to read * @param is An InputStream for use with the reader * @return The DatabaseReader that is set up with the new file * @throws IOException If there is an issue reading the file. */ public static DatabaseReader readNewDatabase(DatabaseReader reader, String hdfsFile, InputStream is) throws IOException { LOG.info("Update to GeoIP data started with {}", hdfsFile); // InputStream based DatabaseReaders are always in memory. DatabaseReader newReader = new DatabaseReader.Builder(is).withCache(new CHMCache()).build(); // If we've never set a reader, don't close the old one if (reader != null) { reader.close(); } LOG.info("Finished update to GeoIP data started with {}", hdfsFile); return newReader; }
private DatabaseReader getReader(URL url) { try { FileInputStream cityDatabase = new FileInputStream(MaxmindGeoIPModule.downloadOrGetFile(url)); return new DatabaseReader.Builder(cityDatabase).fileMode(MEMORY).build(); } catch (Exception e) { throw new RuntimeException(e); } }
try { final InetAddress ip = InetAddress.getByName(ipMatch.group()); final Omni lookup = geoLookup.omni(ip); dimensions.put("continent", lookup.getContinent().getName()); dimensions.put("country", lookup.getCountry().getName()); dimensions.put("region", lookup.getMostSpecificSubdivision().getName()); dimensions.put("city", lookup.getCity().getName());
private void setIsp(InetAddress address, GenericRecord properties) { IspResponse isp; try { isp = ispLookup.isp(address); } catch (AddressNotFoundException e) { return; } catch (Exception e) { LOGGER.error(e, "Error while searching for location information."); return; } put(properties,"_isp", isp.getIsp()); }
private void setConnectionType(InetAddress address, GenericRecord properties) { ConnectionTypeResponse connectionType; try { connectionType = connectionTypeLookup.connectionType(address); } catch (AddressNotFoundException e) { return; } catch (Exception e) { LOGGER.error(e, "Error while searching for location information."); return; } ConnectionTypeResponse.ConnectionType connType = connectionType.getConnectionType(); if (connType != null) { put(properties,"_connection_type", connType.name()); } }
String type) throws IOException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2]
public String country(String ipAddress) throws IOException { InetAddress ia = InetAddress.getByName(ipAddress); Object o = get(ia, "Country"); if (!(o instanceof Map)) return null; Map m = (Map) o; o = m.get("country"); if (!(o instanceof Map)) return null; m = (Map) o; o = m.get("iso_code"); if (!(o instanceof String)) return null; return (String) o; }
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); } }
public GeoIpResolverEngine(GeoIpResolverConfig config, MetricRegistry metricRegistry) { this.resolveTime = metricRegistry.timer(name(GeoIpResolverEngine.class, "resolveTime")); try { final File database = new File(config.dbPath()); if (Files.exists(database.toPath())) { this.databaseReader = new DatabaseReader.Builder(database).build(); this.enabled = config.enabled(); } else { LOG.warn("GeoIP database file does not exist: {}", config.dbPath()); this.enabled = false; } } catch (IOException e) { LOG.error("Could not open GeoIP database {}", config.dbPath(), e); this.enabled = false; } }
/** * @return an instance of {@code DatabaseReader} created from the * fields set on this builder. * @throws IOException if there is an error reading the database */ public DatabaseReader build() throws IOException { return new DatabaseReader(this); } }
@Override protected void doRefresh(LookupCachePurge cachePurge) throws Exception { try { final FileInfo.Change databaseFileCheck = fileInfo.checkForChange(); if (!databaseFileCheck.isChanged() && !getError().isPresent()) { return; } // file has different attributes, let's reload it LOG.debug("MaxMind database file has changed, reloading it from {}", config.path()); final DatabaseReader oldReader = this.databaseReader.get(); try { this.databaseReader.set(loadReader(Paths.get(config.path()).toFile())); cachePurge.purgeAll(); if (oldReader != null) { oldReader.close(); } fileInfo = databaseFileCheck.fileInfo(); clearError(); } catch (IOException e) { LOG.warn("Unable to load changed database file, leaving old one intact. Error message: {}", e.getMessage()); setError(e); } } catch (IllegalArgumentException iae) { LOG.error("Unable to refresh MaxMind database file: {}", iae.getMessage()); setError(iae); } }
@Override public void afterPropertiesSet() throws Exception { File database = new File(geoDBFilePath); reader = new DatabaseReader.Builder(database).build(); }