@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); }
attrs.put(new StringBuilder(ipAttributeName).append(".geo.city").toString(), response.getCity().getName()); final Double latitude = response.getLocation().getLatitude(); if (latitude != null) { attrs.put(new StringBuilder(ipAttributeName).append(".geo.latitude").toString(), latitude.toString()); final Double longitude = response.getLocation().getLongitude(); if (longitude != null) { attrs.put(new StringBuilder(ipAttributeName).append(".geo.longitude").toString(), longitude.toString()); final Integer accuracy = response.getLocation().getAccuracyRadius(); if (accuracy != null) { attrs.put(new StringBuilder(ipAttributeName).append(".accuracy").toString(), String.valueOf(accuracy));
final Location location = city.getLocation(); final Map<Object, Object> map = new HashMap<>(); map.put("city", city.getCity());
private Record createRecord(final CityResponse city) { if (city == null) { return null; } final Map<String, Object> values = new HashMap<>(); values.put(CitySchema.CITY.getFieldName(), city.getCity().getName()); final Location location = city.getLocation(); values.put(CitySchema.ACCURACY.getFieldName(), location.getAccuracyRadius()); values.put(CitySchema.METRO_CODE.getFieldName(), location.getMetroCode()); values.put(CitySchema.TIMEZONE.getFieldName(), location.getTimeZone()); values.put(CitySchema.LATITUDE.getFieldName(), location.getLatitude()); values.put(CitySchema.LONGITUDE.getFieldName(), location.getLongitude()); values.put(CitySchema.CONTINENT.getFieldName(), city.getContinent().getName()); values.put(CitySchema.POSTALCODE.getFieldName(), city.getPostal().getCode()); values.put(CitySchema.COUNTRY.getFieldName(), createRecord(city.getCountry())); final Object[] subdivisions = new Object[city.getSubdivisions().size()]; int i = 0; for (final Subdivision subdivision : city.getSubdivisions()) { subdivisions[i++] = createRecord(subdivision); } values.put(CitySchema.SUBDIVISIONS.getFieldName(), subdivisions); return new MapRecord(CitySchema.GEO_SCHEMA, values); }
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;
City city = cityResponse.getCity(); Postal postal = cityResponse.getPostal(); Location location = cityResponse.getLocation();
@Override public Object getValue(CityResponse response) { return response.getLocation().getTimeZone(); } },
@Override public Object getValue(CityResponse response) { Double longitude = response.getLocation().getLongitude(); Double latitude = response.getLocation().getLatitude(); if (longitude == null || latitude == null) { return null; } return Arrays.asList(longitude, latitude); } },
@Override public Double getValue(CityResponse response) { return response.getLocation().getLatitude(); } },
@Override public Object getValue(CityResponse response) { return response.getLocation().getLongitude(); } },
@Override public Object getValue(CityResponse response) { return response.getLocation().getMetroCode(); } };
private GeoInfo _getGeoInfo(String ipAddress) { GeoInfo geoInfo = new GeoInfo(); if (ipAddress == null) { return geoInfo; } try{ InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); Country country = response.getCountry(); Continent continent = response.getContinent(); City city = response.getCity(); Postal postal = response.getPostal(); Location location = response.getLocation(); Subdivision subdivision = response.getMostSpecificSubdivision(); geoInfo.setRegion(subdivision.getName()); geoInfo.setCountryIsoCode(country.getIsoCode()); geoInfo.setCountry(country.getName()); geoInfo.setContinent(continent.getCode()); geoInfo.setCity(city.getName()); geoInfo.setPostalCode(postal.getCode()); geoInfo.setLatitude(location.getLatitude()); geoInfo.setLongitude(location.getLongitude()); return geoInfo; } catch(Exception ex){ return null; } }
InetAddress clientIp = InetAddress.getByName(ipString); final Location clientLocation = GEO_IP_DATABASE.city(clientIp).getLocation();
@Override public GeoLocation readLocation(InetAddress address) { if(reader == null) { initializeDataset(); } GeoLocation geoLocation = null; try { if(reader != null) { Location location = reader.city(address).getLocation(); geoLocation = new GeoLocation(location.getLatitude(), location.getLongitude()); } } catch(IOException | GeoIp2Exception e) { if(logger != null) { logger.warn("Couldn't locate lat/long for address", e); } } return geoLocation; }
attrs.put(new StringBuilder(ipAttributeName).append(".geo.city").toString(), response.getCity().getName()); final Double latitude = response.getLocation().getLatitude(); if (latitude != null) { attrs.put(new StringBuilder(ipAttributeName).append(".geo.latitude").toString(), latitude.toString()); final Double longitude = response.getLocation().getLongitude(); if (longitude != null) { attrs.put(new StringBuilder(ipAttributeName).append(".geo.longitude").toString(), longitude.toString());
HostGeoInfo geo = new HostGeoInfo(address.getHostName(), name.toString(), l.getLocation().getLatitude(), l.getLocation().getLongitude()); log.debug("Geo info lookup (MaxMind DB) for "+address+" returned: "+geo); return geo;
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; }
if (cityResponse.getLocation().getLatitude() != null && cityResponse.getLocation().getLongitude() != null) { locationMap.put("lat", cityResponse.getLocation().getLatitude()); locationMap.put("lon", cityResponse.getLocation().getLongitude()); session.setProperty("location", locationMap);
access.setMessage(null); access.setLl(new Double[] { cr.getLocation().getLatitude(), cr.getLocation().getLongitude() });
CityResponse location = locationService.city(ipAddress); String countryCode = location.getCountry().getIsoCode(); double latitude = location.getLocation().getLatitude(); double longitude = location.getLocation().getLongitude(); if (!( "--".equals(countryCode)