Coverage Report - org.apache.commons.latka.http.ResponseImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponseImpl
0%
0/26
0%
0/4
2
 
 1  
 /*
 2  
  * Copyright 1999-2001,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.latka.http;
 18  
 
 19  
 import java.io.InputStream;
 20  
 import java.io.IOException;
 21  
 
 22  
 import org.apache.commons.httpclient.Header;
 23  
 import org.apache.commons.httpclient.HttpMethod;
 24  
 
 25  
 /**
 26  
  * An implementation of a Latka Response interface based on the Apache Commons
 27  
  * HttpClient package.
 28  
  *
 29  
  * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
 30  
  * @author <a href="mailto:mdelagra@us.britannica.com">Morgan Delagrange</a>
 31  
  * @author dIon Gillard
 32  
  * @version $Id: ResponseImpl.java 561366 2007-07-31 15:58:29Z rahul $
 33  
  */
 34  
 public class ResponseImpl implements Response {
 35  
 
 36  
     /** the request used to get the response */
 37  
     protected RequestImpl _request;
 38  
     /** the implementation of the http method used to create the response */
 39  
     protected HttpMethod  _httpMethod;
 40  
 
 41  
     /**
 42  
      * Create a Response
 43  
      *
 44  
      * @param request  the request that motivated this response
 45  
      */
 46  0
     ResponseImpl(RequestImpl request) {
 47  0
         _request = request;
 48  0
         _httpMethod = request.getHttpMethod();
 49  0
     }
 50  
 
 51  
     /**
 52  
      * Defined in interface
 53  
      * @return the request associated with this response
 54  
      * @see Response#getRequest()
 55  
      */
 56  
     public Request getRequest() {
 57  0
         return _request;
 58  
     }
 59  
 
 60  
     ////////////////////////////////
 61  
     // Response Interface Methods //
 62  
     ////////////////////////////////
 63  
 
 64  
     /**
 65  
      * @return the integer status code provided by the HTTP server.
 66  
      */
 67  
     public int getStatusCode() {
 68  0
         return _httpMethod.getStatusCode();
 69  
     }
 70  
 
 71  
     /**
 72  
      * @return the status text (or "reason phrase") associated with the response
 73  
      */
 74  
     public String getStatusText() {
 75  0
         return _httpMethod.getStatusText();
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns the resource, in string form, 
 80  
      * provided by the HTTP server
 81  
      * 
 82  
      * @return the contents of the HTTP response body has a String,
 83  
      *         or null if there was no response body
 84  
      */
 85  
     public String getResource() {
 86  
         try {
 87  0
             return _httpMethod.getResponseBodyAsString();
 88  0
         } catch (Exception e) {
 89  0
             return null;
 90  
         }
 91  
     }
 92  
 
 93  
     /**
 94  
      * Check a response header.  If more than one header
 95  
      * of the same name is encountered, they are collapsed
 96  
      * into one comma-separated String.
 97  
      * 
 98  
      * @param headerName The name of the header to find in the Reponse
 99  
      * @return the value of the header(s), or null if the header does not
 100  
      *         exist
 101  
      */
 102  
     public String getHeader(String headerName) {
 103  0
         Header header = _httpMethod.getResponseHeader(headerName);
 104  0
         if (header != null) {
 105  0
             return header.getValue();
 106  
         }
 107  0
         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  0
         byte[] responseBytes = null;
 118  
         try {
 119  0
             responseBytes = _httpMethod.getResponseBody();
 120  0
         } catch (Exception e) {
 121  0
         }
 122  0
         if (responseBytes == null) {
 123  0
             return -1;
 124  
         }
 125  
 
 126  0
         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  0
             stream = _httpMethod.getResponseBodyAsStream();
 140  0
         } catch (IOException ioX) {
 141  0
             stream = null;
 142  0
         }
 143  
 
 144  0
         return stream;
 145  
     }
 146  
 }