Search in sources :

Example 1 with GeodataResponse

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) GeodataResponse(org.opennms.netmgt.poller.remote.support.GeodataResponse) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

HashMap (java.util.HashMap)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 GeodataResponse (org.opennms.netmgt.poller.remote.support.GeodataResponse)1