/*
   Copyright 2017-2018 Integration Partners

   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 nl.nn.adapterframework.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.nn.adapterframework.util.Misc;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;

public class HttpResponseHandler {
	private HttpResponse httpResponse;
	private HttpEntity httpEntity;
	private InputStream content;
	private String contentAsString = null;
	private int statusCode = -1;

	public HttpResponseHandler(HttpResponse resp) throws IllegalStateException, IOException {
		httpResponse = resp;
		httpEntity = httpResponse.getEntity();
		content = httpEntity.getContent();
	}

	public StatusLine getStatusLine() {
		return httpResponse.getStatusLine();
	}

	public Header[] getAllHeaders() {
		return httpResponse.getAllHeaders();
	}

	/**
	 * Returns an {@link ReleaseConnectionAfterReadInputStream InputStream} that will automatically close the HttpRequest when fully read
	 * @return an {@link ReleaseConnectionAfterReadInputStream InputStream} retrieved from {@link HttpEntity#getContent()}
	 * @throws IOException
	 */
	public InputStream getResponse() throws IOException {
		statusCode = getStatusLine().getStatusCode();

		return new ReleaseConnectionAfterReadInputStream(this, content);
	}

	public String getResponseAsString() throws IOException {
		return getResponseAsString(false);
	}

	public String getResponseAsString(boolean returnNullonFault) throws IOException {
		if(statusCode < 0)
			contentAsString = Misc.streamToString(getResponse(), "\n", getContentType(), false);
		else if(returnNullonFault && statusCode == 500)
			return "";

		return contentAsString;
	}

	public String getHeader(String header) {
		return httpResponse.getFirstHeader(header).getValue();
	}

	public String getContentType() {
		Header contentTypeHeader = this.getFirstHeader(HttpHeaders.CONTENT_TYPE);
		ContentType contentType;
		if (contentTypeHeader != null) {
			contentType = ContentType.parse(contentTypeHeader.getValue());
		} else {
			contentType = ContentType.getOrDefault(httpEntity);
		}
		String charSet = Misc.DEFAULT_INPUT_STREAM_ENCODING;
		if(contentType != null && contentType.getCharset() != null)
			charSet = contentType.getCharset().displayName();
		return charSet;
	}

	/**
	 * Consumes the {@link HttpEntity} and will release the connection.
	 * @throws IOException
	 */
	public void close() throws IOException {
		EntityUtils.consume(httpEntity);
		content.close();
	}

	public Header getFirstHeader(String string) {
		return httpResponse.getFirstHeader(string);
	}

	public Map<String, List<String>> getHeaderFields() {
		Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
		Header[] headers = httpResponse.getAllHeaders();
		for (int i = 0; i < headers.length; i++) {
			Header header = headers[i];
			String name = header.getName().toLowerCase();
			List<String> value;
			if(headerMap.containsKey(name)) {
				value = headerMap.get(name);
			}
			else {
				value = new ArrayList<String>();
			}
			value.add(header.getValue());
			headerMap.put(name, value);
		}
		return headerMap;
	}
}

@@ -1408,16 +1408,58 @@
 = resp;%0A
+%09%09if(httpResponse.getEntity() != null) %7B%0A%09
 %09%09httpEn
@@ -1491,16 +1491,17 @@
 tity();%0A
+%09
 %09%09conten
@@ -1525,24 +1525,28 @@
 tContent();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09public 

Fix NPE when http result has no entity