// Path: src/main/java/org/nudge/elasticstack/exception/NudgeESConnectorException.java
// @SuppressWarnings("serial")
// public class NudgeESConnectorException extends Exception {
// 
// 	public NudgeESConnectorException(String m) {
// 		super(m);
// 	}
// 
// 	public NudgeESConnectorException(String m, Throwable t) {
// 		super(m, t);
// 	}
// }
// 
// Path: src/main/java/org/nudge/elasticstack/context/elasticsearch/bean/GeoLocation.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class GeoLocation {
// 
// 	private double latitude;
// 	private double longitude;
// 	private long responseTime;
// 	private String type;
// 	private  String transactionId;
// 
// 	// =====================
// 	// Getters and Setters
// 	// =====================
// 
// 	public String getType() {
// 		return type;
// 	}
// 
// 	public void setType(String type) {
// 		this.type = type;
// 	}
// 
// 	public double getLatitude() {
// 		return latitude;
// 	}
// 
// 	public void setLatitude(double latitude) {
// 		this.latitude = latitude;
// 	}
// 
// 	public double getLongitude() {
// 		return longitude;
// 	}
// 
// 	public void setLongitude(double longitude) {
// 		this.longitude = longitude;
// 	}
// 
// 	public long getResponseTime() {
// 		return responseTime;
// 	}
// 
// 	public void setResponseTime(long responseTime) {
// 		this.responseTime = responseTime;
// 	}
// 
// 	public String getClientlocation() {
// 		return latitude + "," + longitude;
// 	}
// 	
// 	public  String getTransactionId() {
// 		return transactionId;
// 	}
// 
// 	public  void setTransactionId(String transactionId) {
// 		this.transactionId = transactionId;
// 	}
// 
// 
// }
// 
// Path: src/main/java/org/nudge/elasticstack/service/GeoLocationService.java
// public interface GeoLocationService {
//     public GeoLocation requestGeoLocationFromIp(String ip) throws NudgeESConnectorException;
// }


import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.nudge.elasticstack.exception.NudgeESConnectorException;
import org.nudge.elasticstack.context.elasticsearch.bean.GeoLocation;
import org.nudge.elasticstack.service.GeoLocationService;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
package org.nudge.elasticstack.service.impl;



/**
 * @author Sarah Bourgeois
 * @author Frederic Massart
 */
public class GeoFreeGeoIpImpl implements GeoLocationService {

	private static final Logger LOG = Logger.getLogger(GeoFreeGeoIpImpl.class);
	private static final String FINAL_URL = "http://freegeoip.net/json/";

	@Override
	public GeoLocation requestGeoLocationFromIp(String ip) throws NudgeESConnectorException {
		String geoLocString = getGeoFromIP(ip);
		return parseGeoLocation(geoLocString);
	}

	/**
	 *
	 * @param completeUrl
	 * @return
	 * @throws IOException
	 */
	private HttpURLConnection prepareGeoDataRequest(String completeUrl) throws NudgeESConnectorException {
		try {
			URL url = new URL(completeUrl);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("GET");
			con.setConnectTimeout(1000);
			con.setReadTimeout(5000);
			con.setInstanceFollowRedirects(false);
			return con;
		} catch (IOException e) {
			throw new NudgeESConnectorException("Failed to request geo data service", e);
		}
	}

// Path: src/main/java/org/nudge/elasticstack/exception/NudgeESConnectorException.java
// @SuppressWarnings("serial")
// public class NudgeESConnectorException extends Exception {
// 
// 	public NudgeESConnectorException(String m) {
// 		super(m);
// 	}
// 
// 	public NudgeESConnectorException(String m, Throwable t) {
// 		super(m, t);
// 	}
// }
// 
// Path: src/main/java/org/nudge/elasticstack/context/elasticsearch/bean/GeoLocation.java
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class GeoLocation {
// 
// 	private double latitude;
// 	private double longitude;
// 	private long responseTime;
// 	private String type;
// 	private  String transactionId;
// 
// 	// =====================
// 	// Getters and Setters
// 	// =====================
// 
// 	public String getType() {
// 		return type;
// 	}
// 
// 	public void setType(String type) {
// 		this.type = type;
// 	}
// 
// 	public double getLatitude() {
// 		return latitude;
// 	}
// 
// 	public void setLatitude(double latitude) {
// 		this.latitude = latitude;
// 	}
// 
// 	public double getLongitude() {
// 		return longitude;
// 	}
// 
// 	public void setLongitude(double longitude) {
// 		this.longitude = longitude;
// 	}
// 
// 	public long getResponseTime() {
// 		return responseTime;
// 	}
// 
// 	public void setResponseTime(long responseTime) {
// 		this.responseTime = responseTime;
// 	}
// 
// 	public String getClientlocation() {
// 		return latitude + "," + longitude;
// 	}
// 	
// 	public  String getTransactionId() {
// 		return transactionId;
// 	}
// 
// 	public  void setTransactionId(String transactionId) {
// 		this.transactionId = transactionId;
// 	}
// 
// 
// }
// 
// Path: src/main/java/org/nudge/elasticstack/service/GeoLocationService.java
// public interface GeoLocationService {
//     public GeoLocation requestGeoLocationFromIp(String ip) throws NudgeESConnectorException;
// }


// Path: src/main/java/org/nudge/elasticstack/service/impl/GeoFreeGeoIpImpl.java
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.nudge.elasticstack.exception.NudgeESConnectorException;
import org.nudge.elasticstack.context.elasticsearch.bean.GeoLocation;
import org.nudge.elasticstack.service.GeoLocationService;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

package org.nudge.elasticstack.service.impl;



/**
 * @author Sarah Bourgeois
 * @author Frederic Massart
 */
public class GeoFreeGeoIpImpl implements GeoLocationService {

	private static final Logger LOG = Logger.getLogger(GeoFreeGeoIpImpl.class);
	private static final String FINAL_URL = "http://freegeoip.net/json/";

	@Override
	public GeoLocation requestGeoLocationFromIp(String ip) throws NudgeESConnectorException {
		String geoLocString = getGeoFromIP(ip);
		return parseGeoLocation(geoLocString);
	}

	/**
	 *
	 * @param completeUrl
	 * @return
	 * @throws IOException
	 */
	private HttpURLConnection prepareGeoDataRequest(String completeUrl) throws NudgeESConnectorException {
		try {
			URL url = new URL(completeUrl);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("GET");
			con.setConnectTimeout(1000);
			con.setReadTimeout(5000);
			con.setInstanceFollowRedirects(false);
			return con;
		} catch (IOException e) {
			throw new NudgeESConnectorException("Failed to request geo data service", e);
		}
	}

	private GeoLocation parseGeoLocation(String geoLocationString) throws NudgeESConnectorException {