Search in sources :

Example 11 with CityResponse

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) InetAddress(java.net.InetAddress) GeoProperty(com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty) OperationException(com.nextdoor.bender.operation.OperationException)

Example 12 with CityResponse

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;
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) IOException(java.io.IOException) InetAddress(java.net.InetAddress) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception)

Example 13 with CityResponse

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());
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) JSONObject(org.json.JSONObject) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 14 with CityResponse

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, "");
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) GeoDef(com.quickutil.platform.def.GeoDef)

Example 15 with CityResponse

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"));
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) MockFlowFile(org.apache.nifi.util.MockFlowFile) FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)25 InetAddress (java.net.InetAddress)13 Location (com.maxmind.geoip2.record.Location)10 HashMap (java.util.HashMap)10 Country (com.maxmind.geoip2.record.Country)9 IOException (java.io.IOException)9 City (com.maxmind.geoip2.record.City)7 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)6 Continent (com.maxmind.geoip2.record.Continent)6 Subdivision (com.maxmind.geoip2.record.Subdivision)6 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)5 CountryResponse (com.maxmind.geoip2.model.CountryResponse)5 Postal (com.maxmind.geoip2.record.Postal)5 Test (org.junit.Test)5 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 UnknownHostException (java.net.UnknownHostException)4 FlowFile (org.apache.nifi.flowfile.FlowFile)4 Traits (com.maxmind.geoip2.record.Traits)3 User (com.earth2me.essentials.User)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2