package railo.commons.net.http.httpclient4;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

import railo.commons.io.IOUtil;
import railo.commons.lang.StringUtil;
import railo.commons.net.http.HTTPResponse;
import railo.commons.net.http.HTTPResponseSupport;
import railo.commons.net.http.Header;

public class HTTPResponse4Impl extends HTTPResponseSupport implements HTTPResponse {

	HttpResponse rsp;
	HttpUriRequest req;
	private URL url;
	private HttpContext context; 

	public HTTPResponse4Impl(URL url,HttpContext context, HttpUriRequest req,HttpResponse rsp) {
		this.url=url;
		this.context=context;
		this.req=req;
		this.rsp=rsp;
	}
	
	@Override
	public String getContentAsString() throws IOException {
		return getContentAsString(null);
	}
	

	@Override
	public String getContentAsString(String charset) throws IOException {
		HttpEntity entity = rsp.getEntity();
		InputStream is=null;
		if(StringUtil.isEmpty(charset,true))charset=getCharset();
		try{
			return IOUtil.toString(is=entity.getContent(), charset);
		}
		finally {
			IOUtil.closeEL(is);
		}
	}
	
	@Override
	public InputStream getContentAsStream() throws IOException {
		HttpEntity e = rsp.getEntity();
		if(e==null) return  null;
		return e.getContent();
	}
	
	@Override
	public byte[] getContentAsByteArray() throws IOException {
		HttpEntity entity = rsp.getEntity();
		InputStream is=null;
		try{
			return IOUtil.toBytes(is=entity.getContent());
		}
		finally {
			IOUtil.closeEL(is);
		}
	}
	
	/*public ContentType getContentType() {
		Header header = getLastHeaderIgnoreCase("Content-Type");
		if(header==null) return null;
		
		String[] mimeCharset = HTTPUtil.splitMimeTypeAndCharset(header.getValue());
		String[] typeSub = HTTPUtil.splitTypeAndSubType(mimeCharset[0]);
		return new ContentTypeImpl(typeSub[0],typeSub[1],mimeCharset[1]);
	}*/
	
	@Override
	public Header getLastHeader(String name) {
		org.apache.http.Header header = rsp.getLastHeader(name);
		if(header!=null) return new HeaderWrap(header);
		return null;
	}
	
	@Override
	public Header getLastHeaderIgnoreCase(String name) {
		return getLastHeaderIgnoreCase(rsp, name);
	}
	public static Header getLastHeaderIgnoreCase(HttpResponse rsp,String name) {
		org.apache.http.Header header = rsp.getLastHeader(name);
		if(header!=null) return new HeaderWrap(header);
		
		org.apache.http.Header[] headers = rsp.getAllHeaders();
		for(int i=headers.length-1;i>=0;i--){
			if(name.equalsIgnoreCase(headers[i].getName())){
				return new HeaderWrap(headers[i]);
			}
		}
		return null;
	}
	
	/*public String getCharset() {
		ContentType ct = getContentType();
		String charset=null;
		if(ct!=null)charset=ct.getCharset();
		if(!StringUtil.isEmpty(charset)) return charset;
		
		PageContext pc = ThreadLocalPageContext.get();
		if(pc!=null) return pc.getConfig().getWebCharset();
		return "ISO-8859-1";
	}*/
	
	/*public long getContentLength() throws IOException {
		
		
		Header ct = getLastHeaderIgnoreCase("Content-Length");
		if(ct!=null) return Caster.toLongValue(ct.getValue(),-1);
		
		
		HttpEntity entity = rsp.getEntity();
		InputStream is=null;
		long length=0;
		try{
			is=entity.getContent();
			byte[] buffer = new byte[1024];
		    int len;
		    
		    while((len = is.read(buffer)) !=-1){
		      length+=len;
		    }
			return length;
		}
		finally {
			IOUtil.closeEL(is);
		}
	}*/
	
