Search in sources :

Example 6 with Country

use of com.maxmind.geoip2.record.Country in project Nucleus by NucleusPowered.

the class GeoIpDatabaseHandler method getDetails.

public CompletableFuture<Optional<Country>> getDetails(InetAddress address) throws Exception {
    init();
    final CompletableFuture<Optional<Country>> completableFuture = new CompletableFuture<>();
    Sponge.getScheduler().createAsyncExecutor(Nucleus.getNucleus()).execute(() -> {
        try {
            load(LoadType.IF_REQUIRED);
            int counter = 0;
            // Load check.
            if (isLoading) {
                while (counter < 30) {
                    Thread.sleep(500);
                    if (!isLoading) {
                        break;
                    }
                    counter++;
                }
                if (counter == 30) {
                    completableFuture.completeExceptionally(new TimeoutException("loading"));
                    return;
                }
            }
            completableFuture.complete(Optional.ofNullable(databaseReader.country(address).getCountry()));
        } catch (AddressNotFoundException ex) {
            completableFuture.complete(Optional.empty());
        } catch (Exception e) {
            completableFuture.completeExceptionally(e);
        }
    });
    return completableFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with Country

use of com.maxmind.geoip2.record.Country in project Nucleus by NucleusPowered.

the class GeoIpCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Player player = args.<Player>getOne(playerKey).get();
    Optional<Country> country = databaseHandler.getDetails(player.getConnection().getAddress().getAddress()).get();
    if (country.isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("geoip.playerfrom", player.getName(), country.get().getName()));
    } else {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("geoip.noinfo", player.getName()));
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Country(com.maxmind.geoip2.record.Country)

Example 8 with Country

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

Example 9 with Country

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

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

CityResponse (com.maxmind.geoip2.model.CityResponse)12 Country (com.maxmind.geoip2.record.Country)11 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)10 Location (com.maxmind.geoip2.record.Location)9 IOException (java.io.IOException)9 InetAddress (java.net.InetAddress)8 CountryResponse (com.maxmind.geoip2.model.CountryResponse)7 City (com.maxmind.geoip2.record.City)7 Continent (com.maxmind.geoip2.record.Continent)7 UnknownHostException (java.net.UnknownHostException)7 HashMap (java.util.HashMap)6 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)5 Postal (com.maxmind.geoip2.record.Postal)5 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 Subdivision (com.maxmind.geoip2.record.Subdivision)4 GeoDef (com.quickutil.platform.def.GeoDef)4 JsonObject (com.google.gson.JsonObject)3 Traits (com.maxmind.geoip2.record.Traits)3 User (com.earth2me.essentials.User)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2