final CountryResponse countryResponse = reader.country(addr); if (countryResponse == null) { LOG.debug("No country data for IP address {}, returning empty result.", addr);
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); } }
.withCache(new CHMCache()).build()) { CountryResponse response = reader.country(InetAddress.getByName(rp.getRemoteAddr())); Country country = response.getCountry(); rp.setRemoteAddrLocale(new Locale("", country.getIsoCode()));
public void dissect(final Parsable<?> parsable, final String inputname, final InetAddress ipAddress) throws DissectionFailure { CountryResponse response; try { response = reader.country(ipAddress); } catch (IOException | GeoIp2Exception e) { return; } extractCountryFields(parsable, inputname, response); }
public Optional<Country> getCountry(String ipAddress) { if (reader != null) { try { CountryResponse country = reader.country(InetAddress.getByName(ipAddress)); if (country != null) return Optional.of(country.getCountry()); } catch (AddressNotFoundException | UnknownHostException ex) { LOGGER.debug("Error geo-locating country.", ex); } catch (Exception ex) { LOGGER.error("Error geo-locating country.", ex); } } return Optional.empty(); } }
/** * Retrieves the country in full length (e.g. United States) from the IP Address. * <p> * This product includes GeoLite2 data created by MaxMind, available from * <a href="http://www.maxmind.com">http://www.maxmind.com</a>. * * @param ipAddress The IP Address from which the country is retrieved * @return The name of the country in full length. * <p> * An exception from that rule is when the country is unknown or the retrieval of the country failed in any way, * if that happens, "Not Known" will be returned. * @see <a href="http://maxmind.com">http://maxmind.com</a> * @see #getCountry(String) */ private String getUnCachedCountry(String ipAddress) { if ("127.0.0.1".equals(ipAddress)) { return "Local Machine"; } try { checkDB(); try (DatabaseReader reader = new DatabaseReader.Builder(geolocationDB).build()) { InetAddress inetAddress = InetAddress.getByName(ipAddress); CountryResponse response = reader.country(inetAddress); Country country = response.getCountry(); return country.getName(); } } catch (IOException | GeoIp2Exception e) { return "Not Known"; } }
CountryResponse response = reader.country(ipInfo); if(response == null) { throw event.buildException("Country not found for " + ipInfo.toString());
private Map<String, Object> ip2SpatialData(InetAddress ip) { if (!enabled) { return null; } if (reader == null) { LOG.warn("Location database is not initialized. Exiting."); return null; } try { Map<String, Object> holder = new HashMap<>(3); if (dbType == LocationDatabaseType.COUNTRY) { Country country = reader.country(ip).getCountry(); holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), country.getIsoCode()); } else { CityResponse city = reader.city(ip); Location loc = city.getLocation(); holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), city.getCountry().getIsoCode()); holder.put(ObjectEsParameterFactory.GEOLOC_CITY_NAME.getName(), city.getCity().getName()); holder.put(ObjectEsParameterFactory.GEOLOC_GEO_POINT.getName(), new GeoPoint(loc.getLatitude(), loc.getLongitude())); } return holder; } catch (Throwable e) { LOG.warn("Can't convert IP to GeoIp", e); } return null; }