com.maxmind.geoip2.model.CityResponse Java Examples

The following examples show how to use com.maxmind.geoip2.model.CityResponse. 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: GeoEnrichIPRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
private boolean enrichRecord(CityResponse response, Record record, Map<PropertyDescriptor, RecordPath> cached) {
    boolean retVal;

    if (response == null) {
        return false;
    } else if (response.getCity() == null) {
        return false;
    }

    boolean city = update(GEO_CITY, cached, record, response.getCity().getName());
    boolean accuracy = update(GEO_ACCURACY, cached, record, response.getCity().getConfidence());
    boolean country = update(GEO_COUNTRY, cached, record, response.getCountry().getName());
    boolean iso = update(GEO_COUNTRY_ISO, cached, record, response.getCountry().getIsoCode());
    boolean lat = update(GEO_LATITUDE, cached, record, response.getLocation().getLatitude());
    boolean lon = update(GEO_LONGITUDE, cached, record, response.getLocation().getLongitude());
    boolean postal = update(GEO_POSTAL_CODE, cached, record, response.getPostal().getCode());

    retVal = (city || accuracy || country || iso || lat || lon || postal);

    return retVal;
}
 
Example #2
Source File: GeoIpIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
    
    ClassLoader classLoader = getClass().getClassLoader();
    File database = new File(classLoader.getResource("GeoLite2-City.mmdb").getFile());
    DatabaseReader dbReader = new DatabaseReader.Builder(database).build();

    InetAddress ipAddress = InetAddress.getByName("google.com");
    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();

}
 
Example #3
Source File: GeoIpProcessor.java    From sawmill with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> extractGeoIp(InetAddress ipAddress) throws GeoIp2Exception, IOException {
    CityResponse response = databaseReader.city(ipAddress);

    if (LOCATION.getValue(response) == null) {
        return null;
    }

    Map<String, Object> geoIp = new HashMap<>();
    for (Property property : properties) {
        Object propertyValue = property.getValue(response);

        if (propertyValue != null) {
            geoIp.put(property.toString(), propertyValue);
        }
    }

    return geoIp;
}
 
Example #4
Source File: LocRecord.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 6 votes vote down vote up
public LocRecord(final String owner, final double lat, final double lon) {
	this.owner = owner;
	final StringBuilder sb = new StringBuilder();
	//owner TTL class LOC ( d1 [m1 [s1]] {"N"|"S"} d2 [m2 [s2]] {"E"|"W"} alt["m"] [siz["m"] [hp["m"] [vp["m"]]]] )
	final double[] latDms = Angle.fromDegreesLatitude(lat).toDMS();
	final double[] lonDms = Angle.fromDegreesLatitude(lon).toDMS();
	sb.append(owner).append(".").append(SPACE).append(0).append(SPACE).append("IN").append(SPACE).append("LOC").append(SPACE);
	sb.append(Math.abs((int) latDms[0])).append(SPACE).append((int) latDms[1]).append(SPACE).append((float) latDms[2]).append(SPACE).append(lat >= 0 ? "N" : "S")
			.append(SPACE);
	sb.append(Math.abs((int) lonDms[0])).append(SPACE).append((int) lonDms[1]).append(SPACE).append((float) lonDms[2]).append(SPACE).append(lon >= 0 ? "E" : "W")
			.append(SPACE);
	sb.append("0m").append(SPACE).append("0m").append(SPACE).append("0m").append(SPACE).append("0m");
	raw = sb.toString();
	final Map<String, String> names = new HashMap<>();
	names.put("name", "Loc record");
	final City city = new City(null, null, null, names);
	final Country country = new Country(null, 0, 0, LOC, names);
	final Location loc = new Location(0, 0, lat, lon, null, null, null);
	location = new CityResponse(city, null, country, loc, null, null, null, null, null, null);
	valid = true;
}
 
Example #5
Source File: GeoEnrichIPRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
private CityResponse geocode(RecordPath ipPath, Record record, DatabaseReader reader) throws Exception {
    RecordPathResult result = ipPath.evaluate(record);
    Optional<FieldValue> ipField = result.getSelectedFields().findFirst();
    if (ipField.isPresent()) {
        FieldValue value = ipField.get();
        Object val = value.getValue();
        if (val == null) {
            return null;
        }
        String realValue = val.toString();
        InetAddress address = InetAddress.getByName(realValue);

        return reader.city(address);
    } else {
        return null;
    }
}
 
