use of com.maxmind.geoip2.record.Postal in project tutorials by eugenp.
the class GeoIpIntegrationTest method givenIP_whenFetchingCity_thenReturnsCityData.
@Test
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
File database = new File("your-path-to-db-file");
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("your-public-ip");
CityResponse response = dbReader.city(ipAddress);
String countryName = response.getCountry().getName();
String cityName = response.getCity().getName();
String postal = response.getPostal().getCode();
String state = response.getLeastSpecificSubdivision().getName();
}
use of com.maxmind.geoip2.record.Postal 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.Postal 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.Postal in project bender by Nextdoor.
the class GeoIpOperation method perform.
@Override
public InternalEvent perform(InternalEvent ievent) {
String ipStr = null;
/*
* Get field containing an IP address
*/
try {
ipStr = ievent.getEventObj().getFieldAsString(this.pathToIpAddress);
} catch (FieldNotFoundException e) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " does not exist");
}
if (ipStr == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " was null");
}
/*
* Sometimes the field contains comma separated ip addresses (ie forwarded web requests). In
* this case pick the first value in the list which is typically the user.
*/
if (!ipStr.isEmpty() && ipStr.contains(",")) {
ipStr = ipStr.split(",")[0];
}
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(ipStr);
} catch (UnknownHostException e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
if (ipAddress == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address " + ipStr + " did not resolve");
}
CityResponse response = null;
try {
response = this.databaseReader.city(ipAddress);
} catch (IOException | GeoIp2Exception e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
HashMap<String, Object> geo = new HashMap<String, Object>(1);
for (GeoProperty property : this.geoProperties) {
switch(property) {
case COUNTRY_NAME:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_name", response.getCountry().getName());
break;
case COUNTRY_ISO_CODE:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_iso_code", response.getCountry().getIsoCode());
break;
case SUBDIVISION_NAME:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_name", response.getMostSpecificSubdivision().getName());
break;
case SUBDIVISION_ISO_CODE:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_iso_code", response.getMostSpecificSubdivision().getIsoCode());
break;
case CITY_NAME:
if (response.getCity() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("city returned null");
}
geo.put("city_name", response.getCity().getName());
break;
case POSTAL_CODE:
if (response.getPostal() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("postal returned null");
}
geo.put("postal_code", response.getPostal().getCode());
break;
case LOCATION:
if (response.getLocation() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("location returned null");
}
Double lat = response.getLocation().getLatitude();
Double lon = response.getLocation().getLongitude();
if (lat == null || lon == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("error getting lat/lon");
}
HashMap<String, Object> location = new HashMap<String, Object>(2);
location.put("lat", lat);
location.put("lon", lon);
geo.put("location", location);
break;
}
}
try {
ievent.getEventObj().setField(this.destFieldName, geo);
} catch (FieldNotFoundException e) {
throw new OperationException(e);
}
return ievent;
}
use of com.maxmind.geoip2.record.Postal 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);
}
Aggregations