Java Code Examples for com.maxmind.geoip2.model.IspResponse#getIsp()

The following examples show how to use com.maxmind.geoip2.model.IspResponse#getIsp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MMDB.java    From bidder with Apache License 2.0 6 votes vote down vote up
public String solve(String key) {
	String value = null;
	try {
		String parts[] = key.split("/");
		if (parts.length < 3)
			return null;
		String sip = parts[2];
		InetAddress ip = InetAddress.getByName(sip);
		IspResponse r = reader.isp(ip);
		switch (parts[1]) {
		case "org":
			return r.getOrganization();
		case "isp":
			return r.getIsp();
		case "json":
			return r.toJson();
		default:
			return null;
		}
	} catch (Exception e) {

	}
	return value;
}
 
Example 2
Source File: MMDB.java    From XRTB with Apache License 2.0 6 votes vote down vote up
public String solve(String key) {
	String value = null;
	try {
		String parts[] = key.split("/");
		if (parts.length < 3)
			return null;
		String sip = parts[2];
		InetAddress ip = InetAddress.getByName(sip);
		IspResponse r = reader.isp(ip);
		switch (parts[1]) {
		case "org":
			return r.getOrganization();
		case "isp":
			return r.getIsp();
		case "json":
			return r.toJson();
		default:
			return null;
		}
	} catch (Exception e) {

	}
	return value;
}
 
Example 3
Source File: ISPEnrichIP.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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 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)));



    // During test I observed behavior where null values in ASN data could trigger NPEs. Instead of relying on the
    // underlying database to be free from Nulls wrapping ensure equality to null without assigning a variable
    // 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);
}