001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.net.http.httpclient4;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.MalformedURLException;
024import java.net.URI;
025import java.net.URL;
026
027import lucee.commons.io.IOUtil;
028import lucee.commons.lang.StringUtil;
029import lucee.commons.net.http.HTTPResponse;
030import lucee.commons.net.http.HTTPResponseSupport;
031import lucee.commons.net.http.Header;
032
033import org.apache.http.HttpEntity;
034import org.apache.http.HttpResponse;
035import org.apache.http.client.methods.HttpUriRequest;
036import org.apache.http.protocol.ExecutionContext;
037import org.apache.http.protocol.HttpContext;
038
039public class HTTPResponse4Impl extends HTTPResponseSupport implements HTTPResponse {
040
041        HttpResponse rsp;
042        HttpUriRequest req;
043        private URL url;
044        private HttpContext context; 
045
046        public HTTPResponse4Impl(URL url,HttpContext context, HttpUriRequest req,HttpResponse rsp) {
047                this.url=url;
048                this.context=context;
049                this.req=req;
050                this.rsp=rsp;
051        }
052        
053        @Override
054        public String getContentAsString() throws IOException {
055                return getContentAsString(null);
056        }
057        
058
059        @Override
060        public String getContentAsString(String charset) throws IOException {
061                HttpEntity entity = rsp.getEntity();
062                InputStream is=null;
063                if(StringUtil.isEmpty(charset,true))charset=getCharset();
064                try{
065                        return IOUtil.toString(is=entity.getContent(), charset);
066                }
067                finally {
068                        IOUtil.closeEL(is);
069                }
070        }
071        
072        @Override
073        public InputStream getContentAsStream() throws IOException {
074                HttpEntity e = rsp.getEntity();
075                if(e==null) return  null;
076                return e.getContent();
077        }
078        
079        @Override
080        public byte[] getContentAsByteArray() throws IOException {
081                HttpEntity entity = rsp.getEntity();
082                InputStream is=null;
083                if(entity==null) return new byte[0];
084                try{
085                        return IOUtil.toBytes(is=entity.getContent());
086                }
087                finally {
088                        IOUtil.closeEL(is);
089                }
090        }
091
092        @Override
093        public Header getLastHeader(String name) {
094                org.apache.http.Header header = rsp.getLastHeader(name);
095                if(header!=null) return new HeaderWrap(header);
096                return null;
097        }
098        
099        @Override
100        public Header getLastHeaderIgnoreCase(String name) {
101                return getLastHeaderIgnoreCase(rsp, name);
102        }
103        public static Header getLastHeaderIgnoreCase(HttpResponse rsp,String name) {
104                org.apache.http.Header header = rsp.getLastHeader(name);
105                if(header!=null) return new HeaderWrap(header);
106                
107                org.apache.http.Header[] headers = rsp.getAllHeaders();
108                for(int i=headers.length-1;i>=0;i--){
109                        if(name.equalsIgnoreCase(headers[i].getName())){
110                                return new HeaderWrap(headers[i]);
111                        }
112                }
113                return null;
114        }
115
116        @Override
117        public URL getURL() {
118                try {
119                        return req.getURI().toURL();
120                } catch (MalformedURLException e) {
121                        return url;
122                }
123        }
124        
125        public URL getTargetURL() {
126                URL start = getURL();   
127                
128                HttpUriRequest req = (HttpUriRequest) context.getAttribute(
129                        ExecutionContext.HTTP_REQUEST);
130                        URI uri = req.getURI();
131                        String path=uri.getPath();
132                        String query=uri.getQuery();
133                        if(!StringUtil.isEmpty(query)) path+="?"+query;
134                        
135                        URL _url=start;
136                        try {
137                                _url = new URL(start.getProtocol(),start.getHost(),start.getPort(),path);
138                        } 
139                        catch (MalformedURLException e) {}
140                
141                
142                return _url;
143        }
144        
145        
146
147
148        @Override
149        public int getStatusCode() {
150                return rsp.getStatusLine().getStatusCode();
151        }
152        
153        @Override
154        public String getStatusText() {
155                return rsp.getStatusLine().getReasonPhrase();
156        }
157
158        @Override
159        public String getProtocolVersion() {
160                return rsp.getStatusLine().getProtocolVersion().toString();
161        }
162
163        @Override
164        public String getStatusLine() {
165                return rsp.getStatusLine().toString();
166        }
167
168        @Override
169        public Header[] getAllHeaders() {
170                org.apache.http.Header[] src = rsp.getAllHeaders();
171                if(src==null) return new Header[0];
172                Header[] trg=new Header[src.length];
173                for(int i=0;i<src.length;i++){
174                        trg[i]=new HeaderWrap(src[i]);
175                }
176                return trg;
177        }
178}