Search in sources :

Example 1 with Continent

use of com.maxmind.geoip2.record.Continent 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 Continent

use of com.maxmind.geoip2.record.Continent 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 Continent

use of com.maxmind.geoip2.record.Continent in project druid by druid-io.

the class WikipediaIrcDecoder method decodeMessage.

@Override
public InputRow decodeMessage(final DateTime timestamp, String channel, String msg) {
    final Map<String, String> dimensions = Maps.newHashMap();
    final Map<String, Float> metrics = Maps.newHashMap();
    Matcher m = pattern.matcher(msg);
    if (!m.matches()) {
        throw new IllegalArgumentException("Invalid input format");
    }
    Matcher shortname = shortnamePattern.matcher(channel);
    if (shortname.matches()) {
        dimensions.put("language", shortname.group(1));
    }
    String page = m.group(1);
    String pageUrl = page.replaceAll("\\s", "_");
    dimensions.put("page", pageUrl);
    String user = m.group(4);
    Matcher ipMatch = ipPattern.matcher(user);
    boolean anonymous = ipMatch.matches();
    if (anonymous) {
        try {
            final InetAddress ip = InetAddress.getByName(ipMatch.group());
            final Omni lookup = geoLookup.omni(ip);
            dimensions.put("continent", lookup.getContinent().getName());
            dimensions.put("country", lookup.getCountry().getName());
            dimensions.put("region", lookup.getMostSpecificSubdivision().getName());
            dimensions.put("city", lookup.getCity().getName());
        } catch (UnknownHostException e) {
            log.error(e, "invalid ip [%s]", ipMatch.group());
        } catch (IOException e) {
            log.error(e, "error looking up geo ip");
        } catch (GeoIp2Exception e) {
            log.error(e, "error looking up geo ip");
        }
    }
    dimensions.put("user", user);
    final String flags = m.group(2);
    dimensions.put("unpatrolled", Boolean.toString(flags.contains("!")));
    dimensions.put("newPage", Boolean.toString(flags.contains("N")));
    dimensions.put("robot", Boolean.toString(flags.contains("B")));
    dimensions.put("anonymous", Boolean.toString(anonymous));
    String[] parts = page.split(":");
    if (parts.length > 1 && !parts[1].startsWith(" ")) {
        Map<String, String> channelNamespaces = namespaces.get(channel);
        if (channelNamespaces != null && channelNamespaces.containsKey(parts[0])) {
            dimensions.put("namespace", channelNamespaces.get(parts[0]));
        } else {
            dimensions.put("namespace", "wikipedia");
        }
    } else {
        dimensions.put("namespace", "article");
    }
    float delta = m.group(6) != null ? Float.parseFloat(m.group(6)) : 0;
    metrics.put("delta", delta);
    metrics.put("added", Math.max(delta, 0));
    metrics.put("deleted", Math.min(delta, 0));
    return new InputRow() {

        @Override
        public List<String> getDimensions() {
            return dimensionList;
        }

        @Override
        public long getTimestampFromEpoch() {
            return timestamp.getMillis();
        }

        @Override
        public DateTime getTimestamp() {
            return timestamp;
        }

        @Override
        public List<String> getDimension(String dimension) {
            final String value = dimensions.get(dimension);
            if (value != null) {
                return ImmutableList.of(value);
            } else {
                return ImmutableList.of();
            }
        }

        @Override
        public Object getRaw(String dimension) {
            return dimensions.get(dimension);
        }

        @Override
        public float getFloatMetric(String metric) {
            return metrics.get(metric);
        }

        @Override
        public long getLongMetric(String metric) {
            return new Float(metrics.get(metric)).longValue();
        }

        @Override
        public int compareTo(Row o) {
            return timestamp.compareTo(o.getTimestamp());
        }

        @Override
        public String toString() {
            return "WikipediaRow{" + "timestamp=" + timestamp + ", dimensions=" + dimensions + ", metrics=" + metrics + '}';
        }
    };
}
Also used : UnknownHostException(java.net.UnknownHostException) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) InputRow(io.druid.data.input.InputRow) InputRow(io.druid.data.input.InputRow) Row(io.druid.data.input.Row) Omni(com.maxmind.geoip2.model.Omni) InetAddress(java.net.InetAddress)

Example 4 with Continent

use of com.maxmind.geoip2.record.Continent in project elasticsearch by elastic.

the class GeoIpProcessor method retrieveCountryGeoData.

private Map<String, Object> retrieveCountryGeoData(InetAddress ipAddress) {
    SpecialPermission.check();
    CountryResponse response = AccessController.doPrivileged((PrivilegedAction<CountryResponse>) () -> {
        try {
            return dbReader.country(ipAddress);
        } catch (AddressNotFoundException e) {
            throw new AddressNotFoundRuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    Country country = response.getCountry();
    Continent continent = response.getContinent();
    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;
        }
    }
    return geoData;
}
Also used : Continent(com.maxmind.geoip2.record.Continent) HashMap(java.util.HashMap) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) ConfigurationUtils.readStringProperty(org.elasticsearch.ingest.ConfigurationUtils.readStringProperty) ConfigurationUtils.readBooleanProperty(org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty) ConfigurationUtils.newConfigurationException(org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException) IOException(java.io.IOException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 5 with Continent

use of com.maxmind.geoip2.record.Continent 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)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)3 IOException (java.io.IOException)3 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)2 Continent (com.maxmind.geoip2.record.Continent)2 Country (com.maxmind.geoip2.record.Country)2 ActiveException (edu.umass.cs.gnsserver.activecode.prototype.ActiveException)2 HashMap (java.util.HashMap)2 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 ConfigurationUtils.newConfigurationException (org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException)2 ConfigurationUtils.readBooleanProperty (org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty)2 ConfigurationUtils.readStringProperty (org.elasticsearch.ingest.ConfigurationUtils.readStringProperty)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)1 CountryResponse (com.maxmind.geoip2.model.CountryResponse)1 Omni (com.maxmind.geoip2.model.Omni)1 City (com.maxmind.geoip2.record.City)1 Location (com.maxmind.geoip2.record.Location)1 Subdivision (com.maxmind.geoip2.record.Subdivision)1