Example #6
Source File: Benchmark.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
private static void bench(DatabaseReader r, int count, int seed) throws GeoIp2Exception, UnknownHostException {
    Random random = new Random(seed);
    long startTime = System.nanoTime();
    byte[] address = new byte[4];
    for (int i = 0; i < count; i++) {
        random.nextBytes(address);
        InetAddress ip = InetAddress.getByAddress(address);
        CityResponse t;
        try {
            t = r.city(ip);
        } catch (AddressNotFoundException | IOException e) {
        }
        if (TRACE) {
            if (i % 50000 == 0) {
                System.out.println(i + " " + ip);
                System.out.println(t);
            }
        }
    }
    long endTime = System.nanoTime();

    long duration = endTime - startTime;
    long qps = count * 1000000000L / duration;
    System.out.println("Requests per second: " + qps);
}
 
Example #7
Source File: Locator.java    From FXMaps with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns a {@link Location} object with location information which may
 * not have very strictly accurate information.
 * 
 * @param ipStr         the IP Address for which a {@link Location} will be obtained.
 * @return
 * @throws Exception
 */
public static Location getIPLocation(String ipStr) throws Exception {
    System.out.println("gres = " + Locator.class.getClassLoader().getResource("GeoLite2-City.mmdb"));
    InputStream is = Locator.class.getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
    DatabaseReader reader = new DatabaseReader.Builder(is).build();
    CityResponse response = reader.city(InetAddress.getByName(ipStr));

    System.out.println("City " +response.getCity());
    System.out.println("ZIP Code " +response.getPostal().getCode());
    System.out.println("Country " +response.getCountry());
    System.out.println("Location " +response.getLocation());
    
    return new Location(response.getCity().toString(), response.getPostal().getCode(), 
        response.getCountry().toString(), response.getLocation().getTimeZone(), response.getLocation().getLatitude(), 
            response.getLocation().getLongitude(), response.getPostal().getConfidence(),
                response.getLocation().getAccuracyRadius(), response.getLocation().getPopulationDensity(),
                    response.getLocation().getAverageIncome());
}
 
Example #8
Source File: SessionDetailsFilter.java    From spring-session with Apache License 2.0 6 votes vote down vote up
String getGeoLocation(String remoteAddr) {
	try {
		CityResponse city = this.reader.city(InetAddress.getByName(remoteAddr));
		String cityName = city.getCity().getName();
		String countryName = city.getCountry().getName();
		if (cityName == null && countryName == null) {
			return null;
		}
		else if (cityName == null) {
			return countryName;
		}
		else if (countryName == null) {
			return cityName;
		}
		return cityName + ", " + countryName;
	}
	catch (Exception ex) {
		return UNKNOWN;

	}
}
 
Example #9
Source File: GeoIpProcessor.java    From sawmill with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(CityResponse response) {
    Double longitude = response.getLocation().getLongitude();
    Double latitude = response.getLocation().getLatitude();
    if (longitude == null || latitude == null) {
        return null;
    }
    return Arrays.asList(longitude, latitude);
}
 
Example #10
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 #11
Source File: RawDBDemoGeoIPLocationService.java    From tutorials with MIT License 5 votes vote down vote up
public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
    InetAddress ipAddress = InetAddress.getByName(ip);
    CityResponse response = dbReader.city(ipAddress);

    String cityName = response.getCity().getName();
    String latitude = response.getLocation().getLatitude().toString();
    String longitude = response.getLocation().getLongitude().toString();
    return new GeoIP(ip, cityName, latitude, longitude);
}
 
