use of org.opennms.netmgt.poller.remote.support.GeodataResponse in project opennms by OpenNMS.
the class MetadataUtils method fetchGeodata.
public static Map<String, String> fetchGeodata() {
final Map<String, String> ret = new HashMap<>();
final String url = "http://freegeoip.net/xml/";
final CloseableHttpClient httpclient = HttpClients.createDefault();
final HttpGet get = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(get);
final HttpEntity entity = response.getEntity();
final String xml = EntityUtils.toString(entity);
System.err.println("xml = " + xml);
final GeodataResponse geoResponse = JaxbUtils.unmarshal(GeodataResponse.class, xml);
ret.put("external-ip-address", InetAddressUtils.str(geoResponse.getIp()));
ret.put("country-code", geoResponse.getCountryCode());
ret.put("region-code", geoResponse.getRegionCode());
ret.put("city", geoResponse.getCity());
ret.put("zip-code", geoResponse.getZipCode());
ret.put("time-zone", geoResponse.getTimeZone());
ret.put("latitude", geoResponse.getLatitude() == null ? null : geoResponse.getLatitude().toString());
ret.put("longitude", geoResponse.getLongitude() == null ? null : geoResponse.getLongitude().toString());
EntityUtils.consumeQuietly(entity);
} catch (final Exception e) {
LOG.debug("Failed to get GeoIP data from " + url, e);
} finally {
IOUtils.closeQuietly(response);
}
return ret;
}
Aggregations