use of com.maxmind.geoip2.exception.AddressNotFoundException in project elasticsearch by elastic.
the class GeoIpProcessor method retrieveCityGeoData.
private Map<String, Object> retrieveCityGeoData(InetAddress ipAddress) {
SpecialPermission.check();
CityResponse response = AccessController.doPrivileged((PrivilegedAction<CityResponse>) () -> {
try {
return dbReader.city(ipAddress);
} catch (AddressNotFoundException e) {
throw new AddressNotFoundRuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Country country = response.getCountry();
City city = response.getCity();
Location location = response.getLocation();
Continent continent = response.getContinent();
Subdivision subdivision = response.getMostSpecificSubdivision();
Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
switch(property) {
case IP:
geoData.put("ip", NetworkAddress.format(ipAddress));
break;
case COUNTRY_ISO_CODE:
String countryIsoCode = country.getIsoCode();
if (countryIsoCode != null) {
geoData.put("country_iso_code", countryIsoCode);
}
break;
case COUNTRY_NAME:
String countryName = country.getName();
if (countryName != null) {
geoData.put("country_name", countryName);
}
break;
case CONTINENT_NAME:
String continentName = continent.getName();
if (continentName != null) {
geoData.put("continent_name", continentName);
}
break;
case REGION_NAME:
String subdivisionName = subdivision.getName();
if (subdivisionName != null) {
geoData.put("region_name", subdivisionName);
}
break;
case CITY_NAME:
String cityName = city.getName();
if (cityName != null) {
geoData.put("city_name", cityName);
}
break;
case TIMEZONE:
String locationTimeZone = location.getTimeZone();
if (locationTimeZone != null) {
geoData.put("timezone", locationTimeZone);
}
break;
case LOCATION:
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
if (latitude != null && longitude != null) {
Map<String, Object> locationObject = new HashMap<>();
locationObject.put("lat", latitude);
locationObject.put("lon", longitude);
geoData.put("location", locationObject);
}
break;
}
}
return geoData;
}
use of com.maxmind.geoip2.exception.AddressNotFoundException in project cas by apereo.
the class MaxmindDatabaseGeoLocationService method locate.
@Override
public GeoLocationResponse locate(final InetAddress address) {
try {
final GeoLocationResponse location = new GeoLocationResponse();
if (this.cityDatabaseReader != null) {
final CityResponse response = this.cityDatabaseReader.city(address);
location.addAddress(response.getCity().getName());
location.setLatitude(response.getLocation().getLatitude());
location.setLongitude(response.getLocation().getLongitude());
}
if (this.countryDatabaseReader != null) {
final CountryResponse response = this.countryDatabaseReader.country(address);
location.addAddress(response.getCountry().getName());
}
LOGGER.debug("Geo location for [{}] is calculated as [{}]", address, location);
return location;
} catch (final AddressNotFoundException e) {
LOGGER.info(e.getMessage(), e);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
use of com.maxmind.geoip2.exception.AddressNotFoundException in project elasticsearch by elastic.
the class GeoIpProcessor method retrieveCountryGeoData.
private Map<String, Object> retrieveCountryGeoData(InetAddress ipAddress) {
SpecialPermission.check();
CountryResponse response = AccessController.doPrivileged((PrivilegedAction<CountryResponse>) () -> {
try {
return dbReader.country(ipAddress);
} catch (AddressNotFoundException e) {
throw new AddressNotFoundRuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Country country = response.getCountry();
Continent continent = response.getContinent();
Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
switch(property) {
case IP:
geoData.put("ip", NetworkAddress.format(ipAddress));
break;
case COUNTRY_ISO_CODE:
String countryIsoCode = country.getIsoCode();
if (countryIsoCode != null) {
geoData.put("country_iso_code", countryIsoCode);
}
break;
case COUNTRY_NAME:
String countryName = country.getName();
if (countryName != null) {
geoData.put("country_name", countryName);
}
break;
case CONTINENT_NAME:
String continentName = continent.getName();
if (continentName != null) {
geoData.put("continent_name", continentName);
}
break;
}
}
return geoData;
}
Aggregations