Example #12
Source File: GeoEnrichTestUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static CityResponse getFullCityResponse() throws Exception {
    // Taken from MaxMind unit tests.
    final String maxMindCityResponse = "{\"city\":{\"confidence\":76,"
            + "\"geoname_id\":9876,\"names\":{\"en\":\"Minneapolis\""
            + "}},\"continent\":{\"code\":\"NA\","
            + "\"geoname_id\":42,\"names\":{" + "\"en\":\"North America\""
            + "}},\"country\":{\"confidence\":99,"
            + "\"iso_code\":\"US\",\"geoname_id\":1,\"names\":{"
            + "\"en\":\"United States of America\"" + "}" + "},"
            + "\"location\":{" + "\"accuracy_radius\":1500,"
            + "\"latitude\":44.98," + "\"longitude\":93.2636,"
            + "\"metro_code\":765," + "\"time_zone\":\"America/Chicago\""
            + "}," + "\"postal\":{\"confidence\": 33, \"code\":\"55401\"},"
            + "\"registered_country\":{" + "\"geoname_id\":2,"
            + "\"iso_code\":\"CA\"," + "\"names\":{" + "\"en\":\"Canada\""
            + "}" + "}," + "\"represented_country\":{" + "\"geoname_id\":3,"
            + "\"iso_code\":\"GB\"," + "\"names\":{"
            + "\"en\":\"United Kingdom\"" + "}," + "\"type\":\"C<military>\""
            + "}," + "\"subdivisions\":[{" + "\"confidence\":88,"
            + "\"geoname_id\":574635," + "\"iso_code\":\"MN\"," + "\"names\":{"
            + "\"en\":\"Minnesota\"" + "}" + "}," + "{\"iso_code\":\"TT\"}],"
            + "\"traits\":{" + "\"autonomous_system_number\":1234,"
            + "\"autonomous_system_organization\":\"AS Organization\","
            + "\"domain\":\"example.com\"," + "\"ip_address\":\"1.2.3.4\","
            + "\"is_anonymous_proxy\":true,"
            + "\"is_satellite_provider\":true," + "\"isp\":\"Comcast\","
            + "\"organization\":\"Blorg\"," + "\"user_type\":\"college\""
            + "}," + "\"maxmind\":{\"queries_remaining\":11}" + "}";

    InjectableValues inject = new InjectableValues.Std().addValue("locales", Collections.singletonList("en"));
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return new ObjectMapper().reader(CityResponse.class).with(inject).readValue(maxMindCityResponse);
}
 
Example #13
Source File: GeoEnrichTestUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static CityResponse getNullLatAndLongCityResponse() throws Exception {
    // Taken from MaxMind unit tests and modified.
    final String maxMindCityResponse = "{" + "\"city\":{" + "\"confidence\":76,"
            + "\"geoname_id\":9876," + "\"names\":{" + "\"en\":\"Minneapolis\""
            + "}" + "}," + "\"continent\":{" + "\"code\":\"NA\","
            + "\"geoname_id\":42," + "\"names\":{" + "\"en\":\"North America\""
            + "}" + "}," + "\"country\":{" + "\"confidence\":99,"
            + "\"iso_code\":\"US\"," + "\"geoname_id\":1," + "\"names\":{"
            + "\"en\":\"United States of America\"" + "}" + "},"
            + "\"location\":{" + "\"accuracy_radius\":1500,"
            + "\"metro_code\":765," + "\"time_zone\":\"America/Chicago\""
            + "}," + "\"postal\":{\"confidence\": 33, \"code\":\"55401\"},"
            + "\"registered_country\":{" + "\"geoname_id\":2,"
            + "\"iso_code\":\"CA\"," + "\"names\":{" + "\"en\":\"Canada\""
            + "}" + "}," + "\"represented_country\":{" + "\"geoname_id\":3,"
            + "\"iso_code\":\"GB\"," + "\"names\":{"
            + "\"en\":\"United Kingdom\"" + "}," + "\"type\":\"C<military>\""
            + "}," + "\"subdivisions\":[{" + "\"confidence\":88,"
            + "\"geoname_id\":574635," + "\"iso_code\":\"MN\"," + "\"names\":{"
            + "\"en\":\"Minnesota\"" + "}" + "}," + "{\"iso_code\":\"TT\"}],"
            + "\"traits\":{" + "\"autonomous_system_number\":1234,"
            + "\"autonomous_system_organization\":\"AS Organization\","
            + "\"domain\":\"example.com\"," + "\"ip_address\":\"1.2.3.4\","
            + "\"is_anonymous_proxy\":true,"
            + "\"is_satellite_provider\":true," + "\"isp\":\"Comcast\","
            + "\"organization\":\"Blorg\"," + "\"user_type\":\"college\""
            + "}," + "\"maxmind\":{\"queries_remaining\":11}" + "}";

    InjectableValues inject = new InjectableValues.Std().addValue("locales", Collections.singletonList("en"));
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return new ObjectMapper().reader(CityResponse.class).with(inject).readValue(maxMindCityResponse);
}
 
