Search in sources :

Example 1 with Traits

use of com.maxmind.geoip2.record.Traits 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);
}
Also used : lombok.val(lombok.val) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) City(com.maxmind.geoip2.record.City) Traits(com.maxmind.geoip2.record.Traits) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) MaxMind(com.maxmind.geoip2.record.MaxMind) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location) Test(org.junit.jupiter.api.Test)

Example 2 with Traits

use of com.maxmind.geoip2.record.Traits 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);
}
Also used : lombok.val(lombok.val) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) MaxMind(com.maxmind.geoip2.record.MaxMind) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) Postal(com.maxmind.geoip2.record.Postal) Traits(com.maxmind.geoip2.record.Traits) Location(com.maxmind.geoip2.record.Location) Test(org.junit.jupiter.api.Test)

Example 3 with Traits

use of com.maxmind.geoip2.record.Traits 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;
}
Also used : Continent(com.maxmind.geoip2.record.Continent) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) InsightsResponse(com.maxmind.geoip2.model.InsightsResponse) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) Postal(com.maxmind.geoip2.record.Postal) Traits(com.maxmind.geoip2.record.Traits) Location(com.maxmind.geoip2.record.Location)

Example 4 with Traits

use of com.maxmind.geoip2.record.Traits in project graylog2-server by Graylog2.

the class MaxmindDataAdapter method doGet.

