Search in sources :

Example 1 with AnonymousIpResponse

use of com.maxmind.geoip2.model.AnonymousIpResponse in project nifi by apache.

the class IPLookupService method doLookup.

private Optional<Record> doLookup(final DatabaseReader databaseReader, final Map<String, Object> coordinates) throws LookupFailureException, InvalidDatabaseException {
    if (coordinates.get(IP_KEY) == null) {
        return Optional.empty();
    }
    final String ipAddress = coordinates.get(IP_KEY).toString();
    final InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(ipAddress);
    } catch (final IOException ioe) {
        getLogger().warn("Could not resolve the IP for value '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the service with an invalid IP address", new Object[] { coordinates }, ioe);
        return Optional.empty();
    }
    final Record geoRecord;
    if (getProperty(LOOKUP_CITY).asBoolean()) {
        final CityResponse cityResponse;
        try {
            cityResponse = databaseReader.city(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup City information for IP Address " + inetAddress, e);
        }
        geoRecord = createRecord(cityResponse);
    } else {
        geoRecord = null;
    }
    final Record ispRecord;
    if (getProperty(LOOKUP_ISP).asBoolean()) {
        final IspResponse ispResponse;
        try {
            ispResponse = databaseReader.isp(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup ISP information for IP Address " + inetAddress, e);
        }
        ispRecord = createRecord(ispResponse);
    } else {
        ispRecord = null;
    }
    final String domainName;
    if (getProperty(LOOKUP_DOMAIN).asBoolean()) {
        final DomainResponse domainResponse;
        try {
            domainResponse = databaseReader.domain(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
        }
        domainName = domainResponse == null ? null : domainResponse.getDomain();
    } else {
        domainName = null;
    }
    final String connectionType;
    if (getProperty(LOOKUP_CONNECTION_TYPE).asBoolean()) {
        final ConnectionTypeResponse connectionTypeResponse;
        try {
            connectionTypeResponse = databaseReader.connectionType(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
        }
        if (connectionTypeResponse == null) {
            connectionType = null;
        } else {
            final ConnectionType type = connectionTypeResponse.getConnectionType();
            connectionType = type == null ? null : type.name();
        }
    } else {
        connectionType = null;
    }
    final Record anonymousIpRecord;
    if (getProperty(LOOKUP_ANONYMOUS_IP_INFO).asBoolean()) {
        final AnonymousIpResponse anonymousIpResponse;
        try {
            anonymousIpResponse = databaseReader.anonymousIp(inetAddress);
        } catch (final InvalidDatabaseException idbe) {
            throw idbe;
        } catch (final Exception e) {
            throw new LookupFailureException("Failed to lookup Anonymous IP Information for IP Address " + inetAddress, e);
        }
        anonymousIpRecord = createRecord(anonymousIpResponse);
    } else {
        anonymousIpRecord = null;
    }
    if (geoRecord == null && ispRecord == null && domainName == null && connectionType == null && anonymousIpRecord == null) {
        return Optional.empty();
    }
    return Optional.ofNullable(createContainerRecord(geoRecord, ispRecord, domainName, connectionType, anonymousIpRecord));
}
Also used : ConnectionType(com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType) IOException(java.io.IOException) ConnectionTypeResponse(com.maxmind.geoip2.model.ConnectionTypeResponse) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) InvalidDatabaseException(com.maxmind.db.InvalidDatabaseException) IOException(java.io.IOException) AnonymousIpResponse(com.maxmind.geoip2.model.AnonymousIpResponse) CityResponse(com.maxmind.geoip2.model.CityResponse) LookupFailureException(org.apache.nifi.lookup.LookupFailureException) IspResponse(com.maxmind.geoip2.model.IspResponse) InvalidDatabaseException(com.maxmind.db.InvalidDatabaseException) DomainResponse(com.maxmind.geoip2.model.DomainResponse) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) InetAddress(java.net.InetAddress)

Aggregations

InvalidDatabaseException (com.maxmind.db.InvalidDatabaseException)1 AnonymousIpResponse (com.maxmind.geoip2.model.AnonymousIpResponse)1 CityResponse (com.maxmind.geoip2.model.CityResponse)1 ConnectionTypeResponse (com.maxmind.geoip2.model.ConnectionTypeResponse)1 ConnectionType (com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType)1 DomainResponse (com.maxmind.geoip2.model.DomainResponse)1 IspResponse (com.maxmind.geoip2.model.IspResponse)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 LookupFailureException (org.apache.nifi.lookup.LookupFailureException)1 MapRecord (org.apache.nifi.serialization.record.MapRecord)1 Record (org.apache.nifi.serialization.record.Record)1