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