Search in sources :

Example 1 with GeoIp2Exception

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

the class GeoEnrichIP method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final DatabaseReader dbReader = databaseReaderRef.get();
    final String ipAttributeName = context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
    final String ipAttributeValue = flowFile.getAttribute(ipAttributeName);
    if (StringUtils.isEmpty(ipAttributeName)) {
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("FlowFile '{}' attribute '{}' was empty. Routing to failure", new Object[] { flowFile, IP_ADDRESS_ATTRIBUTE.getDisplayName() });
        return;
    }
    InetAddress inetAddress = null;
    CityResponse response = null;
    try {
        inetAddress = InetAddress.getByName(ipAttributeValue);
    } catch (final IOException ioe) {
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("Could not resolve the IP for value '{}', contained within the attribute '{}' in " + "FlowFile '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the processor with an invalid IP address ", new Object[] { ipAttributeValue, IP_ADDRESS_ATTRIBUTE.getDisplayName(), flowFile }, ioe);
        return;
    }
    final StopWatch stopWatch = new StopWatch(true);
    try {
        response = dbReader.city(inetAddress);
        stopWatch.stop();
    } catch (final IOException | GeoIp2Exception ex) {
        // Note IOException is captured again as dbReader also makes InetAddress.getByName() calls.
        // Most name or IP resolutions failure should have been triggered in the try loop above but
        // environmental conditions may trigger errors during the second resolution as well.
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("Failure while trying to find enrichment data for {} due to {}", new Object[] { flowFile, ex }, ex);
        return;
    }
    if (response == null) {
        session.transfer(flowFile, REL_NOT_FOUND);
        return;
    }
    final Map<String, String> attrs = new HashMap<>();
    attrs.put(new StringBuilder(ipAttributeName).append(".geo.lookup.micros").toString(), String.valueOf(stopWatch.getDuration(TimeUnit.MICROSECONDS)));
    attrs.put(new StringBuilder(ipAttributeName).append(".geo.city").toString(), response.getCity().getName());
    final Double latitude = response.getLocation().getLatitude();
    if (latitude != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".geo.latitude").toString(), latitude.toString());
    }
    final Double longitude = response.getLocation().getLongitude();
    if (longitude != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".geo.longitude").toString(), longitude.toString());
    }
    final Integer accuracy = response.getLocation().getAccuracyRadius();
    if (accuracy != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".accuracy").toString(), String.valueOf(accuracy));
    }
    int i = 0;
    for (final Subdivision subd : response.getSubdivisions()) {
        attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.").append(i).toString(), subd.getName());
        attrs.put(new StringBuilder(ipAttributeName).append(".geo.subdivision.isocode.").append(i).toString(), subd.getIsoCode());
        i++;
    }
    attrs.put(new StringBuilder(ipAttributeName).append(".geo.country").toString(), response.getCountry().getName());
    attrs.put(new StringBuilder(ipAttributeName).append(".geo.country.isocode").toString(), response.getCountry().getIsoCode());
    attrs.put(new StringBuilder(ipAttributeName).append(".geo.postalcode").toString(), response.getPostal().getCode());
    flowFile = session.putAllAttributes(flowFile, attrs);
    session.transfer(flowFile, REL_FOUND);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) IOException(java.io.IOException) Subdivision(com.maxmind.geoip2.record.Subdivision) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) StopWatch(org.apache.nifi.util.StopWatch) CityResponse(com.maxmind.geoip2.model.CityResponse) DatabaseReader(org.apache.nifi.processors.maxmind.DatabaseReader) InetAddress(java.net.InetAddress)

Example 2 with GeoIp2Exception

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