@Override
protected LookupResult doGet(Object key) {
    final InetAddress addr;
    if (key instanceof InetAddress) {
        addr = (InetAddress) key;
    } else {
        // try to convert it somehow
        try {
            addr = InetAddresses.forString(key.toString());
        } catch (IllegalArgumentException e) {
            LOG.warn("Unable to parse IP address, returning empty result.");
            return LookupResult.empty();
        }
    }
    final IPLocationDatabaseAdapter reader = this.databaseAdapter.get();
    switch(config.dbType()) {
        case MAXMIND_CITY:
            try {
                final CityResponse city = reader.maxMindCity(addr);
                if (city == null) {
                    LOG.debug("No city data for IP address {}, returning empty result.", addr);
                    return LookupResult.empty();
                }
                final Location location = city.getLocation();
                final Map<Object, Object> map = new HashMap<>();
                map.put("city", city.getCity());
                map.put("continent", city.getContinent());
                map.put("country", city.getCountry());
                map.put("location", location);
                map.put("postal", city.getPostal());
                map.put("registered_country", city.getRegisteredCountry());
                map.put("represented_country", city.getRepresentedCountry());
                map.put("subdivisions", city.getSubdivisions());
                map.put("traits", city.getTraits());
                final String singleValue;
                if (location == null || location.getLatitude() == null || location.getLongitude() == null) {
                    singleValue = null;
                } else {
                    singleValue = location.getLatitude() + "," + location.getLongitude();
                    map.put("coordinates", singleValue);
                }
                return LookupResult.multi(singleValue, map);
            } catch (AddressNotFoundException nfe) {
                LOG.debug("Unable to look up city data for IP address {}, returning empty result.", addr, nfe);
                return LookupResult.empty();
            } catch (Exception e) {
                LOG.warn("Unable to look up city data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            }
        case MAXMIND_COUNTRY:
            try {
                final CountryResponse countryResponse = reader.maxMindCountry(addr);
                if (countryResponse == null) {
                    LOG.debug("No country data for IP address {}, returning empty result.", addr);
                    return LookupResult.empty();
                }
                final Country country = countryResponse.getCountry();
                final Map<Object, Object> map = new HashMap<>();
                map.put("continent", countryResponse.getContinent());
                map.put("country", country);
                map.put("registered_country", countryResponse.getRegisteredCountry());
                map.put("represented_country", countryResponse.getRepresentedCountry());
                map.put("traits", countryResponse.getTraits());
                final String singleValue = country == null ? null : country.getIsoCode();
                return LookupResult.multi(singleValue, map);
            } catch (AddressNotFoundException nfe) {
                LOG.debug("Unable to look up country data for IP address {}, returning empty result.", addr, nfe);
                return LookupResult.empty();
            } catch (Exception e) {
                LOG.warn("Unable to look up country data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            }
        case MAXMIND_ASN:
            try {
                final AsnResponse asn = reader.maxMindASN(addr);
                if (asn == null) {
                    LOG.debug("No ASN data for IP address {}, returning empty result.", addr);
                    return LookupResult.empty();
                }
                final ImmutableMap<Object, Object> map = ImmutableMap.of("as_number", asn.getAutonomousSystemNumber(), "as_organization", asn.getAutonomousSystemOrganization());
                return LookupResult.multi(asn.getAutonomousSystemNumber(), map);
            } catch (AddressNotFoundException nfe) {
                LOG.debug("Unable to look up ASN data for IP address {}, returning empty result.", addr, nfe);
                return LookupResult.empty();
            } catch (Exception e) {
                LOG.warn("Unable to look up ASN data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            }
        case IPINFO_ASN:
            try {
                final IPinfoASN ipInfo = reader.ipInfoASN(addr);
                if (ipInfo == null) {
                    LOG.debug("No IPinfo location data for IP address {}, returning empty result.", addr);
                    return LookupResult.empty();
                }
                // Values can be null so we cannot use an ImmutableMap here
                final Map<Object, Object> multiValue = new HashMap<>();
                multiValue.put("asn", ipInfo.asn());
                multiValue.put("asn_numeric", ipInfo.asnNumeric());
                multiValue.put("name", ipInfo.name());
                multiValue.put("domain", ipInfo.domain());
                multiValue.put("type", ipInfo.type());
                multiValue.put("route", ipInfo.route());
                return LookupResult.multi(ipInfo.asnNumeric(), Collections.unmodifiableMap(multiValue));
            } catch (AddressNotFoundException e) {
                LOG.debug("Unable to look up IPinfo ASN data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            } catch (Exception e) {
                LOG.warn("Unable to look up IPinfo ASN data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            }
        case IPINFO_STANDARD_LOCATION:
            try {
                final IPinfoStandardLocation ipInfo = reader.ipInfoStandardLocation(addr);
                if (ipInfo == null) {
                    LOG.debug("No IPinfo location data for IP address {}, returning empty result.", addr);
                    return LookupResult.empty();
                }
                // Values can be null so we cannot use an ImmutableMap here
                final Map<Object, Object> multiValue = new HashMap<>();
                multiValue.put("coordinates", ipInfo.coordinates());
                multiValue.put("latitude", ipInfo.latitude());
                multiValue.put("longitude", ipInfo.longitude());
                multiValue.put("city", ipInfo.city());
                multiValue.put("country", ipInfo.country());
                multiValue.put("region", ipInfo.region());
                multiValue.put("timezone", ipInfo.timezone());
                multiValue.put("geoname_id", ipInfo.geoNameId());
                return LookupResult.multi(ipInfo.coordinates(), Collections.unmodifiableMap(multiValue));
            } catch (AddressNotFoundException e) {
                LOG.debug("Unable to look up IPinfo location data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            } catch (Exception e) {
                LOG.warn("Unable to look up IPinfo location data for IP address {}, returning empty result.", addr, e);
                return LookupResult.empty();
            }
    }
    return LookupResult.empty();
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) CityResponse(com.maxmind.geoip2.model.CityResponse) AsnResponse(com.maxmind.geoip2.model.AsnResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) Location(com.maxmind.geoip2.record.Location)

Aggregations

Country (com.maxmind.geoip2.record.Country)4 Location (com.maxmind.geoip2.record.Location)4 CityResponse (com.maxmind.geoip2.model.CityResponse)3 City (com.maxmind.geoip2.record.City)3 Continent (com.maxmind.geoip2.record.Continent)3 Postal (com.maxmind.geoip2.record.Postal)3 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)3 Traits (com.maxmind.geoip2.record.Traits)3 CountryResponse (com.maxmind.geoip2.model.CountryResponse)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2 InetAddress (java.net.InetAddress)2 lombok.val (lombok.val)2 Test (org.junit.jupiter.api.Test)2 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)1 AsnResponse (com.maxmind.geoip2.model.AsnResponse)1 InsightsResponse (com.maxmind.geoip2.model.InsightsResponse)1 Subdivision (com.maxmind.geoip2.record.Subdivision)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1