KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: HttpRequest.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.NameValuePair;
19 import org.apache.commons.io.IOUtils;
20 import org.apache.commons.io.output.ByteArrayOutputStream;
21 import org.mule.MuleManager;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * A http request wrapper
29  */

30 public class HttpRequest
31 {
32
33     private RequestLine requestLine = null;
34     private HeaderGroup headers = new HeaderGroup();
35     private InputStream entity = null;
36
37     public HttpRequest()
38     {
39         super();
40     }
41
42     public HttpRequest(final RequestLine requestLine, final Header[] headers, final InputStream content)
43         throws IOException JavaDoc
44     {
45         super();
46         if (requestLine == null)
47         {
48             throw new IllegalArgumentException JavaDoc("Request line may not be null");
49         }
50         this.requestLine = requestLine;
51         if (headers != null)
52         {
53             this.headers.setHeaders(headers);
54         }
55         if (content != null)
56         {
57             // only PUT and POST have content
58
String JavaDoc methodname = requestLine.getMethod();
59             if (HttpConstants.METHOD_POST.equalsIgnoreCase(methodname)
60                 || HttpConstants.METHOD_PUT.equalsIgnoreCase(methodname))
61             {
62                 Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
63                 Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
64                 InputStream in = content;
65                 if (transferEncoding != null)
66                 {
67                     if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1)
68                     {
69                         in = new ChunkedInputStream(in);
70                     }
71                 }
72                 else if (contentLength != null)
73                 {
74                     long len = getContentLength();
75                     if (len >= 0)
76                     {
77                         in = new ContentLengthInputStream(in, len);
78                     }
79                 }
80                 this.entity = in;
81             }
82         }
83     }
84
85     public HttpRequest(final RequestLine requestLine, final Header[] headers) throws IOException JavaDoc
86     {
87         this(requestLine, headers, null);
88     }
89
90     public RequestLine getRequestLine()
91     {
92         return this.requestLine;
93     }
94
95     public void setRequestLine(final RequestLine requestline)
96     {
97         if (requestline == null)
98         {
99             throw new IllegalArgumentException JavaDoc("Request line may not be null");
100         }
101         this.requestLine = requestline;
102     }
103
104     public boolean containsHeader(final String JavaDoc name)
105     {
106         return this.headers.containsHeader(name);
107     }
108
109     public Header[] getHeaders()
110     {
111         return this.headers.getAllHeaders();
112     }
113
114     public Header getFirstHeader(final String JavaDoc s)
115     {
116         return this.headers.getFirstHeader(s);
117     }
118
119     public void removeHeaders(final String JavaDoc s)
120     {
121         if (s == null)
122         {
123             return;
124         }
125         Header[] headers = this.headers.getHeaders(s);
126         for (int i = 0; i < headers.length; i++)
127         {
128             this.headers.removeHeader(headers[i]);
129         }
130     }
131
132     public void addHeader(final Header header)
133     {
134         if (header == null)
135         {
136             return;
137         }
138         this.headers.addHeader(header);
139     }
140
141     public void setHeader(final Header header)
142     {
143         if (header == null)
144         {
145             return;
146         }
147         removeHeaders(header.getName());
148         addHeader(header);
149     }
150
151     public Iterator JavaDoc getHeaderIterator()
152     {
153         return this.headers.getIterator();
154     }
155
156     public String JavaDoc getContentType()
157     {
158         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
159         if (contenttype != null)
160         {
161             return contenttype.getValue();
162         }
163         else
164         {
165             return HttpConstants.DEFAULT_CONTENT_TYPE;
166         }
167     }
168
169     public String JavaDoc getCharset()
170     {
171         String JavaDoc charset = null;
172         Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
173         if (contenttype != null)
174         {
175             HeaderElement values[] = contenttype.getElements();
176             if (values.length == 1)
177             {
178                 NameValuePair param = values[0].getParameterByName("charset");
179                 if (param != null)
180                 {
181                     charset = param.getValue();
182                 }
183             }
184         }
185         if (charset != null)
186         {
187             return charset;
188         }
189         else
190         {
191             return MuleManager.getConfiguration().getEncoding();
192         }
193     }
194
195     public long getContentLength()
196     {
197         Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
198         if (contentLength != null)
199         {
200             try
201             {
202                 return Long.parseLong(contentLength.getValue());
203             }
204             catch (NumberFormatException JavaDoc e)
205             {
206                 return -1;
207             }
208         }
209         else
210         {
211             return -1;
212         }
213     }
214
215     public InputStream getBody()
216     {
217         return this.entity;
218     }
219
220     public byte[] getBodyBytes() throws IOException JavaDoc
221     {
222         InputStream in = getBody();
223         if (in != null)
224         {
225             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
226             IOUtils.copy(in, buffer);
227             return buffer.toByteArray();
228         }
229         else
230         {
231             return null;
232         }
233     }
234
235     public String JavaDoc getBodyString() throws IOException JavaDoc
236     {
237         byte[] raw = getBodyBytes();
238         if (raw != null)
239         {
240             return new String JavaDoc(raw, getCharset());
241         }
242         else
243         {
244             return null;
245         }
246     }
247
248 }
249
Popular Tags