Search in sources :

Example 6 with CityResponse

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();
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) DatabaseReader(com.maxmind.geoip2.DatabaseReader) File(java.io.File) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 7 with CityResponse

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;
}
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 8 with CityResponse

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;
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 9 with CityResponse

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);
}
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 10 with CityResponse

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);
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Timer(com.codahale.metrics.Timer) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) Location(com.maxmind.geoip2.record.Location)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)25 InetAddress (java.net.InetAddress)13 Location (com.maxmind.geoip2.record.Location)10 HashMap (java.util.HashMap)10 Country (com.maxmind.geoip2.record.Country)9 IOException (java.io.IOException)9 City (com.maxmind.geoip2.record.City)7 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)6 Continent (com.maxmind.geoip2.record.Continent)6 Subdivision (com.maxmind.geoip2.record.Subdivision)6 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)5 CountryResponse (com.maxmind.geoip2.model.CountryResponse)5 Postal (com.maxmind.geoip2.record.Postal)5 Test (org.junit.Test)5 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 UnknownHostException (java.net.UnknownHostException)4 FlowFile (org.apache.nifi.flowfile.FlowFile)4 Traits (com.maxmind.geoip2.record.Traits)3 User (com.earth2me.essentials.User)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2