KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpResponse


1 /*
2  * $Id: HttpResponse.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import org.apache.commons.httpclient.ChunkedInputStream;
14 import org.apache.commons.httpclient.ContentLengthInputStream;
15 import org.apache.commons.httpclient.Header;
16 import org.apache.commons.httpclient.HeaderElement;
17 import org.apache.commons.httpclient.HeaderGroup;
18 import org.apache.commons.httpclient.HttpStatus;
19 import org.apache.commons.httpclient.HttpVersion;
20 import org.apache.commons.httpclient.NameValuePair;
21 import org.apache.commons.httpclient.StatusLine;
22 import org.apache.commons.io.IOUtils;
23 import org.apache.commons.io.output.ByteArrayOutputStream;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * A generic HTTP response wrapper.
33  */

34 public class HttpResponse
35 {
36
37     public static final String JavaDoc DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
38
39     private HttpVersion ver = HttpVersion.HTTP_1_1;
40     private int statusCode = HttpStatus.SC_OK;
41     private String JavaDoc phrase = HttpStatus.getStatusText(HttpStatus.SC_OK);
42     private HeaderGroup headers = new HeaderGroup();
43     private InputStream entity = null;
44     private boolean keepAlive = false;
45     private boolean disableKeepAlive = false;
46     private String JavaDoc fallbackCharset = DEFAULT_CONTENT_CHARSET;
47
48     public HttpResponse()
49     {
50         super();
51     }
52
53     public HttpResponse(final StatusLine statusline, final Header[] headers, final InputStream content)
54         throws IOException JavaDoc
55     {
56         super();
57         if (statusline == null)
58         {
59             throw new IllegalArgumentException JavaDoc("Status line may not be null");
60         }
61         setStatusLine(HttpVersion.parse(statusline.getHttpVersion()), statusline.getStatusCode(),
62             statusline.getReasonPhrase());
63         setHeaders(headers);
64         if (content != null)
65         {
66             InputStream in = content;
67             Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
68             Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
69
70             if (transferEncoding != null)
71             {
72                 if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
73                 {
74                     in = new ChunkedInputStream(in);
75                 }
76             }
77             else if (contentLength != null)
78             {
79                 long len = getContentLength();
80                 if (len >= 0)
81                 {
82                     in = new ContentLengthInputStream(in, len);
83                 }
84             }
85             this.entity = in;
86         }
87     }
88
89     public void setStatusLine(final HttpVersion ver, int statuscode, final String JavaDoc phrase)
90     {
91         if (ver == null)
92         {
93             throw new IllegalArgumentException JavaDoc("HTTP version may not be null");
94         }
95         if (statuscode <= 0)
96         {
97             throw new IllegalArgumentException JavaDoc("Status code may not be negative or zero");
98         }
99         this.ver = ver;
100         this.statusCode = statuscode;
101         if (phrase != null)
102         {
103             this.phrase = phrase;
104         }
105         else
106         {
107             this.phrase = HttpStatus.getStatusText(statuscode);
108         }
109     }
110
111     public void setStatusLine(final HttpVersion ver, int statuscode)
112     {
113         setStatusLine(ver, statuscode, null);
114     }
115
116     public String JavaDoc getPhrase()
117     {
118         return this.phrase;
119     }
120
121     /**
122      * @deprecated use {@link #getStatusCode()} instead
123      * @return HTTP status code
124      */

125     public int getStatuscode()
126     {
127         return this.getStatusCode();
128     }
129
130     public int getStatusCode()
131     {
132         return this.statusCode;
133     }
134
135     public HttpVersion getHttpVersion()
136     {
137         return this.ver;
138     }
139
140     public String JavaDoc getStatusLine()
141     {
142         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(64);
143         buffer.append(this.ver);
144         buffer.append(' ');
145         buffer.append(this.statusCode);
146         if (this.phrase != null)
147         {
148             buffer.append(' ');
149             buffer.append(this.phrase);
150         }
151         return buffer.toString();
152     }
153
154     public boolean containsHeader(final String JavaDoc name)
155     {
156         return this.headers.containsHeader(name);
157     }
158
159     public Header[] getHeaders()
160     {
161         return this.headers.getAllHeaders();
162     }
163
164     public Header getFirstHeader(final String JavaDoc name)
165     {
166         return this.headers.getFirstHeader(name);
167     }
168
169     public void removeHeaders(final String JavaDoc s)
170     {
171         if (s == null)
172         {
173             return;
174         }
175         Header[] headers = this.headers.getHeaders(s);
176         for (int i = 0; i < headers.length; i++)
177         {
178             this.headers.removeHeader(headers[i]);
179         }
180     }
181
182     public void addHeader(final Header header)
183     {
184         if (header == null)
185         {
186             return;
187         }
188         this.headers.addHeader(header);
189     }
190
191     public void setHeader(final Header header)
192     {
193         if (header == null)
194         {
195             return;
196         }
197         removeHeaders(header.getName());
198         addHeader(header);
199     }
200
201     public void setHeaders(final Header[] headers)
202     {
203         if (headers == null)
204         {
205             return;
206         }
207         this.headers.setHeaders(headers);
208     }
209
210     public Iterator JavaDoc getHeaderIterator()
211     {
212         return this.headers.getIterator();
213     }
214
215     public String JavaDoc getCharset()
216     {
217         String JavaDoc charset = getFallbackCharset();
218         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
219         if (contenttype != null)
220         {
221             HeaderElement values[] = contenttype.getElements();
222             if (values.length == 1)
223             {
224                 NameValuePair param = values[0].getParameterByName("charset");
225                 if (param != null)
226                 {
227                     charset = param.getValue();
228                 }
229             }
230         }
231         return charset;
232     }
233
234     public long getContentLength()
235     {
236         Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
237         if (contentLength != null)
238         {
239             try
240             {
241                 return Long.parseLong(contentLength.getValue());
242             }
243             catch (NumberFormatException JavaDoc e)
244             {
245                 return -1;
246             }
247         }
248         else
249         {
250             return -1;
251         }
252     }
253
254     public void setBodyString(final String JavaDoc string)
255     {
256         if (string != null)
257         {
258             byte[] raw = null;
259             try
260             {
261                 raw = string.getBytes(getCharset());
262             }
263             catch (UnsupportedEncodingException JavaDoc e)
264             {
265                 raw = string.getBytes();
266             }
267             this.entity = new ByteArrayInputStream JavaDoc(raw);
268             if (!containsHeader(HttpConstants.HEADER_CONTENT_TYPE))
269             {
270                 setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE));
271             }
272             setHeader(new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(raw.length)));
273         }
274         else
275         {
276             this.entity = null;
277         }
278     }
279
280     public void setBody(final InputStream instream)
281     {
282         this.entity = instream;
283     }
284
285     public InputStream getBody()
286     {
287         return this.entity;
288     }
289
290     public byte[] getBodyBytes() throws IOException JavaDoc
291     {
292         InputStream in = getBody();
293         if (in != null)
294         {
295             ByteArrayOutputStream buffer = new ByteArrayOutputStream(8192);
296             IOUtils.copy(in, buffer);
297             return buffer.toByteArray();
298         }
299         else
300         {
301             return null;
302         }
303     }
304
305     public String JavaDoc getBodyString() throws IOException JavaDoc
306     {
307         byte[] raw = getBodyBytes();
308         if (raw != null)
309         {
310             return new String JavaDoc(raw, getCharset());
311         }
312         else
313         {
314             return null;
315         }
316     }
317
318     public boolean isKeepAlive()
319     {
320         return !disableKeepAlive && keepAlive;
321     }
322
323     public void setKeepAlive(boolean keepAlive)
324     {
325         this.keepAlive = keepAlive;
326     }
327
328     public void disableKeepAlive(boolean keepalive)
329     {
330         disableKeepAlive = keepalive;
331     }
332
333     public String JavaDoc getFallbackCharset()
334     {
335         return fallbackCharset;
336     }
337
338     public void setFallbackCharset(String JavaDoc overrideCharset)
339     {
340         this.fallbackCharset = overrideCharset;
341     }
342
343 }
344
Popular Tags