Search in sources :

Example 1 with CountryResponse

use of com.maxmind.geoip2.model.CountryResponse 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 2 with CountryResponse

use of com.maxmind.geoip2.model.CountryResponse 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)2 CountryResponse (com.maxmind.geoip2.model.CountryResponse)2 CityResponse (com.maxmind.geoip2.model.CityResponse)1 Continent (com.maxmind.geoip2.record.Continent)1 Country (com.maxmind.geoip2.record.Country)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 ConfigurationUtils.newConfigurationException (org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException)1 ConfigurationUtils.readBooleanProperty (org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty)1 ConfigurationUtils.readStringProperty (org.elasticsearch.ingest.ConfigurationUtils.readStringProperty)1