	@Override
	public URL getURL() {
		try {
			return req.getURI().toURL();
		} catch (MalformedURLException e) {
			return url;
		}
	}
	
	public URL getTargetURL() {
		URL start = getURL(); 	
		
		HttpUriRequest req = (HttpUriRequest) context.getAttribute(
	                ExecutionContext.HTTP_REQUEST);
		 	URI uri = req.getURI();
		 	String path=uri.getPath();
		 	String query=uri.getQuery();
		 	if(!StringUtil.isEmpty(query)) path+="?"+query;
		 	
		 	URL _url=start;
			try {
				_url = new URL(start.getProtocol(),start.getHost(),start.getPort(),path);
			} 
			catch (MalformedURLException e) {}
	        
	        
		return _url;
	}
	
	


	@Override
	public int getStatusCode() {
		return rsp.getStatusLine().getStatusCode();
	}
	
	@Override
	public String getStatusText() {
		return rsp.getStatusLine().getReasonPhrase();
	}

	@Override
	public String getProtocolVersion() {
		return rsp.getStatusLine().getProtocolVersion().toString();
	}

	@Override
	public String getStatusLine() {
		return rsp.getStatusLine().toString();
	}

	@Override
	public Header[] getAllHeaders() {
		org.apache.http.Header[] src = rsp.getAllHeaders();
		if(src==null) return new Header[0];
		Header[] trg=new Header[src.length];
		for(int i=0;i<src.length;i++){
			trg[i]=new HeaderWrap(src[i]);
		}
		return trg;
	}
}

@@ -1782,364 +1782,8 @@
 %0A%09%7D%0A
-%09%0A%09/*public ContentType getContentType() %7B%0A%09%09Header header = getLastHeaderIgnoreCase(%22Content-Type%22);%0A%09%09if(header==null) return null;%0A%09%09%0A%09%09String%5B%5D mimeCharset = HTTPUtil.splitMimeTypeAndCharset(header.getValue());%0A%09%09String%5B%5D typeSub = HTTPUtil.splitTypeAndSubType(mimeCharset%5B0%5D);%0A%09%09return new ContentTypeImpl(typeSub%5B0%5D,typeSub%5B1%5D,mimeCharset%5B1%5D);%0A%09%7D*/%0A%09
 %0A%09@O
@@ -2486,821 +2486,8 @@
 %0A%09%7D%0A
-%09%0A%09/*public String getCharset() %7B%0A%09%09ContentType ct = getContentType();%0A%09%09String charset=null;%0A%09%09if(ct!=null)charset=ct.getCharset();%0A%09%09if(!StringUtil.isEmpty(charset)) return charset;%0A%09%09%0A%09%09PageContext pc = ThreadLocalPageContext.get();%0A%09%09if(pc!=null) return pc.getConfig().getWebCharset();%0A%09%09return %22ISO-8859-1%22;%0A%09%7D*/%0A%09%0A%09/*public long getContentLength() throws IOException %7B%0A%09%09%0A%09%09%0A%09%09Header ct = getLastHeaderIgnoreCase(%22Content-Length%22);%0A%09%09if(ct!=null) return Caster.toLongValue(ct.getValue(),-1);%0A%09%09%0A%09%09%0A%09%09HttpEntity entity = rsp.getEntity();%0A%09%09InputStream is=null;%0A%09%09long length=0;%0A%09%09try%7B%0A%09%09%09is=entity.getContent();%0A%09%09%09byte%5B%5D buffer = new byte%5B1024%5D;%0A%09%09    int len;%0A%09%09    %0A%09%09    while((len = is.read(buffer)) !=-1)%7B%0A%09%09      length+=len;%0A%09%09    %7D%0A%09%09%09return length;%0A%09%09%7D%0A%09%09finally %7B%0A%09%09%09IOUtil.closeEL(is);%0A%09%09%7D%0A%09%7D*/%0A%09
 %0A%09@O

remove unnecessary comments