/*
 *  Copyright 2015 Eric F. Savage, code@efsavage.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.ajah.http;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;

import lombok.Getter;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.ajah.util.AjahUtils;
import com.ajah.util.ToStringable;

public class SimplePost {

	private CloseableHttpClient client;

	@Getter
	private String host;

	@Getter
	private String path;

	@Getter
	private ArrayList<NameValuePair> params;

	public SimplePost(CloseableHttpClient client) {
		this.client = client;
	}

	public void addParam(String name, String value) {
		if (this.params == null) {
			this.params = new ArrayList<>();
		}
		this.params.add(new BasicNameValuePair(name, value));
	}

	public String getResponseAsString() throws IOException {
		HttpPost post = new HttpPost(this.host + this.path);
		if (this.params != null) {
			post.setEntity(new UrlEncodedFormEntity(this.params));
		}
		try (final CloseableHttpResponse response = this.client.execute(post)) {
			final String string = EntityUtils.toString(response.getEntity());
			EntityUtils.consumeQuietly(response.getEntity());
			return string;
		}
	}

	/**
	 * Sets the host.
	 * 
	 * @param _host
	 *            The host, required.
	 * @return This instance.
	 */
	public SimplePost host(String _host) {
		AjahUtils.requireParam(_host, "host");
		this.host = _host;
		return this;
	}

	/**
	 * Sets the path.
	 * 
	 * @param _path
	 *            The URL path, not required.
	 * @return This instance.
	 */
	public SimplePost path(String _path) {
		this.path = _path;
		return this;
	}

	public void addParam(String name, BigDecimal value) {
		if (value != null) {
			addParam(name, value.toString());
		} else {
			addParam(name, (String) null);
		}
	}

	public void addParam(String name, int value) {
		addParam(name, String.valueOf(value));
	}

	public void addParam(String name, ToStringable value) {
		if (value != null) {
			addParam(name, value.toString());
		} else {
			addParam(name, (String) null);
		}
	}

}

@@ -14,16 +14,21 @@
 ght 2015
+-2016
  Eric F.
@@ -748,31 +748,8 @@
 t;%0A%0A
-import lombok.Getter;%0A%0A
 impo
@@ -1167,16 +1167,39 @@
 gable;%0A%0A
+import lombok.Getter;%0A%0A
 public c
@@ -1251,24 +1251,99 @@
 nt client;%0A%0A
+%09@Getter%0A%09private int statusCode;%0A%0A%09@Getter%0A%09private String statusReason;%0A%0A
 %09@Getter%0A%09pr
@@ -1446,16 +1446,111 @@
 arams;%0A%0A
+%09@Getter%0A%09private ArrayList%3CNameValuePair%3E headers;%0A%0A%09private String responseAsString = null;%0A%0A
 %09public 
@@ -1856,16 +1856,82 @@
 ption %7B%0A
+%09%09if (responseAsString != null) %7B%0A%09%09%09return responseAsString;%0A%09%09%7D%0A
 %09%09HttpPo
@@ -2074,80 +2074,338 @@
 %7D%0A%09%09
-try (final CloseableHttpResponse response = this.client.execute(post)) %7B
+if (this.headers != null) %7B%0A%09%09%09for (NameValuePair header : headers) %7B%0A%09%09%09%09post.setHeader(header.getName(), header.getValue());%0A%09%09%09%7D%0A%09%09%7D%0A%09%09try (final CloseableHttpResponse response = this.client.execute(post)) %7B%0A%09%09%09statusCode = response.getStatusLine().getStatusCode();%0A%09%09%09statusReason = response.getStatusLine().getReasonPhrase();
 %0A%09%09%09
@@ -2532,24 +2532,63 @@
 %09%09re
-turn string;%0A%09%09%7D
+sponseAsString = string;%0A%09%09%7D%0A%09%09return responseAsString;
 %0A%09%7D%0A
@@ -3454,10 +3454,194 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09public void addHeader(String name, String value) %7B%0A%09%09if (this.headers == null) %7B%0A%09%09%09this.headers = new ArrayList%3C%3E();%0A%09%09%7D%0A%09%09this.headers.add(new BasicNameValuePair(name, value));%0A%09%7D%0A%0A
 %7D%0A

Support headers, status line