use of com.maxmind.geoip2.model.CityResponse in project bender by Nextdoor.
the class GeoIpOperation method perform.
@Override
public InternalEvent perform(InternalEvent ievent) {
String ipStr = null;
/*
* Get field containing an IP address
*/
try {
ipStr = ievent.getEventObj().getFieldAsString(this.pathToIpAddress);
} catch (FieldNotFoundException e) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " does not exist");
}
if (ipStr == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " was null");
}
/*
* Sometimes the field contains comma separated ip addresses (ie forwarded web requests). In
* this case pick the first value in the list which is typically the user.
*/
if (!ipStr.isEmpty() && ipStr.contains(",")) {
ipStr = ipStr.split(",")[0];
}
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(ipStr);
} catch (UnknownHostException e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
if (ipAddress == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address " + ipStr + " did not resolve");
}
CityResponse response = null;
try {
response = this.databaseReader.city(ipAddress);
} catch (IOException | GeoIp2Exception e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
HashMap<String, Object> geo = new HashMap<String, Object>(1);
for (GeoProperty property : this.geoProperties) {
switch(property) {
case COUNTRY_NAME:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_name", response.getCountry().getName());
break;
case COUNTRY_ISO_CODE:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_iso_code", response.getCountry().getIsoCode());
break;
case SUBDIVISION_NAME:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_name", response.getMostSpecificSubdivision().getName());
break;
case SUBDIVISION_ISO_CODE:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_iso_code", response.getMostSpecificSubdivision().getIsoCode());
break;
case CITY_NAME:
if (response.getCity() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("city returned null");
}
geo.put("city_name", response.getCity().getName());
break;
case POSTAL_CODE:
if (response.getPostal() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("postal returned null");
}
geo.put("postal_code", response.getPostal().getCode());
break;
case LOCATION:
if (response.getLocation() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("location returned null");
}
Double lat = response.getLocation().getLatitude();
Double lon = response.getLocation().getLongitude();
if (lat == null || lon == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("error getting lat/lon");
}
HashMap<String, Object> location = new HashMap<String, Object>(2);
location.put("lat", lat);
location.put("lon", lon);
geo.put("location", location);
break;
}
}
try {
ievent.getEventObj().setField(this.destFieldName, geo);
} catch (FieldNotFoundException e) {
throw new OperationException(e);
}
return ievent;
}
use of com.maxmind.geoip2.model.CityResponse in project GNS by MobilityFirst.
the class GeoIPUtils method getLocation_City.
/**
* This method is used for test, whether a static method can be called by active code
* @param ip
* @param dbReader
* @return a GeoIP response
*/
public static CityResponse getLocation_City(String ip, DatabaseReader dbReader) {
try {
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = dbReader.city(ipAddress);
return response;
} catch (IOException | GeoIp2Exception e) {
return null;
}
}
use of com.maxmind.geoip2.model.CityResponse in project GNS by MobilityFirst.
the class ActiveNonBlockingQuerier 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());
}
//System.out.println(">>>>>>>>>>>> The query is "+arr.toString());
// 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;
}
}
//System.out.println("The result is "+obj.toString());
return string2JS(obj.toString());
}
use of com.maxmind.geoip2.model.CityResponse in project quickutil by quickutil.
the class GeoUtil method GeoIPByMMDB.
public static GeoDef GeoIPByMMDB(String ip) {
String countryCode = UNKNOWN;
String country = UNKNOWN;
String countryChinese = UNKNOWN;
String stateCode = UNKNOWN;
String state = UNKNOWN;
String stateChinese = UNKNOWN;
String city = UNKNOWN;
Double latitude = 0.0;
Double longitude = 0.0;
try {
InetAddress ipAddr = InetAddress.getByName(ip);
CityResponse result;
result = databaseReader.city(ipAddr);
countryCode = result.getCountry().getIsoCode();
country = result.getCountry().getName();
countryChinese = countryChineseByCountryCode(countryCode);
stateCode = result.getMostSpecificSubdivision().getIsoCode();
state = stateNameByStateCode(countryCode, stateCode);
stateChinese = stateChineseByStateCode(countryCode, stateCode);
city = result.getCity().getName();
if (countryCode != null && countryCode.equals("CN") && city != null && result.getCity().getNames().containsKey("zh-CN")) {
if (!result.getCity().getNames().get("zh-CN").endsWith("市")) {
city = result.getCity().getNames().get("zh-CN") + "市";
} else {
city = result.getCity().getNames().get("zh-CN");
}
}
latitude = result.getLocation().getLatitude();
longitude = result.getLocation().getLongitude();
} catch (AddressNotFoundException ae) {
} catch (UnknownHostException ue) {
} catch (Exception e) {
e.printStackTrace();
}
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;
latitude = (latitude == null) ? 0.0 : latitude;
longitude = (longitude == null) ? 0.0 : longitude;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, "");
}
use of com.maxmind.geoip2.model.CityResponse in project nifi by apache.
the class TestGeoEnrichIP method successfulMaxMindResponseShouldFlowToFoundRelationship.
@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationship() throws Exception {
testRunner.setProperty(GeoEnrichIP.GEO_DATABASE_FILE, "./");
testRunner.setProperty(GeoEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");
final CityResponse cityResponse = getFullCityResponse();
when(databaseReader.city(InetAddress.getByName("1.2.3.4"))).thenReturn(cityResponse);
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(0, notFound.size());
assertEquals(1, found.size());
FlowFile finishedFound = found.get(0);
assertNotNull(finishedFound.getAttribute("ip.geo.lookup.micros"));
assertEquals("Minneapolis", finishedFound.getAttribute("ip.geo.city"));
assertEquals("44.98", finishedFound.getAttribute("ip.geo.latitude"));
assertEquals("93.2636", finishedFound.getAttribute("ip.geo.longitude"));
assertEquals("Minnesota", finishedFound.getAttribute("ip.geo.subdivision.0"));
assertEquals("MN", finishedFound.getAttribute("ip.geo.subdivision.isocode.0"));
assertNull(finishedFound.getAttribute("ip.geo.subdivision.1"));
assertEquals("TT", finishedFound.getAttribute("ip.geo.subdivision.isocode.1"));
assertEquals("United States of America", finishedFound.getAttribute("ip.geo.country"));
assertEquals("US", finishedFound.getAttribute("ip.geo.country.isocode"));
assertEquals("55401", finishedFound.getAttribute("ip.geo.postalcode"));
}
Aggregations