// Path: src/main/java/org/nudge/elasticstack/context/elasticsearch/ESVersion.java
// public enum ESVersion {
// 	ES2, ES5, ES6
// }
// 
// 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/exception/UnsupportedElasticStackException.java
// @SuppressWarnings("serial")
// public class UnsupportedElasticStackException extends RuntimeException {
// 
// 	public UnsupportedElasticStackException(String message) {
// 		super(message);
// 	}
// 
// 	public UnsupportedElasticStackException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// }


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.nudge.elasticstack.context.elasticsearch.ESVersion;
import org.nudge.elasticstack.exception.NudgeESConnectorException;
import org.nudge.elasticstack.exception.UnsupportedElasticStackException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
package org.nudge.elasticstack.connection;



public class ElasticConnection {

	private static final Logger LOG = Logger.getLogger(ElasticConnection.class.getName());

	private final String elasticHost;
	private final Metadata metadata;
	private final ESVersion esVersion;
	private String esHostIndexURL;

	public ElasticConnection(String hostUrl) throws Exception {
		this.elasticHost = hostUrl;
		String jsonMetadata = get();
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		this.metadata = mapper.readValue(jsonMetadata, Metadata.class);
		this.esVersion = determineESVersion(this.metadata);
	}

	private String get() throws Exception {
		URL url = new URL(elasticHost);
		if (LOG.isDebugEnabled()) {
			LOG.debug("GET " + url);
		}
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();
		String message = readHttpResponse(connection);
		if (connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
			return message;
		} else {
// Path: src/main/java/org/nudge/elasticstack/context/elasticsearch/ESVersion.java
// public enum ESVersion {
// 	ES2, ES5, ES6
// }
// 
// 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/exception/UnsupportedElasticStackException.java
// @SuppressWarnings("serial")
// public class UnsupportedElasticStackException extends RuntimeException {
// 
// 	public UnsupportedElasticStackException(String message) {
// 		super(message);
// 	}
// 
// 	public UnsupportedElasticStackException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// }


// Path: src/main/java/org/nudge/elasticstack/connection/ElasticConnection.java
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.nudge.elasticstack.context.elasticsearch.ESVersion;
import org.nudge.elasticstack.exception.NudgeESConnectorException;
import org.nudge.elasticstack.exception.UnsupportedElasticStackException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

package org.nudge.elasticstack.connection;



public class ElasticConnection {

	private static final Logger LOG = Logger.getLogger(ElasticConnection.class.getName());

	private final String elasticHost;
	private final Metadata metadata;
	private final ESVersion esVersion;
	private String esHostIndexURL;

	public ElasticConnection(String hostUrl) throws Exception {
		this.elasticHost = hostUrl;
		String jsonMetadata = get();
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		this.metadata = mapper.readValue(jsonMetadata, Metadata.class);
		this.esVersion = determineESVersion(this.metadata);
	}

	private String get() throws Exception {
		URL url = new URL(elasticHost);
		if (LOG.isDebugEnabled()) {
			LOG.debug("GET " + url);
		}
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();
		String message = readHttpResponse(connection);
		if (connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
			return message;
		} else {
			throw new NudgeESConnectorException("Failed ES command with message: " + message);