package com.ecwid.consul.transport;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;

public class AbstractHttpTransport implements HttpTransport {

	protected final HttpClient httpClient;

	public AbstractHttpTransport() {
		PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
		connectionManager.setMaxTotal(1000);
		connectionManager.setDefaultMaxPerRoute(500);

		this.httpClient = new DefaultHttpClient(connectionManager);
	}

	public AbstractHttpTransport(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	@Override
	public RawResponse makeGetRequest(String url) {
		HttpGet httpGet = new HttpGet(url);
		return executeRequest(httpGet);
	}

	@Override
	public RawResponse makePutRequest(String url, String content) {
		HttpPut httpPut = new HttpPut(url);
		httpPut.setEntity(new StringEntity(content, Charset.forName("UTF-8")));
		return executeRequest(httpPut);
	}

	@Override
	public RawResponse makePutRequest(String url, byte[] content) {
		HttpPut httpPut = new HttpPut(url);
		httpPut.setEntity(new ByteArrayEntity(content));
		return executeRequest(httpPut);
	}

	@Override
	public RawResponse makeDeleteRequest(String url) {
		HttpDelete httpDelete = new HttpDelete(url);
		return executeRequest(httpDelete);
	}

	private RawResponse executeRequest(HttpUriRequest httpRequest) {
		try {
			return httpClient.execute(httpRequest, new ResponseHandler<RawResponse>() {
				@Override
				public RawResponse handleResponse(HttpResponse response) throws IOException {
					int statusCode = response.getStatusLine().getStatusCode();
					String statusMessage = response.getStatusLine().getReasonPhrase();

					String content = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));

					Long consulIndex = parseLong(response.getFirstHeader("X-Consul-Index"));
					Boolean consulKnownLeader = parseBoolean(response.getFirstHeader("X-Consul-Knownleader"));
					Long consulLastContact = parseLong(response.getFirstHeader("X-Consul-Lastcontact"));

					return new RawResponse(statusCode, statusMessage, content, consulIndex, consulKnownLeader, consulLastContact);
				}
			});
		} catch (IOException e) {
			throw new TransportException(e);
		}
	}

	private Long parseLong(Header header) {
		try {
			return Long.parseLong(header.getValue());
		} catch (Exception e) {
			return null;
		}
	}

	private Boolean parseBoolean(Header header) {
		if (header == null) {
			return null;
		}

		if ("true".equals(header.getValue())) {
			return true;
		}

		if ("false".equals(header.getValue())) {
			return false;
		}

		return null;
	}

}
@@ -34,70 +34,8 @@
 t;%0A%0A
-import java.io.IOException;%0Aimport java.nio.charset.Charset;%0A%0A
 impo
@@ -622,88 +622,430 @@
 ttp.
-util.EntityUtils;%0A%0Apublic class AbstractHttpTransport implements HttpTransport %7B
+params.BasicHttpParams;%0Aimport org.apache.http.params.HttpConnectionParams;%0Aimport org.apache.http.params.HttpParams;%0Aimport org.apache.http.util.EntityUtils;%0A%0Aimport java.io.IOException;%0Aimport java.nio.charset.Charset;%0A%0Apublic class AbstractHttpTransport implements HttpTransport %7B%0A%0A%09private static final int DEFAULT_CONNECTION_TIMEOUT = 10000; // 10 sec%0A%09private static final int DEFAULT_READ_TIMEOUT = 60000; // 60 sec
 %0A%0A%09p
@@ -1296,16 +1296,222 @@
 (500);%0A%0A
+%09%09HttpParams httpParams = new BasicHttpParams();%0A%09%09HttpConnectionParams.setConnectionTimeout(httpParams, DEFAULT_CONNECTION_TIMEOUT);%0A%09%09HttpConnectionParams.setSoTimeout(httpParams, DEFAULT_READ_TIMEOUT);%0A%0A
 %09%09this.h
@@ -1561,16 +1561,28 @@
 nManager
+, httpParams
 );%0A%09%7D%0A%0A%09

Fix #40