com.maxmind.geoip2.exception.AddressNotFoundException Java Examples

The following examples show how to use com.maxmind.geoip2.exception.AddressNotFoundException. 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: GeoIpOperationTest.java    From bender with Apache License 2.0 6 votes vote down vote up
@Test(expected = AddressNotFoundException.class)
public void testUnkownIpRequired() throws Throwable {
  GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true);

  DummpyMapEvent devent = new DummpyMapEvent();
  devent.setField("ip_address", "10.0.0.1");

  InternalEvent ievent = new InternalEvent("", null, 0);
  ievent.setEventObj(devent);

  try {
    op.perform(ievent);
  } catch (OperationException e) {
    throw e.getCause();
  }
}
 
Example #2
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 #3
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 6 votes vote down vote up
/**
 * @param ipAddress    IPv4 or IPv6 address to lookup.
 * @param cls          The class to deserialize to.
 * @param expectedType The expected database type.
 * @param stackDepth   Used to work out how far down the stack we should look, for the method name
 *                     we should use to report back to the user when in error. If this is called directly from the
 *                     method to report to the use set to zero, if this is called indirectly then it is the number of
 *                     methods between this method and the method to report the name of.
 * @return A <T> object with the data for the IP address or null if the IP address is not in our database
 * @throws IOException if there is an error opening or reading from the file.
 */
private <T> Optional<T> get(InetAddress ipAddress, Class<T> cls,
                            DatabaseType expectedType, int stackDepth) throws IOException, AddressNotFoundException {

    if ((databaseType & expectedType.type) == 0) {
        String caller = Thread.currentThread().getStackTrace()[2 + stackDepth]
                .getMethodName();
        throw new UnsupportedOperationException(
                "Invalid attempt to open a " + getMetadata().getDatabaseType()
                        + " database using the " + caller + " method");
    }

    // We are using the fully qualified name as otherwise it is ambiguous
    // on Java 14 due to the new java.lang.Record.
    com.maxmind.db.Record record = reader.getRecord(ipAddress);

    ObjectNode node = jsonNodeToObjectNode(record.getData());

    if (node == null) {
        return Optional.empty();
    }

    InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress(), record.getNetwork());

    return Optional.of(this.om.reader(inject).treeToValue(node, cls));
}
 
Example #4
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 #5
Source File: DatabaseReader.java    From GeoIP2-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param ipAddress    IPv4 or IPv6 address to lookup.
 * @param cls          The class to deserialize to.
 * @param expectedType The expected database type.
 * @return A <T> object with the data for the IP address
 * @throws IOException              if there is an error opening or reading from the file.
 * @throws AddressNotFoundException if the IP address is not in our database
 */
private <T> T getOrThrowException(InetAddress ipAddress, Class<T> cls,
                                  DatabaseType expectedType) throws IOException, AddressNotFoundException {
    Optional<T> t = get(ipAddress, cls, expectedType, 1);
    if (!t.isPresent()) {
        throw new AddressNotFoundException("The address "
                + ipAddress.getHostAddress() + " is not in the database.");
    }

    return t.get();
}
 
Example #6
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"));
}