Example #14
Source File: TestGeoEnrichIP.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationship() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");

    final CityResponse cityResponse = getFullCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip.geo.city"));
    assertEquals("44.98", finishedFound.getAttribute("ip.geo.latitude"));
    assertEquals("93.2636", finishedFound.getAttribute("ip.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip.geo.postalcode"));
}
 
Example #15
Source File: TestGeoEnrichIP.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationshipWhenLatAndLongAreNotSet() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");

    final CityResponse cityResponse = getNullLatAndLongCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip.geo.city"));
    assertNull(finishedFound.getAttribute("ip.geo.latitude"));
    assertNull(finishedFound.getAttribute("ip.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip.geo.postalcode"));
}
 
Example #16
Source File: DslRecordMapping.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
GeoIpValueProducer(final ValueProducer<InetAddress> source, final LookupService service) {
    super("ip2geo(" + source.identifier + ")",
          CityResponse.class,
          (e,c) -> source.produce(e, c).flatMap((address) -> {
                try {
                    return service.lookup(address);
                } catch (final ClosedServiceException ex) {
                    return Optional.empty();
                }
            }),
          true);
}
 
Example #17
Source File: TestGeoEnrichIP.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void evaluatingExpressionLanguageShouldAndFindingIpFieldWithSuccessfulLookUpShouldFlowToFoundRelationship() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "${ip.fields:substringBefore(',')}");

    final CityResponse cityResponse = getNullLatAndLongCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip.fields", "ip0,ip1,ip2");
    attributes.put("ip0", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip0.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip0.geo.city"));
    assertNull(finishedFound.getAttribute("ip0.geo.latitude"));
    assertNull(finishedFound.getAttribute("ip0.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip0.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip0.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip0.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip0.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip0.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip0.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip0.geo.postalcode"));
}
 
Example #18
Source File: ExternalDatabaseLookupService.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CityResponse> lookup(final InetAddress address) throws ClosedServiceException {
    final DatabaseLookupService service = databaseLookupService.get();
    if (null == service) {
        throw new ClosedServiceException(this);
    }
    try {
        return service.lookup(address);
    } catch (final ClosedServiceException e) {
        // We accidentally performed a lookup using an underlying service that was closed
        // between us fetching it and using it. Retry via recursion.
        return lookup(address);
    }
}
 
Example #19
Source File: GeoIPCityDissector.java    From logparser with Apache License 2.0 5 votes vote down vote up
public void dissect(final Parsable<?> parsable, final String inputname, final InetAddress ipAddress) throws DissectionFailure {
    // City is the 'Country' + more details.
    CityResponse response;
    try {
        response = reader.city(ipAddress);
    } catch (IOException | GeoIp2Exception e) {
        return;
    }

    extractCountryFields(parsable, inputname, response);
    extractCityFields(parsable, inputname, response);
}
 
Example #20
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationship() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");

    final CityResponse cityResponse = getFullCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip.geo.city"));
    assertEquals("44.98", finishedFound.getAttribute("ip.geo.latitude"));
    assertEquals("93.2636", finishedFound.getAttribute("ip.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip.geo.postalcode"));
}
 
Example #21
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationshipWhenLatAndLongAreNotSet() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");

    final CityResponse cityResponse = getNullLatAndLongCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip.geo.city"));
    assertNull(finishedFound.getAttribute("ip.geo.latitude"));
    assertNull(finishedFound.getAttribute("ip.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip.geo.postalcode"));
}
 
