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;
}
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();
}
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);
}
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);
}
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;
}
Aggregations