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.httpclient3;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.MalformedURLException;
024import java.net.URL;
025
026import lucee.commons.io.IOUtil;
027import lucee.commons.lang.StringUtil;
028import lucee.commons.net.http.HTTPResponseSupport;
029import lucee.commons.net.http.Header;
030
031import org.apache.commons.httpclient.HostConfiguration;
032import org.apache.commons.httpclient.HttpMethod;
033
034public class HTTPResponse3Impl extends HTTPResponseSupport  {
035
036        private HttpMethod rsp;
037        private URL url;
038
039        public HTTPResponse3Impl(HttpMethod rsp, URL url) {
040                this.rsp=rsp;
041                this.url=url;
042        }
043
044        @Override
045        public String getContentAsString() throws IOException {
046                return getContentAsString(getCharset());
047        }
048
049        @Override
050        public String getContentAsString(String charset) throws IOException {
051                InputStream is=null;
052                try{
053                        is=getContentAsStream();
054                        return IOUtil.toString(is,charset);
055                }
056                finally {
057                        IOUtil.closeEL(is);
058                }
059        }
060
061        @Override
062        public InputStream getContentAsStream() throws IOException {
063                return rsp.getResponseBodyAsStream();
064        }
065
066        @Override
067        public byte[] getContentAsByteArray() throws IOException {
068                InputStream is=null;
069                try{
070                        return IOUtil.toBytes(is=getContentAsStream());
071                }
072                finally {
073                        IOUtil.closeEL(is);
074                }
075        }
076
077        @Override
078        public Header getLastHeader(String name) {
079                return new HeaderWrap(rsp.getResponseHeader(name));
080        }
081
082        @Override
083        public Header getLastHeaderIgnoreCase(String name) {
084                org.apache.commons.httpclient.Header[] headers = rsp.getResponseHeaders();
085                for(int i=headers.length-1;i>=0;i--){
086                        if(headers[i].getName().equalsIgnoreCase(name)) return new HeaderWrap(headers[i]);
087                }
088                return null;
089        }
090
091        @Override
092        public URL getURL() {
093                HostConfiguration config = rsp.getHostConfiguration();
094                
095                try {
096                        String qs = rsp.getQueryString();
097                        if(StringUtil.isEmpty(qs))
098                                return new URL(config.getProtocol().getScheme(),config.getHost(),config.getPort(),rsp.getPath());
099                        return new URL(config.getProtocol().getScheme(),config.getHost(),config.getPort(),rsp.getPath()+"?"+qs);
100                } catch (MalformedURLException e) {
101                }
102                
103                return url;
104        }
105
106        @Override
107        public int getStatusCode() {
108                return rsp.getStatusCode();
109        }
110
111        @Override
112        public String getStatusText() {
113                return rsp.getStatusText();
114        }
115
116        @Override
117        public String getProtocolVersion() {
118                return rsp.getStatusLine().getHttpVersion();
119        }
120
121        @Override
122        public String getStatusLine() {
123                return rsp.getStatusLine().toString();
124        }
125
126        @Override
127        public Header[] getAllHeaders() {
128                org.apache.commons.httpclient.Header[] src = rsp.getResponseHeaders();
129                if(src==null) return new Header[0];
130                Header[] trg=new Header[src.length];
131                for(int i=0;i<src.length;i++){
132                        trg[i]=new HeaderWrap(src[i]);
133                }
134                return trg;
135        }
136
137
138}