Search in sources :

Example 1 with MaxMind

use of com.maxmind.geoip2.record.MaxMind in project cas by apereo.

the class MaxmindDatabaseGeoLocationServiceTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val city = mock(DatabaseReader.class);
    val cityResponse = new CityResponse(new City(), new Continent(), new Country(), new Location(), new MaxMind(), new Postal(), new Country(), new RepresentedCountry(), new ArrayList<>(), new Traits());
    when(city.city(any(InetAddress.class))).thenReturn(cityResponse);
    val country = mock(DatabaseReader.class);
    val countryResponse = new CountryResponse(new Continent(), new Country(), new MaxMind(), new Country(), new RepresentedCountry(), new Traits());
    when(country.country(any(InetAddress.class))).thenReturn(countryResponse);
    val service = new MaxmindDatabaseGeoLocationService(city, country);
    val response = service.locate("127.0.0.1");
    assertNotNull(response);
    val response2 = service.locate(100D, 100D);
    assertNull(response2);
}
Also used : lombok.val(lombok.val) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) City(com.maxmind.geoip2.record.City) Traits(com.maxmind.geoip2.record.Traits) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) MaxMind(com.maxmind.geoip2.record.MaxMind) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location) Test(org.junit.jupiter.api.Test)

Example 2 with MaxMind

use of com.maxmind.geoip2.record.MaxMind in project quickutil by quickutil.

the class GeoUtil method initIPOffline.

// 初始化之后使用maxmind的库查询IP
public static boolean initIPOffline(String mmdbPath, String countryStatePath) {
    try {
        // 读取国家地区库
        if (countryStatePath == null) {
            countryStatePath = FileUtil.getCurrentPath() + File.separator + "country_state.json";
        }
        File countryStateFile = new File(countryStatePath);
        if (!countryStateFile.exists()) {
            LOGGER.error("缺少country_state.json文件,请放置到" + countryStatePath);
            return false;
        }
        // 读取IP库
        if (mmdbPath == null) {
            mmdbPath = FileUtil.getCurrentPath() + File.separator + "GeoLite2-City.mmdb";
        }
        File mmdbFile = new File(mmdbPath);
        if (!mmdbFile.exists()) {
            LOGGER.error("缺少GeoLite2-City.mmdb文件,请放置到" + mmdbPath);
            return false;
        }
        mmdbReader = new DatabaseReader.Builder(mmdbFile).build();
        // 生成缓存
        String stateCodeNewVersion = "state_code_iso_3166_2_20171123";
        List<Map<String, Object>> list = JsonUtil.toList(FileUtil.file2String(countryStatePath));
        for (Map<String, Object> map : list) {
            countryCodeByCountryNameMap.put((String) map.get("country_name"), (String) map.get("country_code"));
            countryChineseByCountryCodeMap.put((String) map.get("country_code"), (String) map.get("country_chinese"));
            stateNameByStateCodeMap.put((String) map.get("country_code") + "_" + (String) map.get("state_code"), (String) map.get("state_name"));
            stateChineseByStateCodeMap.put((String) map.get("country_code") + "_" + (String) map.get("state_code"), (String) map.get("state_chinese"));
            stateCodeByStateCodeNewVersionMap.put(map.get("country_code") + "_" + map.get("state_code"), (String) map.get("state_code"));
            if (map.containsKey(stateCodeNewVersion)) {
                stateNameByStateCodeMap.put((String) map.get("country_code") + "_" + (String) map.get(stateCodeNewVersion), (String) map.get("state_name"));
                stateChineseByStateCodeMap.put((String) map.get("country_code") + "_" + (String) map.get(stateCodeNewVersion), (String) map.get("state_chinese"));
                stateCodeByStateCodeNewVersionMap.put(map.get("country_code") + "_" + map.get(stateCodeNewVersion), (String) map.get("state_code"));
            }
            // 只建立到旧的state_code的映射,供geoCodeyByBaidu、geoCodeyByAmap使用
            stateCodeByStateNameMap.put((String) map.get("country_code") + "_" + (String) map.get("state_name"), (String) map.get("state_code"));
            stateCodeByStateNameChineseMap.put((String) map.get("country_code") + "_" + (String) map.get("state_chinese"), (String) map.get("state_code"));
        }
        return true;
    } catch (Exception e) {
        LOGGER.error(Symbol.BLANK, e);
    }
    return false;
}
Also used : JsonObject(com.google.gson.JsonObject) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 3 with MaxMind

use of com.maxmind.geoip2.record.MaxMind in project cas by apereo.

the class MaxmindDatabaseGeoLocationService method locate.

@Override
public GeoLocationResponse locate(final InetAddress address) {
    try {
        if (cityDatabaseReader == null && countryDatabaseReader == null) {
            throw new IllegalArgumentException("No geolocation services have been defined for Maxmind");
        }
        val location = new GeoLocationResponse();
        if (this.cityDatabaseReader != null) {
            val response = this.cityDatabaseReader.city(address);
            location.addAddress(response.getCity().getName());
            val loc = response.getLocation();
            if (loc != null) {
                if (loc.getLatitude() != null) {
                    location.setLatitude(loc.getLatitude());
                }
                if (loc.getLongitude() != null) {
                    location.setLongitude(loc.getLongitude());
                }
            }
        }
        if (this.countryDatabaseReader != null) {
            val response = this.countryDatabaseReader.country(address);
            location.addAddress(response.getCountry().getName());
        }
        LOGGER.debug("Geo location for [{}] is calculated as [{}]", address, location);
        return location;
    } catch (final AddressNotFoundException e) {
        LOGGER.info(e.getMessage(), e);
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return null;
}
Also used : lombok.val(lombok.val) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 4 with MaxMind

use of com.maxmind.geoip2.record.MaxMind in project cas by apereo.

the class MaxmindDatabaseGeoLocationServiceTests method verifyCity.

@Test
public void verifyCity() throws Exception {
    val cityReader = mock(DatabaseReader.class);
    val cityResponse = new CityResponse(new City(), new Continent(), new Country(), new Location(10, 100, 40D, 70D, 1, 1, "UTC"), new MaxMind(), new Postal(), new Country(), new RepresentedCountry(), new ArrayList<>(), new Traits());
    when(cityReader.city(any())).thenReturn(cityResponse);
    val service = new MaxmindDatabaseGeoLocationService(cityReader, null);
    val response = service.locate("127.0.0.1");
    assertNotNull(response);
}
Also used : lombok.val(lombok.val) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) MaxMind(com.maxmind.geoip2.record.MaxMind) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) Postal(com.maxmind.geoip2.record.Postal) Traits(com.maxmind.geoip2.record.Traits) Location(com.maxmind.geoip2.record.Location) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)3 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)2 CityResponse (com.maxmind.geoip2.model.CityResponse)2 City (com.maxmind.geoip2.record.City)2 Continent (com.maxmind.geoip2.record.Continent)2 Country (com.maxmind.geoip2.record.Country)2 Location (com.maxmind.geoip2.record.Location)2 MaxMind (com.maxmind.geoip2.record.MaxMind)2 Postal (com.maxmind.geoip2.record.Postal)2 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)2 Traits (com.maxmind.geoip2.record.Traits)2 Test (org.junit.jupiter.api.Test)2 JsonObject (com.google.gson.JsonObject)1 CountryResponse (com.maxmind.geoip2.model.CountryResponse)1 File (java.io.File)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)1