com.maxmind.geoip2.record.Subdivision Java Examples

The following examples show how to use com.maxmind.geoip2.record.Subdivision. 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: MaxMindGeoLocationServiceTest.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
@Test
public void lookupShouldReturnCountryIsoWhenDatabaseReaderWasSet() throws NoSuchFieldException, IOException,
        GeoIp2Exception {
    // given
    final Country country = new Country(null, null, null, "fr", null);
    final Continent continent = new Continent(null, "eu", null, null);
    final City city = new City(singletonList("test"), null, null, singletonMap("test", "Paris"));
    final Location location = new Location(null, null, 48.8566, 2.3522,
            null, null, null);
    final ArrayList<Subdivision> subdivisions = new ArrayList<>();
    subdivisions.add(new Subdivision(null, null, null, "paris", null));
    final CityResponse cityResponse = new CityResponse(city, continent, country, location, null,
            null, null, null, subdivisions, null);

    final DatabaseReader databaseReader = Mockito.mock(DatabaseReader.class);
    given(databaseReader.city(any())).willReturn(cityResponse);

    FieldSetter.setField(maxMindGeoLocationService,
            maxMindGeoLocationService.getClass().getDeclaredField("databaseReader"), databaseReader);

    // when
    final Future<GeoInfo> future = maxMindGeoLocationService.lookup(TEST_IP, null);

    // then
    assertThat(future.succeeded()).isTrue();
    assertThat(future.result())
            .isEqualTo(GeoInfo.builder()
                    .vendor("maxmind")
                    .continent("eu")
                    .country("fr")
                    .region("paris")
                    .city("Paris")
                    .lat(48.8566f)
                    .lon(2.3522f)
                    .build());
}
 
Example #2
Source File: IPLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Record createRecord(final CityResponse city) {
    if (city == null) {
        return null;
    }

    final Map<String, Object> values = new HashMap<>();
    values.put(CitySchema.CITY.getFieldName(), city.getCity().getName());

    final Location location = city.getLocation();
    values.put(CitySchema.ACCURACY.getFieldName(), location.getAccuracyRadius());
    values.put(CitySchema.METRO_CODE.getFieldName(), location.getMetroCode());
    values.put(CitySchema.TIMEZONE.getFieldName(), location.getTimeZone());
    values.put(CitySchema.LATITUDE.getFieldName(), location.getLatitude());
    values.put(CitySchema.LONGITUDE.getFieldName(), location.getLongitude());
    values.put(CitySchema.CONTINENT.getFieldName(), city.getContinent().getName());
    values.put(CitySchema.POSTALCODE.getFieldName(), city.getPostal().getCode());
    values.put(CitySchema.COUNTRY.getFieldName(), createRecord(city.getCountry()));

    final Object[] subdivisions = new Object[city.getSubdivisions().size()];
    int i = 0;
    for (final Subdivision subdivision : city.getSubdivisions()) {
        subdivisions[i++] = createRecord(subdivision);
    }
    values.put(CitySchema.SUBDIVISIONS.getFieldName(), subdivisions);

    return new MapRecord(CitySchema.GEO_SCHEMA, values);
}
 
Example #3
Source File: IPLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Record createRecord(final Subdivision subdivision) {
    if (subdivision == null) {
        return null;
    }

    final Map<String, Object> values = new HashMap<>(2);
    values.put(CitySchema.SUBDIVISION_NAME.getFieldName(), subdivision.getName());
    values.put(CitySchema.SUBDIVISION_ISO.getFieldName(), subdivision.getIsoCode());
    return new MapRecord(CitySchema.SUBDIVISION_SCHEMA, values);
}
 
Example #4
Source File: GeoEnrichIP.java    From localization_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)) { //TODO need to add additional validation - should look like an IPv4 or IPv6 addr for instance
        session.transfer(flowFile, REL_NOT_FOUND);
        getLogger().warn("Unable to find ip address for {}", new Object[]{flowFile});
        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 {} to ip address for {}", new Object[]{ipAttributeValue, flowFile}, ioe);
        return;
    }
    final StopWatch stopWatch = new StopWatch(true);
    try {
        response = dbReader.city(inetAddress);
        stopWatch.stop();
    } catch (final IOException | GeoIp2Exception ex) {
        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());
    }

    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);
}
 
Example #5
Source File: MaxMindGeoLocationService.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
private String getRegionCode(InetAddress inetAddress) throws IOException, GeoIp2Exception {
    final List<Subdivision> subdivisions = getCity(inetAddress).getSubdivisions();
    return CollectionUtils.isEmpty(subdivisions) ? null : subdivisions.get(0).getIsoCode();
}
 
Example #6
Source File: MaxMind2HostGeoLookup.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public HostGeoInfo getHostGeoInfo(InetAddress address) throws MalformedURLException, IOException {
    if (lookupFailed) return null;
    
    DatabaseReader ll = getDatabaseReader();
    if (ll==null) return null;
    
    InetAddress extAddress = address;
    if (Networking.isPrivateSubnet(extAddress)) extAddress = InetAddress.getByName(LocalhostExternalIpLoader.getLocalhostIpQuicklyOrDefault());
    
    try {
        CityResponse l = ll.city(extAddress);
        if (l==null) {
            if (log.isDebugEnabled()) log.debug("Geo info failed to find location for address {}, using {}", extAddress, ll);
            return null;
        }
        
        StringBuilder name = new StringBuilder();
        
        if (l.getCity()!=null && l.getCity().getName()!=null) name.append(l.getCity().getName());
        
        if (l.getSubdivisions()!=null) {
            for (Subdivision subd: Lists.reverse(l.getSubdivisions())) {
                if (name.length()>0) name.append(", ");
                // prefer e.g. USA state codes over state names
                if (!Strings.isBlank(subd.getIsoCode())) 
                    name.append(subd.getIsoCode());
                else
                    name.append(subd.getName());
            }
        }
        
        if (l.getCountry()!=null) {
            if (name.length()==0) {
                name.append(l.getCountry().getName());
            } else {
                name.append(" ("); name.append(l.getCountry().getIsoCode()); name.append(")");
            }
        }

        
        HostGeoInfo geo = new HostGeoInfo(address.getHostName(), name.toString(), l.getLocation().getLatitude(), l.getLocation().getLongitude());
        log.debug("Geo info lookup (MaxMind DB) for "+address+" returned: "+geo);
        return geo;
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Geo info lookup failed: "+e);
        throw Throwables.propagate(e);
    }
}
 
Example #7
Source File: GeoEnrichIP.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;
    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 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);
}