use of com.maxmind.geoip2.model.CityResponse 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.model.CityResponse 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.model.CityResponse in project spring-session by spring-projects.
the class SessionDetailsFilter method getGeoLocation.
// end::dofilterinternal[]
String getGeoLocation(String remoteAddr) {
try {
CityResponse city = this.reader.city(InetAddress.getByName(remoteAddr));
String cityName = city.getCity().getName();
String countryName = city.getCountry().getName();
if (cityName == null && countryName == null) {
return null;
} else if (cityName == null) {
return countryName;
} else if (countryName == null) {
return cityName;
}
return cityName + ", " + countryName;
} catch (Exception e) {
return UNKNOWN;
}
}
use of com.maxmind.geoip2.model.CityResponse 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.model.CityResponse in project graylog2-server by Graylog2.
the class MaxMindIpLocationResolver method doGetGeoIpData.
@Override
public Optional<GeoLocationInformation> doGetGeoIpData(InetAddress address) {
GeoLocationInformation info;
try (Timer.Context ignored = resolveTime.time()) {
final CityResponse response = databaseReader.city(address);
final Location location = response.getLocation();
final Country country = response.getCountry();
final City city = response.getCity();
info = GeoLocationInformation.create(location.getLatitude(), location.getLongitude(), country.getGeoNameId() == null ? "N/A" : country.getIsoCode(), country.getGeoNameId() == null ? "N/A" : country.getName(), // calling to .getName() may throw a NPE
city.getGeoNameId() == null ? "N/A" : city.getName(), "N/A", "N/A");
} catch (IOException | GeoIp2Exception | UnsupportedOperationException e) {
info = null;
if (!(e instanceof AddressNotFoundException)) {
LOG.debug("Could not get location from IP {}", address.getHostAddress(), e);
lastError = e.getMessage();
}
}
return Optional.ofNullable(info);
}
Aggregations