Search in sources :

Example 1 with AddressNotFoundException

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;
}
Also used : HashMap(java.util.HashMap) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) ConfigurationUtils.newConfigurationException(org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) ConfigurationUtils.readStringProperty(org.elasticsearch.ingest.ConfigurationUtils.readStringProperty) ConfigurationUtils.readBooleanProperty(org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty) Location(com.maxmind.geoip2.record.Location)

Example 2 with AddressNotFoundException

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;
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) CountryResponse(com.maxmind.geoip2.model.CountryResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 3 with AddressNotFoundException

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;
}
Also used : Continent(com.maxmind.geoip2.record.Continent) HashMap(java.util.HashMap) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) ConfigurationUtils.readStringProperty(org.elasticsearch.ingest.ConfigurationUtils.readStringProperty) ConfigurationUtils.readBooleanProperty(org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty) ConfigurationUtils.newConfigurationException(org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Aggregations

AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)3 CityResponse (com.maxmind.geoip2.model.CityResponse)2 CountryResponse (com.maxmind.geoip2.model.CountryResponse)2 Continent (com.maxmind.geoip2.record.Continent)2 Country (com.maxmind.geoip2.record.Country)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 ConfigurationUtils.newConfigurationException (org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException)2 ConfigurationUtils.readBooleanProperty (org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty)2 ConfigurationUtils.readStringProperty (org.elasticsearch.ingest.ConfigurationUtils.readStringProperty)2 City (com.maxmind.geoip2.record.City)1 Location (com.maxmind.geoip2.record.Location)1 Subdivision (com.maxmind.geoip2.record.Subdivision)1 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)1