com.maxmind.geoip2.record.Country Java Examples

The following examples show how to use com.maxmind.geoip2.record.Country. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocRecord.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 6 votes vote down vote up
public LocRecord(final String owner, final double lat, final double lon) {
	this.owner = owner;
	final StringBuilder sb = new StringBuilder();
	//owner TTL class LOC ( d1 [m1 [s1]] {"N"|"S"} d2 [m2 [s2]] {"E"|"W"} alt["m"] [siz["m"] [hp["m"] [vp["m"]]]] )
	final double[] latDms = Angle.fromDegreesLatitude(lat).toDMS();
	final double[] lonDms = Angle.fromDegreesLatitude(lon).toDMS();
	sb.append(owner).append(".").append(SPACE).append(0).append(SPACE).append("IN").append(SPACE).append("LOC").append(SPACE);
	sb.append(Math.abs((int) latDms[0])).append(SPACE).append((int) latDms[1]).append(SPACE).append((float) latDms[2]).append(SPACE).append(lat >= 0 ? "N" : "S")
			.append(SPACE);
	sb.append(Math.abs((int) lonDms[0])).append(SPACE).append((int) lonDms[1]).append(SPACE).append((float) lonDms[2]).append(SPACE).append(lon >= 0 ? "E" : "W")
			.append(SPACE);
	sb.append("0m").append(SPACE).append("0m").append(SPACE).append("0m").append(SPACE).append("0m");
	raw = sb.toString();
	final Map<String, String> names = new HashMap<>();
	names.put("name", "Loc record");
	final City city = new City(null, null, null, names);
	final Country country = new Country(null, 0, 0, LOC, names);
	final Location loc = new Location(0, 0, lat, lon, null, null, null);
	location = new CityResponse(city, null, country, loc, null, null, null, null, null, null);
	valid = true;
}
 
Example #2
Source File: MaxMindGeoLocationServiceTest.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
@Test
public void lookupShouldReturnCountryIsoWhenDatabaseReaderWasSet() throws NoSuchFieldException, IOException,
        GeoIp2Exception {
    // given
    final Country country = new Country(null, null, null, "fr", null);
    final Continent continent = new Continent(null, "eu", null, null);
    final City city = new City(singletonList("test"), null, null, singletonMap("test", "Paris"));
    final Location location = new Location(null, null, 48.8566, 2.3522,
            null, null, null);
    final ArrayList<Subdivision> subdivisions = new ArrayList<>();
    subdivisions.add(new Subdivision(null, null, null, "paris", null));
    final CityResponse cityResponse = new CityResponse(city, continent, country, location, null,
            null, null, null, subdivisions, null);

    final DatabaseReader databaseReader = Mockito.mock(DatabaseReader.class);
    given(databaseReader.city(any())).willReturn(cityResponse);

    FieldSetter.setField(maxMindGeoLocationService,
            maxMindGeoLocationService.getClass().getDeclaredField("databaseReader"), databaseReader);

    // when
    final Future<GeoInfo> future = maxMindGeoLocationService.lookup(TEST_IP, null);

    // then
    assertThat(future.succeeded()).isTrue();
    assertThat(future.result())
            .isEqualTo(GeoInfo.builder()
                    .vendor("maxmind")
                    .continent("eu")
                    .country("fr")
                    .region("paris")
                    .city("Paris")
                    .lat(48.8566f)
                    .lon(2.3522f)
                    .build());
}
 
Example #3
Source File: StatisticsLocationUtil.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> ip2SpatialData(InetAddress ip) {
    if (!enabled) {
        return null;
    }
    if (reader == null) {
        LOG.warn("Location database is not initialized. Exiting.");
        return null;
    }
    try {
        Map<String, Object> holder = new HashMap<>(3);
        if (dbType == LocationDatabaseType.COUNTRY) {
            Country country = reader.country(ip).getCountry();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), country.getIsoCode());
        } else {
            CityResponse city = reader.city(ip);
            Location loc = city.getLocation();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), city.getCountry().getIsoCode());
            holder.put(ObjectEsParameterFactory.GEOLOC_CITY_NAME.getName(), city.getCity().getName());
            holder.put(ObjectEsParameterFactory.GEOLOC_GEO_POINT.getName(),
                       new GeoPoint(loc.getLatitude(), loc.getLongitude()));
        }
        return holder;
    } catch (Throwable e) {
        LOG.warn("Can't convert IP to GeoIp", e);
    }
    return null;
}
 
