Search in sources :

Example 1 with Location

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;
}
Also used : HashMap(java.util.HashMap) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) ConfigurationUtils.newConfigurationException(org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) ConfigurationUtils.readStringProperty(org.elasticsearch.ingest.ConfigurationUtils.readStringProperty) ConfigurationUtils.readBooleanProperty(org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty) Location(com.maxmind.geoip2.record.Location)

Example 2 with Location

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());
}
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 3 with Location

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;
}
Also used : JsonObject(com.google.gson.JsonObject) GeoDef(com.quickutil.platform.def.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 4 with Location

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)));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Arrays(java.util.Arrays) InjectableValues(com.fasterxml.jackson.databind.InjectableValues) ClosedServiceException(io.divolte.server.ip2geo.LookupService.ClosedServiceException) URL(java.net.URL) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) EventPayload(io.divolte.server.ServerTestUtils.EventPayload) GenericData(org.apache.avro.generic.GenericData) ImmutableList(com.google.common.collect.ImmutableList) ValidatedConfiguration(io.divolte.server.config.ValidatedConfiguration) Map(java.util.Map) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Utf8(org.apache.avro.util.Utf8) GenericRecord(org.apache.avro.generic.GenericRecord) Schema(org.apache.avro.Schema) JsonParser(com.fasterxml.jackson.core.JsonParser) Files(java.nio.file.Files) Config(com.typesafe.config.Config) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SchemaMappingException(io.divolte.server.recordmapping.SchemaMappingException) StandardCharsets(java.nio.charset.StandardCharsets) TestServer(io.divolte.server.ServerTestUtils.TestServer) Mockito(org.mockito.Mockito) URLEncoder(java.net.URLEncoder) List(java.util.List) Stream(java.util.stream.Stream) java.io(java.io) LookupService(io.divolte.server.ip2geo.LookupService) CityResponse(com.maxmind.geoip2.model.CityResponse) Optional(java.util.Optional) DslRecordMapper(io.divolte.server.recordmapping.DslRecordMapper) Assert(org.junit.Assert) Collections(java.util.Collections) GenericRecord(org.apache.avro.generic.GenericRecord) EventPayload(io.divolte.server.ServerTestUtils.EventPayload) Test(org.junit.Test)

Example 5 with Location

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) HashMap(java.util.HashMap) Map(java.util.Map) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)12 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)11 Location (com.maxmind.geoip2.record.Location)10 Country (com.maxmind.geoip2.record.Country)9 UnknownHostException (java.net.UnknownHostException)8 City (com.maxmind.geoip2.record.City)7 JsonObject (com.google.gson.JsonObject)6 Continent (com.maxmind.geoip2.record.Continent)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Postal (com.maxmind.geoip2.record.Postal)5 Subdivision (com.maxmind.geoip2.record.Subdivision)5 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)4 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 InetAddress (java.net.InetAddress)4 CountryResponse (com.maxmind.geoip2.model.CountryResponse)3 Traits (com.maxmind.geoip2.record.Traits)3 GeoDef (com.quickutil.platform.def.GeoDef)3 GeoDef (com.quickutil.platform.entity.GeoDef)3 JsonArray (com.google.gson.JsonArray)2