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