Example #22
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void evaluatingExpressionLanguageShouldAndFindingIpFieldWithSuccessfulLookUpShouldFlowToFoundRelationship() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "${ip.fields:substringBefore(',')}");

    final CityResponse cityResponse = getNullLatAndLongCityResponse();

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip.fields", "ip0,ip1,ip2");
    attributes.put("ip0", "1.2.3.4");

    testRunner.enqueue(new byte[0], attributes);

    testRunner.run();

    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(GeoEnrichIP.REL_FOUND);

    assertEquals(0, notFound.size());
    assertEquals(1, found.size());

    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip0.geo.lookup.micros"));
    assertEquals("Minneapolis", finishedFound.getAttribute("ip0.geo.city"));
    assertNull(finishedFound.getAttribute("ip0.geo.latitude"));
    assertNull(finishedFound.getAttribute("ip0.geo.longitude"));
    assertEquals("Minnesota", finishedFound.getAttribute("ip0.geo.subdivision.0"));
    assertEquals("MN", finishedFound.getAttribute("ip0.geo.subdivision.isocode.0"));
    assertNull(finishedFound.getAttribute("ip0.geo.subdivision.1"));
    assertEquals("TT", finishedFound.getAttribute("ip0.geo.subdivision.isocode.1"));
    assertEquals("United States of America", finishedFound.getAttribute("ip0.geo.country"));
    assertEquals("US", finishedFound.getAttribute("ip0.geo.country.isocode"));
    assertEquals("55401", finishedFound.getAttribute("ip0.geo.postalcode"));
}
 
Example #23
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private CityResponse getFullCityResponse() throws Exception {
    // Taken from MaxMind unit tests.
    final String maxMindCityResponse = "{" + "\"city\":{" + "\"confidence\":76,"
        + "\"geoname_id\":9876," + "\"names\":{" + "\"en\":\"Minneapolis\""
        + "}" + "}," + "\"continent\":{" + "\"code\":\"NA\","
        + "\"geoname_id\":42," + "\"names\":{" + "\"en\":\"North America\""
        + "}" + "}," + "\"country\":{" + "\"confidence\":99,"
        + "\"iso_code\":\"US\"," + "\"geoname_id\":1," + "\"names\":{"
        + "\"en\":\"United States of America\"" + "}" + "},"
        + "\"location\":{" + "\"accuracy_radius\":1500,"
        + "\"latitude\":44.98," + "\"longitude\":93.2636,"
        + "\"metro_code\":765," + "\"time_zone\":\"America/Chicago\""
        + "}," + "\"postal\":{\"confidence\": 33, \"code\":\"55401\"},"
        + "\"registered_country\":{" + "\"geoname_id\":2,"
        + "\"iso_code\":\"CA\"," + "\"names\":{" + "\"en\":\"Canada\""
        + "}" + "}," + "\"represented_country\":{" + "\"geoname_id\":3,"
        + "\"iso_code\":\"GB\"," + "\"names\":{"
        + "\"en\":\"United Kingdom\"" + "}," + "\"type\":\"C<military>\""
        + "}," + "\"subdivisions\":[{" + "\"confidence\":88,"
        + "\"geoname_id\":574635," + "\"iso_code\":\"MN\"," + "\"names\":{"
        + "\"en\":\"Minnesota\"" + "}" + "}," + "{\"iso_code\":\"TT\"}],"
        + "\"traits\":{" + "\"autonomous_system_number\":1234,"
        + "\"autonomous_system_organization\":\"AS Organization\","
        + "\"domain\":\"example.com\"," + "\"ip_address\":\"1.2.3.4\","
        + "\"is_anonymous_proxy\":true,"
        + "\"is_satellite_provider\":true," + "\"isp\":\"Comcast\","
        + "\"organization\":\"Blorg\"," + "\"user_type\":\"college\""
        + "}," + "\"maxmind\":{\"queries_remaining\":11}" + "}";

    InjectableValues inject = new InjectableValues.Std().addValue("locales", Collections.singletonList("en"));
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return new ObjectMapper().reader(CityResponse.class).with(inject).readValue(maxMindCityResponse);
}
 
