Example #1
0
  public String lookupCity(String ip) {
    CityResponse response = null;

    try {
      response = reader.city(InetAddress.getByName(ip));
    } catch (Exception e) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("No city found for: " + ip);
      }
      return null;
    }

    return response.getCity().getName();
  }
Example #2
0
  public City geolocAndGetCity(final String customerRemoteAddr) throws Exception {
    try {
      final InetAddress address = InetAddress.getByName(customerRemoteAddr);

      final DatabaseReader databaseReader = new DatabaseReader.Builder(getCityDataBase()).build();

      final CityResponse cityResponse = databaseReader.city(address);
      if (cityResponse != null) {
        return cityResponse.getCity();
      }
    } catch (AddressNotFoundException e) {
      logger.warn("Geoloc city, can't find this address:" + customerRemoteAddr);
    } catch (FileNotFoundException e) {
      logger.error("Geoloc city, can't find database MaxMind", e);
    } catch (Exception e) {
      logger.error("Geoloc city, can't find this city with this address:" + customerRemoteAddr, e);
    }
    return null;
  }
  public static LocationInfo getLocationInfo(String ipAddr) {

    CityResponse rsp;

    try {
      rsp = dbReader.city(InetAddress.getByName(ipAddr));

      return new LocationInfo(
          rsp.getCity().getName(),
          rsp.getCountry().getName(),
          rsp.getLocation().getLatitude(),
          rsp.getLocation().getLongitude());
    } catch (UnknownHostException e) {
      logger.error(e.getMessage());
    } catch (IOException e) {
      logger.error(e.getMessage());
    } catch (GeoIp2Exception e) {
      logger.error(e.getMessage());
    }

    return null;
  }