001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka.http;
018    
019    import java.io.InputStream;
020    import java.io.IOException;
021    
022    import org.apache.commons.httpclient.Header;
023    import org.apache.commons.httpclient.HttpMethod;
024    
025    /**
026     * An implementation of a Latka Response interface based on the Apache Commons
027     * HttpClient package.
028     *
029     * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
030     * @author <a href="mailto:mdelagra@us.britannica.com">Morgan Delagrange</a>
031     * @author dIon Gillard
032     * @version $Id: ResponseImpl.java 561366 2007-07-31 15:58:29Z rahul $
033     */
034    public class ResponseImpl implements Response {
035    
036        /** the request used to get the response */
037        protected RequestImpl _request;
038        /** the implementation of the http method used to create the response */
039        protected HttpMethod  _httpMethod;
040    
041        /**
042         * Create a Response
043         *
044         * @param request  the request that motivated this response
045         */
046        ResponseImpl(RequestImpl request) {
047            _request = request;
048            _httpMethod = request.getHttpMethod();
049        }
050    
051        /**
052         * Defined in interface
053         * @return the request associated with this response
054         * @see Response#getRequest()
055         */
056        public Request getRequest() {
057            return _request;
058        }
059    
060        ////////////////////////////////
061        // Response Interface Methods //
062        ////////////////////////////////
063    
064        /**
065         * @return the integer status code provided by the HTTP server.
066         */
067        public int getStatusCode() {
068            return _httpMethod.getStatusCode();
069        }
070    
071        /**
072         * @return the status text (or "reason phrase") associated with the response
073         */
074        public String getStatusText() {
075            return _httpMethod.getStatusText();
076        }
077    
078        /**
079         * Returns the resource, in string form, 
080         * provided by the HTTP server
081         * 
082         * @return the contents of the HTTP response body has a String,
083         *         or null if there was no response body
084         */
085        public String getResource() {
086            try {
087                return _httpMethod.getResponseBodyAsString();
088            } catch (Exception e) {
089                return null;
090            }
091        }
092    
093        /**
094         * Check a response header.  If more than one header
095         * of the same name is encountered, they are collapsed
096         * into one comma-separated String.
097         * 
098         * @param headerName The name of the header to find in the Reponse
099         * @return the value of the header(s), or null if the header does not
100         *         exist
101         */
102        public String getHeader(String headerName) {
103            Header header = _httpMethod.getResponseHeader(headerName);
104            if (header != null) {
105                return header.getValue();
106            }
107            return null;
108        }
109    
110        /**
111         * Returns the length of the Response stream (as bytes),
112         * or -1 if no stream is available
113         * 
114         * @return Byte length of the response stream
115         */
116        public int getByteLength() {
117            byte[] responseBytes = null;
118            try {
119                responseBytes = _httpMethod.getResponseBody();
120            } catch (Exception e) {
121            }
122            if (responseBytes == null) {
123                return -1;
124            }
125    
126            return responseBytes.length;
127        }
128    
129        /**
130         * Get the actual bytes returned by the web server
131         * 
132         * @return InputStream containing the HTTP response, or null
133         *         if the response contains no body
134         */
135        public InputStream getStream() {
136            InputStream stream;
137    
138            try {
139                stream = _httpMethod.getResponseBodyAsStream();
140            } catch (IOException ioX) {
141                stream = null;
142            }
143    
144            return stream;
145        }
146    }