Example #24
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private CityResponse getNullLatAndLongCityResponse() throws Exception {
    // Taken from MaxMind unit tests and modified.
    final String maxMindCityResponse = "{" + "\"city\":{" + "\"confidence\":76,"
        + "\"geoname_id\":9876," + "\"names\":{" + "\"en\":\"Minneapolis\""
        + "}" + "}," + "\"continent\":{" + "\"code\":\"NA\","
        + "\"geoname_id\":42," + "\"names\":{" + "\"en\":\"North America\""
        + "}" + "}," + "\"country\":{" + "\"confidence\":99,"
        + "\"iso_code\":\"US\"," + "\"geoname_id\":1," + "\"names\":{"
        + "\"en\":\"United States of America\"" + "}" + "},"
        + "\"location\":{" + "\"accuracy_radius\":1500,"
        + "\"metro_code\":765," + "\"time_zone\":\"America/Chicago\""
        + "}," + "\"postal\":{\"confidence\": 33, \"code\":\"55401\"},"
        + "\"registered_country\":{" + "\"geoname_id\":2,"
        + "\"iso_code\":\"CA\"," + "\"names\":{" + "\"en\":\"Canada\""
        + "}" + "}," + "\"represented_country\":{" + "\"geoname_id\":3,"
        + "\"iso_code\":\"GB\"," + "\"names\":{"
        + "\"en\":\"United Kingdom\"" + "}," + "\"type\":\"C<military>\""
        + "}," + "\"subdivisions\":[{" + "\"confidence\":88,"
        + "\"geoname_id\":574635," + "\"iso_code\":\"MN\"," + "\"names\":{"
        + "\"en\":\"Minnesota\"" + "}" + "}," + "{\"iso_code\":\"TT\"}],"
        + "\"traits\":{" + "\"autonomous_system_number\":1234,"
        + "\"autonomous_system_organization\":\"AS Organization\","
        + "\"domain\":\"example.com\"," + "\"ip_address\":\"1.2.3.4\","
        + "\"is_anonymous_proxy\":true,"
        + "\"is_satellite_provider\":true," + "\"isp\":\"Comcast\","
        + "\"organization\":\"Blorg\"," + "\"user_type\":\"college\""
        + "}," + "\"maxmind\":{\"queries_remaining\":11}" + "}";

    InjectableValues inject = new InjectableValues.Std().addValue("locales", Collections.singletonList("en"));
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return new ObjectMapper().reader(CityResponse.class).with(inject).readValue(maxMindCityResponse);
}
 
Example #25
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 #26
Source File: StreamProcessorsSession.java    From proxylive with MIT License 5 votes vote down vote up
public synchronized ClientInfo manage(IStreamProcessor iStreamProcessor, HttpServletRequest request) throws UnknownHostException {
    ClientInfo client = new ClientInfo();
    request.getQueryString();
    MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUriString(ProxyLiveUtils.getURL(request)).build().getQueryParams();
    String clientUser = parameters.getFirst("user");
    if (clientUser == null || clientUser.trim().equals("null")) {
        clientUser = "guest";
    }
    client.setClientUser(clientUser);
    client.setIp(InetAddress.getByName(ProxyLiveUtils.getRequestIP(request)));
    client.setBrowserInfo(ProxyLiveUtils.getBrowserInfo(request));
    client = addClientInfo(client);
    if (geoIPService.isServiceEnabled()) {
        try {
            DatabaseReader geoDBReader = geoIPService.geoGEOInfoReader();
            CityResponse cityResponse = geoDBReader.city(client.getIp());

            if (cityResponse.getLocation() != null) {
                GEOInfo geoInfo = new GEOInfo();
                geoInfo.setCity(cityResponse.getCity());
                geoInfo.setCountry(cityResponse.getCountry());
                geoInfo.setLocation(cityResponse.getLocation());
                client.setGeoInfo(geoInfo);
            }
        }catch(AddressNotFoundException anfe){
        }catch(Exception ex ){
            logger.error("Error parsing user geodata", ex);
        }
    }
    if (!client.getStreams().contains(iStreamProcessor)) {
        client.getStreams().add(iStreamProcessor);
    }
    return client;
}
 
Example #27
Source File: StatisticsLocationUtil.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> ip2SpatialData(InetAddress ip) {
    if (!enabled) {
        return null;
    }
    if (reader == null) {
        LOG.warn("Location database is not initialized. Exiting.");
        return null;
    }
    try {
        Map<String, Object> holder = new HashMap<>(3);
        if (dbType == LocationDatabaseType.COUNTRY) {
            Country country = reader.country(ip).getCountry();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), country.getIsoCode());
        } else {
            CityResponse city = reader.city(ip);
            Location loc = city.getLocation();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), city.getCountry().getIsoCode());
            holder.put(ObjectEsParameterFactory.GEOLOC_CITY_NAME.getName(), city.getCity().getName());
            holder.put(ObjectEsParameterFactory.GEOLOC_GEO_POINT.getName(),
                       new GeoPoint(loc.getLatitude(), loc.getLongitude()));
        }
        return holder;
    } catch (Throwable e) {
        LOG.warn("Can't convert IP to GeoIp", e);
    }
    return null;
}
 
