Search in sources :

Example 16 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project tutorials by eugenp.

the class RawDBDemoGeoIPLocationService method getLocation.

public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
    InetAddress ipAddress = InetAddress.getByName(ip);
    CityResponse response = dbReader.city(ipAddress);
    String cityName = response.getCity().getName();
    String latitude = response.getLocation().getLatitude().toString();
    String longitude = response.getLocation().getLongitude().toString();
    return new GeoIP(ip, cityName, latitude, longitude);
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) GeoIP(com.baeldung.spring.form.GeoIP) InetAddress(java.net.InetAddress)

Example 17 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project Essentials by EssentialsX.

the class EssentialsGeoIPPlayerListener method delayedJoin.

public void delayedJoin(Player player) {
    User u = ess.getUser(player);
    if (u.isAuthorized("essentials.geoip.hide") || player.getAddress() == null) {
        return;
    }
    InetAddress address = player.getAddress().getAddress();
    StringBuilder sb = new StringBuilder();
    try {
        if (config.getBoolean("database.show-cities", false)) {
            CityResponse response = mmreader.city(address);
            if (response == null) {
                return;
            }
            String city;
            String region;
            String country;
            city = response.getCity().getName();
            region = response.getMostSpecificSubdivision().getName();
            country = response.getCountry().getName();
            if (city != null) {
                sb.append(city).append(", ");
            }
            if (region != null) {
                sb.append(region).append(", ");
            }
            sb.append(country);
        } else {
            CountryResponse response = mmreader.country(address);
            sb.append(response.getCountry().getName());
        }
    } catch (AddressNotFoundException ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        // TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
        logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    } catch (IOException | GeoIp2Exception ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    }
    if (config.getBoolean("show-on-whois", true)) {
        u.setGeoLocation(sb.toString());
    }
    if (config.getBoolean("show-on-login", true) && !u.isHidden()) {
        for (Player onlinePlayer : player.getServer().getOnlinePlayers()) {
            User user = ess.getUser(onlinePlayer);
            if (user.isAuthorized("essentials.geoip.show")) {
                user.sendMessage(tl("geoipJoinFormat", u.getDisplayName(), sb.toString()));
            }
        }
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Player(org.bukkit.entity.Player) User(com.earth2me.essentials.User) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress)

Example 18 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project Essentials by drtshock.

the class EssentialsGeoIPPlayerListener method delayedJoin.

public void delayedJoin(Player player) {
    User u = ess.getUser(player);
    if (u.isAuthorized("essentials.geoip.hide") || player.getAddress() == null) {
        return;
    }
    InetAddress address = player.getAddress().getAddress();
    StringBuilder sb = new StringBuilder();
    try {
        if (config.getBoolean("database.show-cities", false)) {
            CityResponse response = mmreader.city(address);
            if (response == null) {
                return;
            }
            String city;
            String region;
            String country;
            city = response.getCity().getName();
            region = response.getMostSpecificSubdivision().getName();
            country = response.getCountry().getName();
            if (city != null) {
                sb.append(city).append(", ");
            }
            if (region != null) {
                sb.append(region).append(", ");
            }
            sb.append(country);
        } else {
            CountryResponse response = mmreader.country(address);
            sb.append(response.getCountry().getName());
        }
    } catch (AddressNotFoundException ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        // TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
        logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    } catch (IOException | GeoIp2Exception ex) {
        // GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
        logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
    }
    if (config.getBoolean("show-on-whois", true)) {
        u.setGeoLocation(sb.toString());
    }
    if (config.getBoolean("show-on-login", true) && !u.isHidden()) {
        for (Player onlinePlayer : player.getServer().getOnlinePlayers()) {
            User user = ess.getUser(onlinePlayer);
            if (user.isAuthorized("essentials.geoip.show")) {
                user.sendMessage(tl("geoipJoinFormat", u.getDisplayName(), sb.toString()));
            }
        }
    }
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Player(org.bukkit.entity.Player) User(com.earth2me.essentials.User) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)10 InetAddress (java.net.InetAddress)10 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)9 IOException (java.io.IOException)9 City (com.maxmind.geoip2.record.City)4 Country (com.maxmind.geoip2.record.Country)4 Location (com.maxmind.geoip2.record.Location)4 Subdivision (com.maxmind.geoip2.record.Subdivision)4 HashMap (java.util.HashMap)4 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)3 CountryResponse (com.maxmind.geoip2.model.CountryResponse)3 Continent (com.maxmind.geoip2.record.Continent)3 Postal (com.maxmind.geoip2.record.Postal)3 UnknownHostException (java.net.UnknownHostException)3 Timer (com.codahale.metrics.Timer)2 User (com.earth2me.essentials.User)2 DatabaseReader (com.maxmind.geoip2.DatabaseReader)2 IspResponse (com.maxmind.geoip2.model.IspResponse)2 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2