the class ISPEnrichIP method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final DatabaseReader dbReader = databaseReaderRef.get();
    final String ipAttributeName = context.getProperty(IP_ADDRESS_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
    final String ipAttributeValue = flowFile.getAttribute(ipAttributeName);
    if (StringUtils.isEmpty(ipAttributeName)) {
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("FlowFile '{}' attribute '{}' was empty. Routing to failure", new Object[] { flowFile, IP_ADDRESS_ATTRIBUTE.getDisplayName() });
        return;
    }
    InetAddress inetAddress = null;
    IspResponse response = null;
    try {
        inetAddress = InetAddress.getByName(ipAttributeValue);
    } catch (final IOException ioe) {
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("Could not resolve the IP for value '{}', contained within the attribute '{}' in " + "FlowFile '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the processor with an invalid IP address ", new Object[] { ipAttributeValue, IP_ADDRESS_ATTRIBUTE.getDisplayName(), flowFile }, ioe);
        return;
    }
    final StopWatch stopWatch = new StopWatch(true);
    try {
        response = dbReader.isp(inetAddress);
        stopWatch.stop();
    } catch (final IOException | GeoIp2Exception ex) {
        // Note IOException is captured again as dbReader also makes InetAddress.getByName() calls.
        // Most name or IP resolutions failure should have been triggered in the try loop above but
        // environmental conditions may trigger errors during the second resolution as well.
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("Failure while trying to find enrichment data for {} due to {}", new Object[] { flowFile, ex }, ex);
        return;
    }
    if (response == null) {
        session.transfer(flowFile, REL_NOT_FOUND);
        return;
    }
    final Map<String, String> attrs = new HashMap<>();
    attrs.put(new StringBuilder(ipAttributeName).append(".isp.lookup.micros").toString(), String.valueOf(stopWatch.getDuration(TimeUnit.MICROSECONDS)));
    // seem like good option to "final int asn ..." as with the other returned data.
    if (!(response.getAutonomousSystemNumber() == null)) {
        attrs.put(new StringBuilder(ipAttributeName).append(".isp.asn").toString(), String.valueOf(response.getAutonomousSystemNumber()));
    }
    final String asnOrg = response.getAutonomousSystemOrganization();
    if (asnOrg != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".isp.asn.organization").toString(), asnOrg);
    }
    final String ispName = response.getIsp();
    if (ispName != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".isp.name").toString(), ispName);
    }
    final String organisation = response.getOrganization();
    if (organisation != null) {
        attrs.put(new StringBuilder(ipAttributeName).append(".isp.organization").toString(), organisation);
    }
    flowFile = session.putAllAttributes(flowFile, attrs);
    session.transfer(flowFile, REL_FOUND);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) StopWatch(org.apache.nifi.util.StopWatch) IspResponse(com.maxmind.geoip2.model.IspResponse) DatabaseReader(org.apache.nifi.processors.maxmind.DatabaseReader) InetAddress(java.net.InetAddress)

Example 3 with GeoIp2Exception

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

the class GeoIPDocumentCreator method createDocFromConnectionDb.

public static NutchDocument createDocFromConnectionDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
    ConnectionTypeResponse response = reader.connectionType(InetAddress.getByName(serverIp));
    doc.add("ip", serverIp);
    doc.add("connType", response.getConnectionType().toString());
    return doc;
}
Also used : ConnectionTypeResponse(com.maxmind.geoip2.model.ConnectionTypeResponse)

Example 4 with GeoIp2Exception

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

the class GeoIPDocumentCreator method createDocFromDomainDb.

public static NutchDocument createDocFromDomainDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
    DomainResponse response = reader.domain(InetAddress.getByName(serverIp));
    doc.add("ip", serverIp);
    doc.add("domain", response.getDomain());
    return doc;
}
Also used : DomainResponse(com.maxmind.geoip2.model.DomainResponse)

Example 5 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project tutorials by eugenp.

the class GeoIpIntegrationTest method givenIP_whenFetchingCity_thenReturnsCityData.

@Test
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
    File database = new File("your-path-to-db-file");
    DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
    InetAddress ipAddress = InetAddress.getByName("your-public-ip");
    CityResponse response = dbReader.city(ipAddress);
    String countryName = response.getCountry().getName();
    String cityName = response.getCity().getName();
    String postal = response.getPostal().getCode();
    String state = response.getLeastSpecificSubdivision().getName();
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) DatabaseReader(com.maxmind.geoip2.DatabaseReader) File(java.io.File) InetAddress(java.net.InetAddress) Test(org.junit.Test)

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