Example #4
Source File: CountryCommand.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkPermission(sender, GeoipPermissions.UC_GEOIP_COUNTRY_BASE);
    if (!GeoipHandler.isLoaded()) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "geoip.command.country.notenabled"));
    }

    Player t = args.<Player>getOne("player").get();
    Country country = GeoipHandler.getCountry(t.getConnection().getAddress().getAddress()).orElse(null);
    if (country == null) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "geoip.command.country.failed", "%player%", t));
    }
    Messages.send(sender, "geoip.command.country.success", "%player%", t, "%country%", country.getName());
    return CommandResult.success();
}
 
Example #5
Source File: GeoipVariable.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public Optional<Text> getValue(@Nullable Object player) {
    //TODO offline player support with last known ip
    if (player instanceof Player) {
        Player p = (Player) player;
        Country c = GeoipHandler.getCountry(p.getConnection().getAddress().getAddress()).orElse(null);
        if (c == null) return Optional.empty();
        return Optional.of(Text.of(c.getName()));
    }
    return Optional.empty();
}
 
Example #6
Source File: GeoipHandler.java    From UltimateCore with MIT License 5 votes vote down vote up
public static Optional<Country> getCountry(InetAddress ip) {
    if (dbr == null) return Optional.empty();
    try {
        return Optional.ofNullable(dbr.country(ip).getCountry());
    } catch (Exception ex) {
        return Optional.empty();
    }
}
 
Example #7
Source File: IPLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Record createRecord(final Country country) {
    if (country == null) {
        return null;
    }

    final Map<String, Object> values = new HashMap<>(2);
    values.put(CitySchema.COUNTRY_NAME.getFieldName(), country.getName());
    values.put(CitySchema.COUNTRY_ISO.getFieldName(), country.getIsoCode());
    return new MapRecord(CitySchema.COUNTRY_SCHEMA, values);
}
 
Example #8
Source File: GeoIPCountryDissector.java    From logparser with Apache License 2.0 5 votes vote down vote up
protected void extractCountryFields(final Parsable<?> parsable, final String inputname, AbstractCountryResponse response)
    throws DissectionFailure {
    if (wantAnyContinent) {
        Continent continent = response.getContinent();
        if (continent != null) {
            if (wantContinentName) {
                parsable.addDissection(inputname, "STRING", "continent.name", continent.getName());
            }
            if (wantContinentCode) {
                parsable.addDissection(inputname, "STRING", "continent.code", continent.getCode());
            }
        }
    }
    if (wantAnyCountry) {
        Country country = response.getCountry();
        if (country != null) {
            if (wantCountryName) {
                parsable.addDissection(inputname, "STRING", "country.name", country.getName());
            }
            if (wantCountryIso) {
                parsable.addDissection(inputname, "STRING", "country.iso", country.getIsoCode());
            }

            if (wantCountryGetConfidence) {
                parsable.addDissection(inputname, "NUMBER", "country.getconfidence", country.getConfidence());
            }
            if (wantCountryIsInEuropeanUnion) {
                parsable.addDissection(inputname, "BOOLEAN", "country.isineuropeanunion", country.isInEuropeanUnion() ? 1L : 0L);
            }
        }
    }
}
 
Example #9
Source File: GEOInfo.java    From proxylive with MIT License 4 votes vote down vote up
public Country getCountry() {
    return country;
}
 
Example #10
Source File: GEOInfo.java    From proxylive with MIT License 4 votes vote down vote up
public void setCountry(Country country) {
    this.country = country;
}