Search in sources :

Example 21 with CityResponse

use of com.maxmind.geoip2.model.CityResponse 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 22 with CityResponse

use of com.maxmind.geoip2.model.CityResponse 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)

Example 23 with CityResponse

use of com.maxmind.geoip2.model.CityResponse in project tutorials by eugenp.

the class RawDBDemoGeoIPLocationService method getLocation.

public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
    InetAddress ipAddress = InetAddress.getByName(ip);
    CityResponse response = dbReader.city(ipAddress);
    String cityName = response.getCity().getName();
    String latitude = response.getLocation().getLatitude().toString();
    String longitude = response.getLocation().getLongitude().toString();
    return new GeoIP(ip, cityName, latitude, longitude);
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) GeoIP(com.baeldung.spring.form.GeoIP) InetAddress(java.net.InetAddress)

Example 24 with CityResponse

use of com.maxmind.geoip2.model.CityResponse in project Essentials by EssentialsX.

the class EssentialsGeoIPPlayerListener method delayedJoin.

public void delayedJoin(Player player) {
    User u = ess.getUser(player);
    if (u.isAuthorized("essentials.geoip.hide") || player.getAddress() == null) {
        return;
    }
    InetAddress address = player.getAddress().getAddress();
    StringBuilder sb = new StringBuilder();
    try {
        if (config.getBoolean("database.show-cities", false)) {
            CityResponse response = mmreader.city(address);
            if (response == null) {
                return;
            }
            String city;
            String region;
            String country;
            city = response.getCity().getName();
            region = response.getMostSpecificSubdivision().getName();
            country = response.getCountry().getName();
            if (city != null) {
                sb.append(city).append(", ");
            }
            if (region != null) {
                sb.append(region).append(", ");
            }
            sb.append(country);
        } else {
            CountryResponse response = mmreader.country(address);
            sb.append(response.getCountry().getName());
        }
    } catch (AddressNotFoundException ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        // TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
        logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    } catch (IOException | GeoIp2Exception ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    }
    if (config.getBoolean("show-on-whois", true)) {
        u.setGeoLocation(sb.toString());
    }
    if (config.getBoolean("show-on-login", true) && !u.isHidden()) {
        for (Player onlinePlayer : player.getServer().getOnlinePlayers()) {
            User user = ess.getUser(onlinePlayer);
            if (user.isAuthorized("essentials.geoip.show")) {
                user.sendMessage(tl("geoipJoinFormat", u.getDisplayName(), sb.toString()));
            }
        }
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Player(org.bukkit.entity.Player) User(com.earth2me.essentials.User) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress)

Example 25 with CityResponse

use of com.maxmind.geoip2.model.CityResponse in project arctic-sea by 52North.

the class StatisticsLocationUtil method ip2SpatialData.

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;
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashMap(java.util.HashMap) Country(com.maxmind.geoip2.record.Country) IAdminStatisticsLocation(org.n52.iceland.statistics.api.interfaces.geolocation.IAdminStatisticsLocation) 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