use of com.maxmind.geoip2.record.Location in project elasticsearch by elastic.
the class GeoIpProcessor method retrieveCityGeoData.
private Map<String, Object> retrieveCityGeoData(InetAddress ipAddress) {
SpecialPermission.check();
CityResponse response = AccessController.doPrivileged((PrivilegedAction<CityResponse>) () -> {
try {
return dbReader.city(ipAddress);
} catch (AddressNotFoundException e) {
throw new AddressNotFoundRuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Country country = response.getCountry();
City city = response.getCity();
Location location = response.getLocation();
Continent continent = response.getContinent();
Subdivision subdivision = response.getMostSpecificSubdivision();
Map<String, Object> geoData = new HashMap<>();
for (Property property : this.properties) {
switch(property) {
case IP:
geoData.put("ip", NetworkAddress.format(ipAddress));
break;
case COUNTRY_ISO_CODE:
String countryIsoCode = country.getIsoCode();
if (countryIsoCode != null) {
geoData.put("country_iso_code", countryIsoCode);
}
break;
case COUNTRY_NAME:
String countryName = country.getName();
if (countryName != null) {
geoData.put("country_name", countryName);
}
break;
case CONTINENT_NAME:
String continentName = continent.getName();
if (continentName != null) {
geoData.put("continent_name", continentName);
}
break;
case REGION_NAME:
String subdivisionName = subdivision.getName();
if (subdivisionName != null) {
geoData.put("region_name", subdivisionName);
}
break;
case CITY_NAME:
String cityName = city.getName();
if (cityName != null) {
geoData.put("city_name", cityName);
}
break;
case TIMEZONE:
String locationTimeZone = location.getTimeZone();
if (locationTimeZone != null) {
geoData.put("timezone", locationTimeZone);
}
break;
case LOCATION:
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
if (latitude != null && longitude != null) {
Map<String, Object> locationObject = new HashMap<>();
locationObject.put("lat", latitude);
locationObject.put("lon", longitude);
geoData.put("location", locationObject);
}
break;
}
}
return geoData;
}
use of com.maxmind.geoip2.record.Location in project GNS by MobilityFirst.
the class ActiveBlockingQuerier method getLocations.
@Override
public ScriptObjectMirror getLocations(ScriptObjectMirror ipList) throws ActiveException {
// convert ipList to a JSONArray
JSONArray arr = null;
try {
arr = new JSONArray("[" + ipList.callMember("toString") + "]");
} catch (JSONException e) {
e.printStackTrace();
throw new ActiveException(e.getMessage());
}
// resolve ip one by one
JSONObject obj = new JSONObject();
for (int i = 0; i < arr.length(); i++) {
try {
String ip = arr.getString(i);
CityResponse loc = GeoIPUtils.getLocation_City(ip, dbReader);
if (loc != null) {
JSONObject value = new JSONObject();
value.put("latitude", loc.getLocation().getLatitude());
value.put("longitude", loc.getLocation().getLongitude());
// continent of the location
value.put("continent", loc.getContinent().getCode());
obj.put(ip, value);
}
} catch (JSONException e) {
continue;
}
}
return string2JS(obj.toString());
}
use of com.maxmind.geoip2.record.Location in project quickutil by quickutil.
the class GeoUtil method geoCodeyByBaidu.
/**
* 根据经纬度查询地理信息-百度世界
*
* @param latitude-纬度
* @param longitude-经度
* @return
*/
public static GeoDef geoCodeyByBaidu(double latitude, double longitude) {
try {
double[] delta = WGSToGCJPointer(latitude, longitude);
delta = GCJToBDPointer(delta[0], delta[1]);
String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s,%s&output=json", baiduKeyIn, delta[0], delta[1]);
JsonObject object = JsonUtil.toJsonMap(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
String country = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("country").getAsString();
if (country.equals("中国"))
country = "China";
if (country.equals("England"))
country = "United Kingdom";
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("province").getAsString();
String stateCode = "";
if (country.equals("China"))
stateCode = stateCodeByStateChinese(countryCode, state);
else
stateCode = stateCodeByStateName(countryCode, state);
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
String city = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("city").getAsString();
String description = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("district").getAsString();
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.maxmind.geoip2.record.Location in project divolte-collector by divolte.
the class DslRecordMapperTest method shouldPopulateFlatFields.
@Test
public void shouldPopulateFlatFields() throws InterruptedException, IOException {
setupServer("flat-mapping.groovy");
final EventPayload payload = request("https://example.com/", "http://example.com/");
final GenericRecord record = payload.record;
final DivolteEvent event = payload.event;
assertEquals(true, record.get("sessionStart"));
assertEquals(true, record.get("unreliable"));
assertEquals(false, record.get("dupe"));
assertEquals(event.requestStartTime.toEpochMilli(), record.get("ts"));
assertEquals("https://example.com/", record.get("location"));
assertEquals("http://example.com/", record.get("referer"));
assertEquals(USER_AGENT, record.get("userAgentString"));
assertEquals("Chrome", record.get("userAgentName"));
assertEquals("Chrome", record.get("userAgentFamily"));
assertEquals("Google Inc.", record.get("userAgentVendor"));
assertEquals("Browser", record.get("userAgentType"));
assertEquals("38.0.2125.122", record.get("userAgentVersion"));
assertEquals("Personal computer", record.get("userAgentDeviceCategory"));
assertEquals("OS X", record.get("userAgentOsFamily"));
assertEquals("10.10.1", record.get("userAgentOsVersion"));
assertEquals("Apple Computer, Inc.", record.get("userAgentOsVendor"));
assertEquals(event.partyId.value, record.get("client"));
assertEquals(event.sessionId.value, record.get("session"));
assertEquals(event.browserEventData.map(bed -> bed.pageViewId), Optional.of(record.get("pageview")));
assertEquals(event.eventId, record.get("event"));
assertEquals(1018, record.get("viewportWidth"));
assertEquals(1018, record.get("viewportHeight"));
assertEquals(1024, record.get("screenWidth"));
assertEquals(1024, record.get("screenHeight"));
assertEquals(2, record.get("pixelRatio"));
assertEquals("pageView", record.get("eventType"));
Stream.of("sessionStart", "unreliable", "dupe", "ts", "location", "referer", "userAgentString", "userAgentName", "userAgentFamily", "userAgentVendor", "userAgentType", "userAgentVersion", "userAgentDeviceCategory", "userAgentOsFamily", "userAgentOsVersion", "userAgentOsVendor", "client", "session", "pageview", "event", "viewportWidth", "viewportHeight", "screenWidth", "screenHeight", "pixelRatio", "eventType").forEach((v) -> assertNotNull(record.get(v)));
}
use of com.maxmind.geoip2.record.Location in project LogHub by fbacchella.
the class Geoip2 method processMessage.
@Override
public boolean processMessage(Event event, String field, String destination) throws ProcessorException {
Object addr = event.get(field);
InetAddress ipInfo = null;
if (addr instanceof InetAddress) {
ipInfo = (InetAddress) addr;
} else if (addr instanceof String) {
try {
ipInfo = Helpers.parseIpAddres((String) addr);
if (ipInfo == null) {
throw event.buildException("can't read ip address " + addr);
}
} catch (UnknownHostException e) {
throw event.buildException("can't read ip address " + addr, e);
}
}
Country country = null;
Country registred_country = null;
Country represented_country = null;
City city = null;
Continent continent = null;
Location location = null;
Postal postal = null;
List<Subdivision> subdivision = null;
Map<String, Object> informations = new HashMap<>();
try {
switch(reader.getMetadata().getDatabaseType()) {
case "GeoIP2-City":
case "GeoLite2-City":
{
CityResponse response = reader.city(ipInfo);
if (response == null) {
throw event.buildException("City not found for " + ipInfo.toString());
}
country = response.getCountry();
city = response.getCity();
continent = response.getContinent();
location = response.getLocation();
postal = response.getPostal();
registred_country = response.getRegisteredCountry();
represented_country = response.getRepresentedCountry();
subdivision = response.getSubdivisions();
break;
}
case "GeoIP2-Country":
case "GeoLite2-Country":
{
CountryResponse response = reader.country(ipInfo);
if (response == null) {
throw event.buildException("Country not found for " + ipInfo.toString());
}
country = response.getCountry();
continent = response.getContinent();
registred_country = response.getRegisteredCountry();
represented_country = response.getRepresentedCountry();
break;
}
}
} catch (AddressNotFoundException e) {
// not an error, just return a failure
return false;
} catch (IOException | GeoIp2Exception e) {
throw event.buildException("can't read geoip database", e);
}
for (LocationType type : types) {
switch(type) {
case COUNTRY:
if (country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", country.getIsoCode());
Helpers.putNotEmpty(infos, "name", country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("country", infos);
}
}
break;
case REPRESENTEDCOUNTRY:
if (represented_country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", represented_country.getIsoCode());
Helpers.putNotEmpty(infos, "name", represented_country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("represented_country", infos);
}
}
break;
case REGISTREDCOUNTRY:
if (registred_country != null) {
Map<String, Object> infos = new HashMap<>(2);
Helpers.putNotEmpty(infos, "code", registred_country.getIsoCode());
Helpers.putNotEmpty(infos, "name", registred_country.getNames().get(locale));
if (infos.size() > 0) {
informations.put("registred_country", infos);
}
}
break;
case CITY:
{
if (city != null) {
Helpers.putNotEmpty(informations, "city", city.getNames().get(locale));
}
break;
}
case LOCATION:
Map<String, Object> infos = new HashMap<>(10);
if (location != null) {
Helpers.putNotEmpty(infos, "latitude", location.getLatitude());
Helpers.putNotEmpty(infos, "longitude", location.getLongitude());
Helpers.putNotEmpty(infos, "timezone", location.getTimeZone());
Helpers.putNotEmpty(infos, "accuray_radius", location.getAccuracyRadius());
Helpers.putNotEmpty(infos, "metro_code", location.getMetroCode());
Helpers.putNotEmpty(infos, "average_income", location.getAverageIncome());
Helpers.putNotEmpty(infos, "population_density", location.getPopulationDensity());
if (infos.size() > 0) {
informations.put("location", infos);
}
}
case CONTINENT:
if (continent != null) {
Helpers.putNotEmpty(informations, "continent", continent.getNames().get(locale));
}
break;
case POSTAL:
if (postal != null) {
Helpers.putNotEmpty(informations, "postal", postal.getCode());
}
break;
case SUBDIVISION:
if (subdivision != null) {
List<Map<String, Object>> all = new ArrayList<>(subdivision.size());
for (Subdivision sub : subdivision) {
Map<String, Object> subdivisioninfo = new HashMap<>(2);
Helpers.putNotEmpty(subdivisioninfo, "code", sub.getIsoCode());
Helpers.putNotEmpty(subdivisioninfo, "name", sub.getNames().get(locale));
if (subdivisioninfo.size() > 0) {
all.add(subdivisioninfo);
}
}
if (all.size() > 0) {
informations.put("subdivisions", all);
}
}
break;
}
}
event.put(destination, informations);
return true;
}
Aggregations