Search in sources :

Example 11 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project OpenAM by OpenRock.

the class Adaptive method checkGeoLocation.

protected int checkGeoLocation() {
    int retVal = 0;
    String countryCode;
    if (debug.messageEnabled()) {
        debug.message("{}.checkGeoLocation: GeoLocation database location = {}", ADAPTIVE, geoLocationDatabase);
    }
    DatabaseReader db = getLookupService(geoLocationDatabase);
    if (db == null) {
        debug.error("{}.checkGeoLocation: GeoLocation database lookup returns null", ADAPTIVE);
        return geoLocationScore;
    }
    if (geoLocationValues == null) {
        debug.error("{}.checkGeoLocation: The property '{}' is null", ADAPTIVE, GEO_LOCATION_VALUES);
        return geoLocationScore;
    }
    try {
        countryCode = getCountryCode(db, clientIP);
    } catch (IOException e) {
        if (debug.warningEnabled()) {
            debug.warning("{}.checkGeoLocation: #getCountryCode :: An IO error happened", ADAPTIVE, e);
        }
        return geoLocationScore;
    } catch (GeoIp2Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{}.checkGeoLocation: #getCountryCode :: An error happened when looking up the IP", ADAPTIVE, e);
        }
        return geoLocationScore;
    }
    if (debug.messageEnabled()) {
        debug.message("{}.checkGeoLocation: {} returns {}", ADAPTIVE, clientIP, countryCode);
    }
    StringTokenizer st = new StringTokenizer(geoLocationValues, "|");
    while (st.hasMoreTokens()) {
        if (countryCode.equalsIgnoreCase(st.nextToken())) {
            if (debug.messageEnabled()) {
                debug.message("{}.checkGeoLocation: Found Country Code : {}", ADAPTIVE, countryCode);
            }
            retVal = geoLocationScore;
            break;
        }
    }
    if (!geoLocationInvert) {
        retVal = geoLocationScore - retVal;
    }
    return retVal;
}
Also used : StringTokenizer(java.util.StringTokenizer) DatabaseReader(com.maxmind.geoip2.DatabaseReader) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception)

Example 12 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project GNS by MobilityFirst.

the class GeoIPUtils method getLocation_City.

/**
	 * This method is used for test, whether a static method can be called by active code  
	 * @param ip
	 * @param dbReader
	 * @return a GeoIP response
	 */
public static CityResponse getLocation_City(String ip, DatabaseReader dbReader) {
    try {
        InetAddress ipAddress = InetAddress.getByName(ip);
        CityResponse response = dbReader.city(ipAddress);
        return response;
    } catch (IOException | GeoIp2Exception e) {
        return null;
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) IOException(java.io.IOException) InetAddress(java.net.InetAddress) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception)

Example 13 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project nutch by apache.

the class GeoIPDocumentCreator method createDocFromInsightsService.

