Search in sources :

Example 11 with DatabaseReader

use of com.maxmind.geoip2.DatabaseReader in project druid by druid-io.

the class WikipediaIrcDecoder method openGeoIpDb.

private DatabaseReader openGeoIpDb(File geoDb) {
    try {
        DatabaseReader reader = new DatabaseReader(geoDb);
        log.info("Using geo ip database at [%s].", geoDb);
        return reader;
    } catch (IOException e) {
        throw new RuntimeException("Could not open geo db at [" + geoDb.getAbsolutePath() + "].", e);
    }
}
Also used : DatabaseReader(com.maxmind.geoip2.DatabaseReader) IOException(java.io.IOException)

Example 12 with DatabaseReader

use of com.maxmind.geoip2.DatabaseReader 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 13 with DatabaseReader

use of com.maxmind.geoip2.DatabaseReader 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 14 with DatabaseReader

use of com.maxmind.geoip2.DatabaseReader in project PretendYoureXyzzy by ajanata.

the class GeoIP method makeReader.

private synchronized DatabaseReader makeReader() {
    LOG.info("Attempting to create GeoIP database reader");
    initialized = true;
    if (reader != null) {
        return reader;
    }
    final String dbPath = propertiesProvider.get().getProperty("geoip.db");
    if (StringUtils.isNotBlank(dbPath)) {
        final File db = new File(dbPath);
        try {
            reader = new DatabaseReader.Builder(db).withCache(new CHMCache()).build();
        } catch (final IOException e) {
            LOG.error("Unable to create database reader", e);
            reader = null;
        }
    } else {
        reader = null;
    }
    return reader;
}
Also used : CHMCache(com.maxmind.db.CHMCache) DatabaseReader(com.maxmind.geoip2.DatabaseReader) IOException(java.io.IOException) File(java.io.File)

Example 15 with DatabaseReader

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

Aggregations

IOException (java.io.IOException)8 DatabaseReader (com.maxmind.geoip2.DatabaseReader)6 CityResponse (com.maxmind.geoip2.model.CityResponse)5 InetAddress (java.net.InetAddress)5 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)4 IspResponse (com.maxmind.geoip2.model.IspResponse)3 File (java.io.File)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 CHMCache (com.maxmind.db.CHMCache)2 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)2 ConnectionTypeResponse (com.maxmind.geoip2.model.ConnectionTypeResponse)2 DomainResponse (com.maxmind.geoip2.model.DomainResponse)2 Subdivision (com.maxmind.geoip2.record.Subdivision)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2 DatabaseReader (org.apache.nifi.processors.maxmind.DatabaseReader)2 StopWatch (org.apache.nifi.util.StopWatch)2 JsonObject (com.google.gson.JsonObject)1 InvalidDatabaseException (com.maxmind.db.InvalidDatabaseException)1