use of com.maxmind.geoip2.record.Subdivision 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.record.Subdivision in project nifi by apache.
the class GeoEnrichIP method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final DatabaseReader dbReader = databaseReaderRef.get();
final String ipAttributeName = context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
final String ipAttributeValue = flowFile.getAttribute(ipAttributeName);
if (StringUtils.isEmpty(ipAttributeName)) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("FlowFile '{}' attribute '{}' was empty. Routing to failure", new Object[] { flowFile, IP_ADDRESS_ATTRIBUTE.getDisplayName() });
return;
}
InetAddress inetAddress = null;
CityResponse response = null;
try {
inetAddress = InetAddress.getByName(ipAttributeValue);
} catch (final IOException ioe) {
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Could not resolve the IP for value '{}', contained within the attribute '{}' in " + "FlowFile '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the processor with an invalid IP address ", new Object[] { ipAttributeValue, IP_ADDRESS_ATTRIBUTE.getDisplayName(), flowFile }, ioe);
return;
}
final StopWatch stopWatch = new StopWatch(true);
try {
response = dbReader.city(inetAddress);
stopWatch.stop();
} catch (final IOException | GeoIp2Exception ex) {
// Note IOException is captured again as dbReader also makes InetAddress.getByName() calls.
// Most name or IP resolutions failure should have been triggered in the try loop above but
// environmental conditions may trigger errors during the second resolution as well.
session.transfer(flowFile, REL_NOT_FOUND);
getLogger().warn("Failure while trying to find enrichment data for {} due to {}", new Object[] { flowFile, ex }, ex);
return;
}
if (response == null) {
session.transfer(flowFile, REL_NOT_FOUND);
return;
}
final Map<String, String> attrs = new HashMap<>();
attrs.put(new StringBuilder(ipAttributeName).append(".geo.lookup.micros").toString(), String.valueOf(stopWatch.getDuration(TimeUnit.MICROSECONDS)));
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));
}
int i = 0;
for (final Subdivision subd : response.getSubdivisions()) {
attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.").append(i).toString(), subd.getName());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.isocode.").append(i).toString(), subd.getIsoCode());
i++;
}
attrs.put(new StringBuilder(ipAttributeName).append(".geo.country").toString(), response.getCountry().getName());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.country.isocode").toString(), response.getCountry().getIsoCode());
attrs.put(new StringBuilder(ipAttributeName).append(".geo.postalcode").toString(), response.getPostal().getCode());
flowFile = session.putAllAttributes(flowFile, attrs);
session.transfer(flowFile, REL_FOUND);
}
use of com.maxmind.geoip2.record.Subdivision in project LogHub by fbacchella.
the class Geoip2 method processMessage.
@Override
public boolean processMessage(Event event, String field, String destination) throws ProcessorException {
Object addr = event.get(field);
InetAddress ipInfo = null;
if (addr instanceof InetAddress) {
ipInfo = (InetAddress) addr;
} else if (addr instanceof String) {
try {
ipInfo = Helpers.parseIpAddres((String) addr);
if (ipInfo == null) {
throw event.buildException("can't read ip address " + addr);
}
} catch (UnknownHostException e) {
throw event.buildException("can't read ip address " + addr, e);
}
}
Country country = null;
Country registred_country = null;
Country represented_country = null;
City city = null;
Continent continent = null;
Location location = null;
Postal postal = null;
List<Subdivision> subdivision = null;
Map<String, Object> informations = new HashMap<>();
try {
switch(reader.getMetadata().getDatabaseType()) {
case "GeoIP2-City":
case "GeoLite2-City":
{
CityResponse response = reader.city(ipInfo);
if (response == null) {
throw event.buildException("City not found for " + ipInfo.toString());
}
country = response.getCountry();
city = response.getCity();
continent = response.getContinent();
location = response.getLocation();
postal = response.getPostal();
registred_country = response.getRegisteredCountry();
represented_country = response.getRepresentedCountry();
subdivision = response.getSubdivisions();
break;
}
case "GeoIP2-Country":
case "GeoLite2-Country":
{
CountryResponse response = reader.country(ipInfo);
if (response == null) {
throw event.buildException("Country not found for " + ipInfo.toString());
}
country = response.getCountry();
continent = response.getContinent();
registred_country = response.getRegisteredCountry();
represented_country = response.getRepresentedCountry();
break;
}
}
} catch (AddressNotFoundException e) {
// not an error, just return a failure
return false;
} catch (IOException | GeoIp2Exception e) {
throw event.buildException("can't read geoip database", e);
}
for (LocationType type : types) {
switch(type) {
case COUNTRY:
if (country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", country.getIsoCode());
Helpers.putNotEmpty(infos, "name", country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("country", infos);
}
}
break;
case REPRESENTEDCOUNTRY:
if (represented_country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", represented_country.getIsoCode());
Helpers.putNotEmpty(infos, "name", represented_country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("represented_country", infos);
}
}
break;
case REGISTREDCOUNTRY:
if (registred_country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", registred_country.getIsoCode());
Helpers.putNotEmpty(infos, "name", registred_country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("registred_country", infos);
}
}
break;
case CITY:
{
if (city != null) {
Helpers.putNotEmpty(informations, "city", city.getNames().get(locale));
}
break;
}
case LOCATION:
Map<String, Object> infos = new HashMap<>(10);
if (location != null) {
Helpers.putNotEmpty(infos, "latitude", location.getLatitude());
Helpers.putNotEmpty(infos, "longitude", location.getLongitude());
Helpers.putNotEmpty(infos, "timezone", location.getTimeZone());
Helpers.putNotEmpty(infos, "accuray_radius", location.getAccuracyRadius());
Helpers.putNotEmpty(infos, "metro_code", location.getMetroCode());
Helpers.putNotEmpty(infos, "average_income", location.getAverageIncome());
Helpers.putNotEmpty(infos, "population_density", location.getPopulationDensity());
if (infos.size() > 0) {
informations.put("location", infos);
}
}
case CONTINENT:
if (continent != null) {
Helpers.putNotEmpty(informations, "continent", continent.getNames().get(locale));
}
break;
case POSTAL:
if (postal != null) {
Helpers.putNotEmpty(informations, "postal", postal.getCode());
}
break;
case SUBDIVISION:
if (subdivision != null) {
List<Map<String, Object>> all = new ArrayList<>(subdivision.size());
for (Subdivision sub : subdivision) {
Map<String, Object> subdivisioninfo = new HashMap<>(2);
Helpers.putNotEmpty(subdivisioninfo, "code", sub.getIsoCode());
Helpers.putNotEmpty(subdivisioninfo, "name", sub.getNames().get(locale));
if (subdivisioninfo.size() > 0) {
all.add(subdivisioninfo);
}
}
if (all.size() > 0) {
informations.put("subdivisions", all);
}
}
break;
}
}
event.put(destination, informations);
return true;
}
use of com.maxmind.geoip2.record.Subdivision in project nifi by apache.
the class IPLookupService method createRecord.
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);
}
use of com.maxmind.geoip2.record.Subdivision in project nutch by apache.
the class GeoIPDocumentCreator method createDocFromCityDb.
public static NutchDocument createDocFromCityDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
addIfNotNull(doc, "ip", serverIp);
CityResponse response = reader.city(InetAddress.getByName(serverIp));
City city = response.getCity();
// 'Minneapolis'
addIfNotNull(doc, "cityName", city.getName());
// 50
addIfNotNull(doc, "cityConfidence", city.getConfidence());
addIfNotNull(doc, "cityGeoNameId", city.getGeoNameId());
Continent continent = response.getContinent();
addIfNotNull(doc, "continentCode", continent.getCode());
addIfNotNull(doc, "continentGeoNameId", continent.getGeoNameId());
addIfNotNull(doc, "continentName", continent.getName());
Country country = response.getCountry();
// 'US'
addIfNotNull(doc, "countryIsoCode", country.getIsoCode());
// 'United States'
addIfNotNull(doc, "countryName", country.getName());
// 99
addIfNotNull(doc, "countryConfidence", country.getConfidence());
addIfNotNull(doc, "countryGeoName", country.getGeoNameId());
Location location = response.getLocation();
// 44.9733,
addIfNotNull(doc, "latLon", location.getLatitude() + "," + location.getLongitude());
// -93.2323
// 3
addIfNotNull(doc, "accRadius", location.getAccuracyRadius());
// 'America/Chicago'
addIfNotNull(doc, "timeZone", location.getTimeZone());
addIfNotNull(doc, "metroCode", location.getMetroCode());
Postal postal = response.getPostal();
// '55455'
addIfNotNull(doc, "postalCode", postal.getCode());
// 40
addIfNotNull(doc, "postalConfidence", postal.getConfidence());
RepresentedCountry rCountry = response.getRepresentedCountry();
addIfNotNull(doc, "countryType", rCountry.getType());
Subdivision subdivision = response.getMostSpecificSubdivision();
// 'Minnesota'
addIfNotNull(doc, "subDivName", subdivision.getName());
// 'MN'
addIfNotNull(doc, "subDivIdoCode", subdivision.getIsoCode());
// 90
addIfNotNull(doc, "subDivConfidence", subdivision.getConfidence());
addIfNotNull(doc, "subDivGeoNameId", subdivision.getGeoNameId());
return doc;
}
Aggregations