public static NutchDocument createDocFromInsightsService(String serverIp, NutchDocument doc, WebServiceClient client) throws UnknownHostException, IOException, GeoIp2Exception {
    doc.add("ip", serverIp);
    InsightsResponse response = client.insights(InetAddress.getByName(serverIp));
    // CityResponse response = client.city(InetAddress.getByName(serverIp));
    City city = response.getCity();
    // 'Minneapolis'
    doc.add("cityName", city.getName());
    // 50
    doc.add("cityConfidence", city.getConfidence());
    doc.add("cityGeoNameId", city.getGeoNameId());
    Continent continent = response.getContinent();
    doc.add("continentCode", continent.getCode());
    doc.add("continentGeoNameId", continent.getGeoNameId());
    doc.add("continentName", continent.getName());
    Country country = response.getCountry();
    // 'US'
    doc.add("countryIsoCode", country.getIsoCode());
    // 'United States'
    doc.add("countryName", country.getName());
    // 99
    doc.add("countryConfidence", country.getConfidence());
    doc.add("countryGeoName", country.getGeoNameId());
    Location location = response.getLocation();
    // 44.9733,
    doc.add("latLon", location.getLatitude() + "," + location.getLongitude());
    // -93.2323
    // 3
    doc.add("accRadius", location.getAccuracyRadius());
    // 'America/Chicago'
    doc.add("timeZone", location.getTimeZone());
    doc.add("metroCode", location.getMetroCode());
    Postal postal = response.getPostal();
    // '55455'
    doc.add("postalCode", postal.getCode());
    // 40
    doc.add("postalConfidence", postal.getConfidence());
    RepresentedCountry rCountry = response.getRepresentedCountry();
    doc.add("countryType", rCountry.getType());
    Subdivision subdivision = response.getMostSpecificSubdivision();
    // 'Minnesota'
    doc.add("subDivName", subdivision.getName());
    // 'MN'
    doc.add("subDivIdoCode", subdivision.getIsoCode());
    // 90
    doc.add("subDivConfidence", subdivision.getConfidence());
    doc.add("subDivGeoNameId", subdivision.getGeoNameId());
    Traits traits = response.getTraits();
    doc.add("autonSystemNum", traits.getAutonomousSystemNumber());
    doc.add("autonSystemOrg", traits.getAutonomousSystemOrganization());
    doc.add("domain", traits.getDomain());
    doc.add("isp", traits.getIsp());
    doc.add("org", traits.getOrganization());
    doc.add("userType", traits.getUserType());
    doc.add("isAnonProxy", traits.isAnonymousProxy());
    doc.add("isSatelliteProv", traits.isSatelliteProvider());
    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 14 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project nutch by apache.

the class GeoIPDocumentCreator method createDocFromCityDb.

public static NutchDocument createDocFromCityDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
    doc.add("ip", serverIp);
    CityResponse response = reader.city(InetAddress.getByName(serverIp));
    City city = response.getCity();
    // 'Minneapolis'
    doc.add("cityName", city.getName());
    // 50
    doc.add("cityConfidence", city.getConfidence());
    doc.add("cityGeoNameId", city.getGeoNameId());
    Continent continent = response.getContinent();
    doc.add("continentCode", continent.getCode());
    doc.add("continentGeoNameId", continent.getGeoNameId());
    doc.add("continentName", continent.getName());
    Country country = response.getCountry();
    // 'US'
    doc.add("countryIsoCode", country.getIsoCode());
    // 'United States'
    doc.add("countryName", country.getName());
    // 99
    doc.add("countryConfidence", country.getConfidence());
    doc.add("countryGeoName", country.getGeoNameId());
    Location location = response.getLocation();
    // 44.9733,
    doc.add("latLon", location.getLatitude() + "," + location.getLongitude());
    // -93.2323
    // 3
    doc.add("accRadius", location.getAccuracyRadius());
    // 'America/Chicago'
    doc.add("timeZone", location.getTimeZone());
    doc.add("metroCode", location.getMetroCode());
    Postal postal = response.getPostal();
    // '55455'
    doc.add("postalCode", postal.getCode());
    // 40
    doc.add("postalConfidence", postal.getConfidence());
    RepresentedCountry rCountry = response.getRepresentedCountry();
    doc.add("countryType", rCountry.getType());
    Subdivision subdivision = response.getMostSpecificSubdivision();
    // 'Minnesota'
    doc.add("subDivName", subdivision.getName());
    // 'MN'
    doc.add("subDivIdoCode", subdivision.getIsoCode());
    // 90
    doc.add("subDivConfidence", subdivision.getConfidence());
    doc.add("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 15 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project nutch by apache.

the class GeoIPDocumentCreator method createDocFromIspDb.

public static NutchDocument createDocFromIspDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
    IspResponse response = reader.isp(InetAddress.getByName(serverIp));
    doc.add("ip", serverIp);
    doc.add("autonSystemNum", response.getAutonomousSystemNumber());
    doc.add("autonSystemOrg", response.getAutonomousSystemOrganization());
    doc.add("isp", response.getIsp());
    doc.add("org", response.getOrganization());
    return doc;
}
Also used : IspResponse(com.maxmind.geoip2.model.IspResponse)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)10 InetAddress (java.net.InetAddress)10 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)9 IOException (java.io.IOException)9 City (com.maxmind.geoip2.record.City)4 Country (com.maxmind.geoip2.record.Country)4 Location (com.maxmind.geoip2.record.Location)4 Subdivision (com.maxmind.geoip2.record.Subdivision)4 HashMap (java.util.HashMap)4 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)3 CountryResponse (com.maxmind.geoip2.model.CountryResponse)3 Continent (com.maxmind.geoip2.record.Continent)3 Postal (com.maxmind.geoip2.record.Postal)3 UnknownHostException (java.net.UnknownHostException)3 Timer (com.codahale.metrics.Timer)2 User (com.earth2me.essentials.User)2 DatabaseReader (com.maxmind.geoip2.DatabaseReader)2 IspResponse (com.maxmind.geoip2.model.IspResponse)2 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2