Example #28
Source File: IpAddressService.java    From springboot-learn with MIT License 5 votes vote down vote up
public String getAddressByIp(String ipAddress) {
    try {
        CityResponse response = reader.city(InetAddress.getByName(ipAddress));
        return response.getMostSpecificSubdivision().getNames().get("zh-CN");
    } catch (Exception e) {
        logger.error("根据IP[{}]获取省份失败:{}", ipAddress, e.getMessage());
        return null;
    }
}
 
Example #29
Source File: GeoService.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addLocRecord(final LocRecord record) {
	if (record.isValid()) {
		final CityResponse old = _locRecords.put(record.getOwner(), record.getLocation());
		if (old != null) {
			final LocRecord o = _rawLocRecordsMap.remove(record.getOwner());
			_rawLocRecords.remove(o);
		}
	}
	_rawLocRecords.add(record);
	_rawLocRecordsMap.put(record.getOwner(), record);
}
 
Example #30
Source File: GeoEnrichIPRecord.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile input = session.get();
    if (input == null) {
        return;
    }

    FlowFile output = session.create(input);
    FlowFile notFound = splitOutput ? session.create(input) : null;
    final DatabaseReader dbReader = databaseReaderRef.get();
    try (InputStream is = session.read(input);
         OutputStream os = session.write(output);
         OutputStream osNotFound = splitOutput ? session.write(notFound) : null) {
        RecordPathCache cache = new RecordPathCache(GEO_PROPERTIES.size() + 1);
        Map<PropertyDescriptor, RecordPath> paths = new HashMap<>();
        for (PropertyDescriptor descriptor : GEO_PROPERTIES) {
            if (!context.getProperty(descriptor).isSet()) {
                continue;
            }
            String rawPath = context.getProperty(descriptor).evaluateAttributeExpressions(input).getValue();
            RecordPath compiled = cache.getCompiled(rawPath);
            paths.put(descriptor, compiled);
        }

        String rawIpPath = context.getProperty(IP_RECORD_PATH).evaluateAttributeExpressions(input).getValue();
        RecordPath ipPath = cache.getCompiled(rawIpPath);

        RecordReader reader = readerFactory.createRecordReader(input, is, getLogger());
        RecordSchema schema = writerFactory.getSchema(input.getAttributes(), reader.getSchema());
        RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, os);
        RecordSetWriter notFoundWriter = splitOutput ? writerFactory.createWriter(getLogger(), schema, osNotFound) : null;
        Record record;
        Relationship targetRelationship = REL_NOT_FOUND;
        writer.beginRecordSet();

        if (notFoundWriter != null) {
            notFoundWriter.beginRecordSet();
        }

        int foundCount = 0;
        int notFoundCount = 0;
        while ((record = reader.nextRecord()) != null) {
            CityResponse response = geocode(ipPath, record, dbReader);
            boolean wasEnriched = enrichRecord(response, record, paths);
            if (wasEnriched) {
                targetRelationship = REL_FOUND;
            }
            if (!splitOutput || (splitOutput && wasEnriched)) {
                writer.write(record);
                foundCount++;
            } else {
                notFoundWriter.write(record);
                notFoundCount++;
            }
        }
        writer.finishRecordSet();
        writer.close();

        if (notFoundWriter != null) {
            notFoundWriter.finishRecordSet();
            notFoundWriter.close();
        }

        is.close();
        os.close();
        if (osNotFound != null) {
            osNotFound.close();
        }

        output = session.putAllAttributes(output, buildAttributes(foundCount, writer.getMimeType()));
        if (!splitOutput) {
            session.transfer(output, targetRelationship);
            session.remove(input);
        } else {
            if (notFoundCount > 0) {
                notFound = session.putAllAttributes(notFound, buildAttributes(notFoundCount, writer.getMimeType()));
                session.transfer(notFound, REL_NOT_FOUND);
            } else {
                session.remove(notFound);
            }
            session.transfer(output, REL_FOUND);
            session.transfer(input, REL_ORIGINAL);
            session.getProvenanceReporter().modifyContent(notFound);
        }
        session.getProvenanceReporter().modifyContent(output);
    } catch (Exception ex) {
        getLogger().error("Error enriching records.", ex);
        session.rollback();
        context.yield();
    }
}