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);
}
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;
}
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;
}
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);
}
Aggregations