Search in sources :

Example 1 with RepresentedCountry

use of com.maxmind.geoip2.record.RepresentedCountry 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;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) HashMap(java.util.HashMap) Map(java.util.Map) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location)

Example 2 with RepresentedCountry

use of com.maxmind.geoip2.record.RepresentedCountry 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 3 with RepresentedCountry

use of com.maxmind.geoip2.record.RepresentedCountry 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 4 with RepresentedCountry

use of com.maxmind.geoip2.record.RepresentedCountry in project nutch by apache.

the class GeoIPDocumentCreator method createDocFromCityDb.

public static NutchDocument createDocFromCityDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
    addIfNotNull(doc, "ip", serverIp);
    CityResponse response = reader.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());
    return doc;
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location)

Example 5 with RepresentedCountry

use of com.maxmind.geoip2.record.RepresentedCountry 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)

Aggregations

City (com.maxmind.geoip2.record.City)5 Continent (com.maxmind.geoip2.record.Continent)5 Country (com.maxmind.geoip2.record.Country)5 Location (com.maxmind.geoip2.record.Location)5 Postal (com.maxmind.geoip2.record.Postal)5 CityResponse (com.maxmind.geoip2.model.CityResponse)4 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 Subdivision (com.maxmind.geoip2.record.Subdivision)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 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)1 InsightsResponse (com.maxmind.geoip2.model.InsightsResponse)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1