com.maxmind.geoip2.exception.GeoIp2Exception Java Examples

The following examples show how to use com.maxmind.geoip2.exception.GeoIp2Exception. 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: InsightsResponseTest.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsInEuropeanUnion() throws IOException, GeoIp2Exception {
    // This uses an alternate fixture where we have the
    // is_in_european_union flag set in locations not set in the other
    // fixture.
    WebServiceClient client = new WebServiceClient.Builder(6, "0123456789")
            .host("localhost")
            .port(this.wireMockRule.port())
            .disableHttps()
            .build();

    InsightsResponse insights = client.insights(
            InetAddress.getByName("1.1.1.2"));

    assertTrue("getCountry().isInEuropeanUnion() does not return true",
            insights.getCountry().isInEuropeanUnion());
    assertTrue(
            "getRegisteredCountry().() isInEuropeanUnion = does not return true",
            insights.getRegisteredCountry().isInEuropeanUnion());
}
 
Example #2
Source File: InsightsResponseTest.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
@Before
public void createClient() throws IOException, GeoIp2Exception,
        URISyntaxException {
    stubFor(get(urlEqualTo("/geoip/v2.1/insights/1.1.1.1"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/vnd.maxmind.com-insights+json; charset=UTF-8; version=2.1")
                    .withBody(readJsonFile("insights0"))));
    stubFor(get(urlEqualTo("/geoip/v2.1/insights/1.1.1.2"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/vnd.maxmind.com-insights+json; charset=UTF-8; version=2.1")
                    .withBody(readJsonFile("insights1"))));

    WebServiceClient client = new WebServiceClient.Builder(6, "0123456789")
            .host("localhost")
            .port(this.wireMockRule.port())
            .disableHttps()
            .build();

    this.insights = client.insights(InetAddress.getByName("1.1.1.1"));
}
 
Example #3
Source File: CountryResponseTest.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
@Before
public void createClient() throws IOException, GeoIp2Exception,
        URISyntaxException {
    stubFor(get(urlEqualTo("/geoip/v2.1/country/1.1.1.1"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/vnd.maxmind.com-country+json; charset=UTF-8; version=2.1")
                    .withBody(readJsonFile("country0"))));

    WebServiceClient client = new WebServiceClient.Builder(6, "0123456789")
            .host("localhost")
            .port(this.wireMockRule.port())
            .disableHttps()
            .build();

    country = client.country(InetAddress.getByName("1.1.1.1"));
}
 
Example #4
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 #5
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 #6
Source File: TestGeoEnrichIP.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void shouldFlowToNotFoundWhenGeoIp2ExceptionThrownFromMaxMind() throws Exception {
    testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");

    when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenThrow(GeoIp2Exception.class);

    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(1, notFound.size());
    assertEquals(0, found.size());
}
 
Example #7
Source File: MaxMindGeoLocationService.java    From prebid-server-java with Apache License 2.0 6 votes vote down vote up
@Override
public Future<GeoInfo> lookup(String ip, Timeout timeout) {
    if (databaseReader == null) {
        return Future.failedFuture("Geo location database file hasn't been downloaded yet, try again later");
    }

    try {
        final InetAddress inetAddress = InetAddress.getByName(ip);
        return Future.succeededFuture(GeoInfo.builder()
                .vendor(VENDOR)
                .continent(getCity(inetAddress).getContinent().getCode().toLowerCase())
                .country(getCity(inetAddress).getCountry().getIsoCode().toLowerCase())
                .region(getRegionCode(inetAddress))
                //metro code is skipped as Max Mind uses Google's version (Nielsen DMAs required)
                .city(getCity(inetAddress).getCity().getName())
                .lat(getLocation(inetAddress).getLatitude().floatValue())
                .lon(getLocation(inetAddress).getLongitude().floatValue())
                .build());
    } catch (IOException | GeoIp2Exception e) {
        return Future.failedFuture(e);
    }
}
 
Example #8
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 #9
Source File: CityResponseTest.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
@Before
public void createClient() throws IOException, GeoIp2Exception,
        URISyntaxException {
    stubFor(get(urlEqualTo("/geoip/v2.1/city/1.1.1.2"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/vnd.maxmind.com-city+json; charset=UTF-8; version=2.1")
                    .withBody(readJsonFile("city0"))));
}
 
Example #10
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 #11
Source File: GeoIPASNDissector.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 {
    AsnResponse response;
    try {
        response = reader.asn(ipAddress);
    } catch (IOException | GeoIp2Exception e) {
        return;
    }

    extractAsnFields(parsable, inputname, response);
}
 
Example #12
Source File: GeoIPCountryDissector.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 {
    CountryResponse response;
    try {
        response = reader.country(ipAddress);
    } catch (IOException | GeoIp2Exception e) {
        return;
    }
    extractCountryFields(parsable, inputname, response);
}
 
Example #13
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 #14
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 #15
Source File: Benchmark.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws GeoIp2Exception, IOException {
    File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb");
    System.out.println("No caching");
    loop("Warming up", file, WARMUPS, NoCache.getInstance());
    loop("Benchmarking", file, BENCHMARKS, NoCache.getInstance());

    System.out.println("With caching");
    loop("Warming up", file, WARMUPS, new CHMCache());
    loop("Benchmarking", file, BENCHMARKS, new CHMCache());
}
 
Example #16
Source File: Benchmark.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
private static void loop(String msg, File file, int loops, NodeCache cache) throws GeoIp2Exception, IOException {
    System.out.println(msg);
    for (int i = 0; i < loops; i++) {
        DatabaseReader r = new DatabaseReader.Builder(file).fileMode(FileMode.MEMORY_MAPPED).withCache(cache).build();
        bench(r, COUNT, i);
    }
    System.out.println();
}
 
Example #17
Source File: GeoIPISPDissector.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 {
    IspResponse response;
    try {
        response = reader.isp(ipAddress);
    } catch (IOException | GeoIp2Exception e) {
        return;
    }

    extractAsnFields(parsable, inputname, response);
    extractIspFields(parsable, inputname, response);
}
 
Example #18
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
private void unknownAddress(DatabaseReader reader) throws IOException,
        GeoIp2Exception {
    assertFalse(reader.tryCity(InetAddress.getByName("10.10.10.10")).isPresent());

    this.exception.expect(AddressNotFoundException.class);
    this.exception
            .expectMessage(containsString("The address 10.10.10.10 is not in the database."));

    reader.city(InetAddress.getByName("10.10.10.10"));
}
 
Example #19
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<AsnResponse> tryAsn(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.get(ipAddress, AsnResponse.class, DatabaseType.ASN, 0);
}
 
Example #20
Source File: ReadLine.java    From storm-nginx-log with MIT License 4 votes vote down vote up
public static void main(String[] argv) throws IOException, ParseException {
    String record = Files.readAllLines(Paths.get(srcFile)).get(0);
    String regx = "([^ ]*) ([^ ]*) ([^ ]*) (\\[.*\\]) (\\\".*?\\\") (-|[0-9]*) (-|[0-9]*) (\\\".*?\\\") (\\\".*?\\\")";
    Pattern pattern = Pattern.compile(regx);
    Matcher matcher = pattern.matcher(record);

    if (matcher.find()) {
        String http_user_agent = matcher.group(9);
        System.out.println(http_user_agent);

        String system = http_user_agent.split(" ")[2];
        System.out.println(system);
    } else {
        System.out.println("NO MATCH");
    }

    String record1 = "http://blog.fenlan96.com/2017/11/30/%E5%88%86%E5%B8%83%E5%BC%8F%E8%AE%A1%E7%AE%97/";
    String regx1 = "([^/]*)(\\/\\/[^/]*\\/)([^ ]*)";
    Pattern pattern1 = Pattern.compile(regx1);
    Matcher matcher1 = pattern1.matcher(record1);
    if (matcher1.find()) {
        System.out.println("ok");
        System.out.println(matcher1.group(2));
    } else {
        System.out.println(regx1 + " failed");
    }

    System.out.println(UserAgent.browserRegx(record1));

    try {
        System.out.println(AnalyzeIP.cityOfIP("23.83.230.171"));
    } catch (GeoIp2Exception e) {
        System.out.println("ip 没有找到");
    }

    String time_local = matcher.group(4).substring(1, matcher.group(4).length()-1);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH);
    LocalDateTime dateTime = LocalDateTime.parse(time_local, formatter);
    System.out.println("date : " + dateTime + " " + dateTime.getMonthValue() + " " + dateTime.getDayOfMonth());
    System.out.println(20180208 / 10000);
    System.out.println(20180208 % 100);
    System.out.println((20180208 - 20180208 / 10000 * 10000) / 100);

    System.out.println(DataAnalyze.getNetFileSizeDescription(15));
}
 
Example #21
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<ConnectionTypeResponse> tryConnectionType(InetAddress ipAddress)
        throws IOException, GeoIp2Exception {
    return this.get(ipAddress, ConnectionTypeResponse.class,
            DatabaseType.CONNECTION_TYPE, 0);
}
 
Example #22
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<IspResponse> tryIsp(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.get(ipAddress, IspResponse.class, DatabaseType.ISP, 0);
}
 
Example #23
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
private void testDefaultLocale(DatabaseReader reader) throws IOException,
        GeoIp2Exception {
    CityResponse city = reader.city(InetAddress.getByName("81.2.69.160"));
    assertEquals("London", city.getCity().getName());
}
 
Example #24
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
private void testLocaleList(DatabaseReader reader) throws IOException,
        GeoIp2Exception {
    CityResponse city = reader.city(InetAddress.getByName("81.2.69.160"));
    assertEquals("Лондон", city.getCity().getName());
}
 
Example #25
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
private void testMemoryMode(DatabaseReader reader) throws IOException,
        GeoIp2Exception {
    CityResponse city = reader.city(InetAddress.getByName("81.2.69.160"));
    assertEquals("London", city.getCity().getName());
    assertEquals(100, city.getLocation().getAccuracyRadius().longValue());
}
 
Example #26
Source File: DatabaseReaderTest.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
private void hasIpInfo(DatabaseReader reader) throws IOException,
        GeoIp2Exception {
    CityResponse cio = reader.city(InetAddress.getByName("81.2.69.160"));
    assertEquals("81.2.69.160", cio.getTraits().getIpAddress());
    assertEquals("81.2.69.160/27", cio.getTraits().getNetwork().toString());
}
 
Example #27
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<EnterpriseResponse> tryEnterprise(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.get(ipAddress, EnterpriseResponse.class, DatabaseType.ENTERPRISE, 0);
}
 
Example #28
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public CityResponse city(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.getOrThrowException(ipAddress, CityResponse.class, DatabaseType.CITY);
}
 
Example #29
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<CountryResponse> tryCountry(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.get(ipAddress, CountryResponse.class, DatabaseType.COUNTRY, 0);
}
 
Example #30
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<AnonymousIpResponse> tryAnonymousIp(InetAddress ipAddress) throws IOException,
        GeoIp2Exception {
    return this.get(ipAddress, AnonymousIpResponse.class, DatabaseType.ANONYMOUS_IP, 0);
}