use of com.maxmind.geoip2.record.RepresentedCountry 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.RepresentedCountry in project cas by apereo.
the class MaxmindDatabaseGeoLocationServiceTests method verifyOperation.
@Test
public void verifyOperation() throws Exception {
val city = mock(DatabaseReader.class);
val cityResponse = new CityResponse(new City(), new Continent(), new Country(), new Location(), new MaxMind(), new Postal(), new Country(), new RepresentedCountry(), new ArrayList<>(), new Traits());
when(city.city(any(InetAddress.class))).thenReturn(cityResponse);
val country = mock(DatabaseReader.class);
val countryResponse = new CountryResponse(new Continent(), new Country(), new MaxMind(), new Country(), new RepresentedCountry(), new Traits());
when(country.country(any(InetAddress.class))).thenReturn(countryResponse);
val service = new MaxmindDatabaseGeoLocationService(city, country);
val response = service.locate("127.0.0.1");
assertNotNull(response);
val response2 = service.locate(100D, 100D);
assertNull(response2);
}
use of com.maxmind.geoip2.record.RepresentedCountry in project cas by apereo.
the class MaxmindDatabaseGeoLocationServiceTests method verifyCity.
@Test
public void verifyCity() throws Exception {
val cityReader = mock(DatabaseReader.class);
val cityResponse = new CityResponse(new City(), new Continent(), new Country(), new Location(10, 100, 40D, 70D, 1, 1, "UTC"), new MaxMind(), new Postal(), new Country(), new RepresentedCountry(), new ArrayList<>(), new Traits());
when(cityReader.city(any())).thenReturn(cityResponse);
val service = new MaxmindDatabaseGeoLocationService(cityReader, null);
val response = service.locate("127.0.0.1");
assertNotNull(response);
}
use of com.maxmind.geoip2.record.RepresentedCountry 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;
}
use of com.maxmind.geoip2.record.RepresentedCountry in project nutch by apache.
the class GeoIPDocumentCreator method createDocFromInsightsService.
public static NutchDocument createDocFromInsightsService(String serverIp, NutchDocument doc, WebServiceClient client) throws UnknownHostException, IOException, GeoIp2Exception {
addIfNotNull(doc, "ip", serverIp);
InsightsResponse response = client.insights(InetAddress.getByName(serverIp));
// CityResponse response = client.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());
Traits traits = response.getTraits();
addIfNotNull(doc, "autonSystemNum", traits.getAutonomousSystemNumber());
addIfNotNull(doc, "autonSystemOrg", traits.getAutonomousSystemOrganization());
addIfNotNull(doc, "domain", traits.getDomain());
addIfNotNull(doc, "isp", traits.getIsp());
addIfNotNull(doc, "org", traits.getOrganization());
addIfNotNull(doc, "userType", traits.getUserType());
// for better results, users should upgrade to
// https://www.maxmind.com/en/solutions/geoip2-enterprise-product-suite/anonymous-ip-database
addIfNotNull(doc, "isAnonProxy", String.valueOf(traits.isAnonymousProxy()));
return doc;
}
Aggregations