KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > http > ResponseImpl


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 JavaDoc;
20 import java.io.IOException JavaDoc;
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 Jakarta 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 155424 2005-02-26 13:09:29Z dirkv $
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     ResponseImpl(RequestImpl request) {
47         _request = request;
48         _httpMethod = request.getHttpMethod();
49     }
50
51     /**
52      * Defined in interface
53      * @return the request associated with this response
54      * @see Response#getRequest()
55      */

56     public Request getRequest() {
57         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         return _httpMethod.getStatusCode();
69     }
70
71     /**
72      * @return the status text (or "reason phrase") associated with the response
73      */

74     public String JavaDoc getStatusText() {
75         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 JavaDoc getResource() {
86         try {
87             return _httpMethod.getResponseBodyAsString();
88         } catch (Exception JavaDoc e) {
89             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 JavaDoc getHeader(String JavaDoc 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 JavaDoc 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 JavaDoc getStream() {
136         InputStream JavaDoc stream;
137
138         try {
139             stream = _httpMethod.getResponseBodyAsStream();
140         } catch (IOException JavaDoc ioX) {
141             stream = null;
142         }
143
144         return stream;
145     }
146 }
Popular Tags