Skip to content

Instantly share code, notes, and snippets.

@loinguyenduc101
Last active November 26, 2015 03:30
Show Gist options
  • Save loinguyenduc101/ab9b46724369b969c707 to your computer and use it in GitHub Desktop.
Save loinguyenduc101/ab9b46724369b969c707 to your computer and use it in GitHub Desktop.
//GEOIP
// http://maxmind.github.io/GeoIP2-java/
//https://github.com/maxmind/GeoIP2-java
//http://dev.maxmind.com/geoip/geoip2/geoip2-csv-databases/
File database = new File("C:/Users/loind/Desktop/GeoIP/GeoLite2-City.mmdb");
// This creates the DatabaseReader object, which should be reused across
// lookups.
DatabaseReader reader = new DatabaseReader.Builder(database).fileMode(Reader.FileMode.MEMORY_MAPPED).build();
InetAddress ipAddress = InetAddress.getByName("14.162.178.45");
// Replace "city" with the appropriate method for your database, e.g.,
// "country".
try {
CityResponse response = reader.city(ipAddress);
Country country = response.getCountry();
extraHeaders.put("country_code",country.getIsoCode());
extraHeaders.put("country_name",country.getName());
Subdivision subdivision = response.getMostSpecificSubdivision();
extraHeaders.put("subdivision_code",subdivision.getIsoCode());
extraHeaders.put("subdivision_name",subdivision.getName());
City city = response.getCity();
extraHeaders.put("city_name",city.getName());
Postal postal = response.getPostal();
extraHeaders.put("postal_code",postal.getCode());
Location location = response.getLocation();
extraHeaders.put("location_latitude",String.valueOf(location.getLatitude()));
extraHeaders.put("location_longitude",String.valueOf(location.getLongitude()));
} catch (GeoIp2Exception e) {
e.printStackTrace();
}
logger.debug("Request extraHeaders:{}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(extraHeaders));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment