package org.lantern.geoip;


import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;

import org.lantern.JsonUtils;
import org.apache.commons.io.IOUtils;

import org.lantern.util.HttpClientFactory;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

class Location {
  private double Latitude;
  private double Longitude;

  public double getLatitude() {
    return Latitude;
  }

  public double getLongitude() {
    return Longitude;
  }
}

class Country {
  private String IsoCode;

  public String getIsoCode() {
    return IsoCode;
  }
}

public class GeoData {
  private static final Logger LOGGER = LoggerFactory.getLogger(GeoData.class);

  private static final String LOOKUP_URL = 
                        "http://go-geoserve.herokuapp.com/lookup/";

  private Country Country;
  private Location Location;

  public double getLatitude() {
    return Location.getLatitude();
  }

  public double getLongitude() {
    return Location.getLongitude();
  }

  public String getCountrycode() {
    return Country.getIsoCode();
  }

  public static GeoData queryGeoServe(final HttpClientFactory httpClientFactory, String ipAddress) {
    final HttpClient proxied = httpClientFactory.newProxiedClient();
    final HttpGet get = new HttpGet(LOOKUP_URL + ipAddress);
    InputStream is = null;
    try {
      final HttpResponse response = proxied.execute(get);
      is = response.getEntity().getContent();
      final String geoStr = IOUtils.toString(is);
      return JsonUtils.OBJECT_MAPPER.readValue(geoStr, GeoData.class);
    } catch (ClientProtocolException e) {
      LOGGER.warn("Error connecting to geo lookup service " + 
          e.getMessage(), e);
    } catch (IOException e) {
      LOGGER.warn("Error parsing JSON from geo lookup " + 
          e.getMessage(), e);
    }
    finally {
      IOUtils.closeQuietly(is);
      get.reset();
    }
    return null;
  }

  @Override
    public String toString() {
      return "GeoData [countryCode=" + getCountrycode() + ", latitude="
        + getLatitude()
        + ", longitude=" + getLongitude() + "]";
    }
}

@@ -912,17 +912,116 @@
 actory;%0A
- 
+%0Aclass Country %7B%0A  private String IsoCode;%0A%0A  public String getIsoCode() %7B%0A    return IsoCode;%0A  %7D%0A%7D
 %0A%0Aclass 
@@ -1211,109 +1211,8 @@
 %0A%7D%0A%0A
-class Country %7B%0A  private String IsoCode;%0A%0A  public String getIsoCode() %7B%0A    return IsoCode;%0A  %7D%0A%7D%0A%0A
 publ
@@ -1230,16 +1230,17 @@
 oData %7B%0A
+%0A
   privat

use jsonutils