1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.http; |
12 | |
|
13 | |
import org.mule.RequestContext; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.transformer.TransformerException; |
17 | |
import org.mule.api.transport.OutputHandler; |
18 | |
import org.mule.transport.NullPayload; |
19 | |
|
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.OutputStream; |
24 | |
import java.io.UnsupportedEncodingException; |
25 | |
import java.util.Iterator; |
26 | |
|
27 | |
import org.apache.commons.httpclient.ChunkedInputStream; |
28 | |
import org.apache.commons.httpclient.ContentLengthInputStream; |
29 | |
import org.apache.commons.httpclient.Header; |
30 | |
import org.apache.commons.httpclient.HeaderElement; |
31 | |
import org.apache.commons.httpclient.HeaderGroup; |
32 | |
import org.apache.commons.httpclient.HttpStatus; |
33 | |
import org.apache.commons.httpclient.HttpVersion; |
34 | |
import org.apache.commons.httpclient.NameValuePair; |
35 | |
import org.apache.commons.httpclient.StatusLine; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class HttpResponse |
41 | |
{ |
42 | |
|
43 | |
public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; |
44 | |
|
45 | 160 | private HttpVersion ver = HttpVersion.HTTP_1_1; |
46 | 160 | private int statusCode = HttpStatus.SC_OK; |
47 | 160 | private String phrase = HttpStatus.getStatusText(HttpStatus.SC_OK); |
48 | 160 | private HeaderGroup headers = new HeaderGroup(); |
49 | 160 | private boolean keepAlive = false; |
50 | 160 | private boolean disableKeepAlive = false; |
51 | 160 | private String fallbackCharset = DEFAULT_CONTENT_CHARSET; |
52 | |
private OutputHandler outputHandler; |
53 | |
|
54 | |
public HttpResponse() |
55 | |
{ |
56 | 160 | super(); |
57 | 160 | } |
58 | |
|
59 | |
public HttpResponse(final StatusLine statusline, final Header[] headers, final InputStream content) |
60 | |
throws IOException |
61 | |
{ |
62 | 0 | super(); |
63 | 0 | if (statusline == null) |
64 | |
{ |
65 | 0 | throw new IllegalArgumentException("Status line may not be null"); |
66 | |
} |
67 | 0 | setStatusLine(HttpVersion.parse(statusline.getHttpVersion()), statusline.getStatusCode(), |
68 | |
statusline.getReasonPhrase()); |
69 | 0 | setHeaders(headers); |
70 | 0 | if (content != null) |
71 | |
{ |
72 | 0 | InputStream in = content; |
73 | 0 | Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH); |
74 | 0 | Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); |
75 | |
|
76 | 0 | if (transferEncoding != null) |
77 | |
{ |
78 | 0 | if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) |
79 | |
{ |
80 | 0 | in = new ChunkedInputStream(in); |
81 | |
} |
82 | |
} |
83 | 0 | else if (contentLength != null) |
84 | |
{ |
85 | 0 | long len = getContentLength(); |
86 | 0 | if (len >= 0) |
87 | |
{ |
88 | 0 | in = new ContentLengthInputStream(in, len); |
89 | |
} |
90 | |
} |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
public void setStatusLine(final HttpVersion ver, int statuscode, final String phrase) |
95 | |
{ |
96 | 160 | if (ver == null) |
97 | |
{ |
98 | 0 | throw new IllegalArgumentException("HTTP version may not be null"); |
99 | |
} |
100 | 160 | if (statuscode <= 0) |
101 | |
{ |
102 | 0 | throw new IllegalArgumentException("Status code may not be negative or zero"); |
103 | |
} |
104 | 160 | this.ver = ver; |
105 | 160 | this.statusCode = statuscode; |
106 | 160 | if (phrase != null) |
107 | |
{ |
108 | 0 | this.phrase = phrase; |
109 | |
} |
110 | |
else |
111 | |
{ |
112 | 160 | this.phrase = HttpStatus.getStatusText(statuscode); |
113 | |
} |
114 | 160 | } |
115 | |
|
116 | |
public void setStatusLine(final HttpVersion ver, int statuscode) |
117 | |
{ |
118 | 160 | setStatusLine(ver, statuscode, null); |
119 | 160 | } |
120 | |
|
121 | |
public String getPhrase() |
122 | |
{ |
123 | 0 | return this.phrase; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public int getStatuscode() |
131 | |
{ |
132 | 0 | return this.getStatusCode(); |
133 | |
} |
134 | |
|
135 | |
public int getStatusCode() |
136 | |
{ |
137 | 0 | return this.statusCode; |
138 | |
} |
139 | |
|
140 | |
public HttpVersion getHttpVersion() |
141 | |
{ |
142 | 154 | return this.ver; |
143 | |
} |
144 | |
|
145 | |
public String getStatusLine() |
146 | |
{ |
147 | 158 | StringBuffer buffer = new StringBuffer(64); |
148 | 158 | buffer.append(this.ver); |
149 | 158 | buffer.append(' '); |
150 | 158 | buffer.append(this.statusCode); |
151 | 158 | if (this.phrase != null) |
152 | |
{ |
153 | 158 | buffer.append(' '); |
154 | 158 | buffer.append(this.phrase); |
155 | |
} |
156 | 158 | return buffer.toString(); |
157 | |
} |
158 | |
|
159 | |
public boolean containsHeader(final String name) |
160 | |
{ |
161 | 622 | return this.headers.containsHeader(name); |
162 | |
} |
163 | |
|
164 | |
public Header[] getHeaders() |
165 | |
{ |
166 | 0 | return this.headers.getAllHeaders(); |
167 | |
} |
168 | |
|
169 | |
public Header getFirstHeader(final String name) |
170 | |
{ |
171 | 144 | return this.headers.getFirstHeader(name); |
172 | |
} |
173 | |
|
174 | |
public void removeHeaders(final String s) |
175 | |
{ |
176 | 790 | if (s == null) |
177 | |
{ |
178 | 0 | return; |
179 | |
} |
180 | 790 | Header[] headers = this.headers.getHeaders(s); |
181 | 794 | for (int i = 0; i < headers.length; i++) |
182 | |
{ |
183 | 4 | this.headers.removeHeader(headers[i]); |
184 | |
} |
185 | 790 | } |
186 | |
|
187 | |
public void addHeader(final Header header) |
188 | |
{ |
189 | 806 | if (header == null) |
190 | |
{ |
191 | 0 | return; |
192 | |
} |
193 | 806 | this.headers.addHeader(header); |
194 | 806 | } |
195 | |
|
196 | |
public void setHeader(final Header header) |
197 | |
{ |
198 | 788 | if (header == null) |
199 | |
{ |
200 | 0 | return; |
201 | |
} |
202 | 788 | removeHeaders(header.getName()); |
203 | 788 | addHeader(header); |
204 | 788 | } |
205 | |
|
206 | |
public void setHeaders(final Header[] headers) |
207 | |
{ |
208 | 0 | if (headers == null) |
209 | |
{ |
210 | 0 | return; |
211 | |
} |
212 | 0 | this.headers.setHeaders(headers); |
213 | 0 | } |
214 | |
|
215 | |
public Iterator getHeaderIterator() |
216 | |
{ |
217 | 158 | return this.headers.getIterator(); |
218 | |
} |
219 | |
|
220 | |
public String getCharset() |
221 | |
{ |
222 | 146 | String charset = getFallbackCharset(); |
223 | 146 | Header contenttype = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE); |
224 | 146 | if (contenttype != null) |
225 | |
{ |
226 | 142 | HeaderElement values[] = contenttype.getElements(); |
227 | 142 | if (values.length == 1) |
228 | |
{ |
229 | 142 | NameValuePair param = values[0].getParameterByName("charset"); |
230 | 142 | if (param != null) |
231 | |
{ |
232 | 116 | charset = param.getValue(); |
233 | |
} |
234 | |
} |
235 | |
} |
236 | 146 | return charset; |
237 | |
} |
238 | |
|
239 | |
public long getContentLength() |
240 | |
{ |
241 | 0 | Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH); |
242 | 0 | if (contentLength != null) |
243 | |
{ |
244 | |
try |
245 | |
{ |
246 | 0 | return Long.parseLong(contentLength.getValue()); |
247 | |
} |
248 | 0 | catch (NumberFormatException e) |
249 | |
{ |
250 | 0 | return -1; |
251 | |
} |
252 | |
} |
253 | |
else |
254 | |
{ |
255 | 0 | return -1; |
256 | |
} |
257 | |
} |
258 | |
|
259 | |
public boolean hasBody() |
260 | |
{ |
261 | 18 | return outputHandler != null; |
262 | |
} |
263 | |
|
264 | |
public OutputHandler getBody() throws TransformerException |
265 | |
{ |
266 | 158 | return outputHandler; |
267 | |
} |
268 | |
|
269 | |
public void setBody(MuleMessage msg) throws TransformerException |
270 | |
{ |
271 | 152 | if (msg == null) return; |
272 | |
|
273 | 152 | Object payload = msg.getPayload(); |
274 | 152 | if (payload instanceof String) |
275 | |
{ |
276 | 142 | setBody(payload.toString()); |
277 | |
} |
278 | 10 | else if (payload instanceof NullPayload) |
279 | |
{ |
280 | 10 | return; |
281 | |
} |
282 | 0 | else if (payload instanceof byte[]) |
283 | |
{ |
284 | 0 | setBody((byte[]) payload); |
285 | |
} |
286 | |
else |
287 | |
{ |
288 | 0 | setBody((OutputHandler) msg.getPayload(OutputHandler.class)); |
289 | |
} |
290 | 142 | } |
291 | |
|
292 | |
public void setBody(OutputHandler outputHandler) |
293 | |
{ |
294 | 0 | this.outputHandler = outputHandler; |
295 | 0 | } |
296 | |
|
297 | |
public void setBody(final String string) |
298 | |
{ |
299 | |
byte[] raw; |
300 | |
try |
301 | |
{ |
302 | 146 | raw = string.getBytes(getCharset()); |
303 | |
} |
304 | 0 | catch (UnsupportedEncodingException e) |
305 | |
{ |
306 | 0 | raw = string.getBytes(); |
307 | 146 | } |
308 | 146 | setBody(raw); |
309 | 146 | } |
310 | |
|
311 | |
private void setBody(final byte[] raw) |
312 | |
{ |
313 | 146 | if (!containsHeader(HttpConstants.HEADER_CONTENT_TYPE)) |
314 | |
{ |
315 | 4 | setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE)); |
316 | |
} |
317 | 146 | setHeader(new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(raw.length))); |
318 | |
|
319 | 146 | this.outputHandler = new OutputHandler() { |
320 | |
|
321 | 146 | public void write(MuleEvent event, OutputStream out) throws IOException |
322 | |
{ |
323 | 144 | out.write(raw); |
324 | 144 | } |
325 | |
|
326 | |
}; |
327 | 146 | } |
328 | |
|
329 | |
public String getBodyAsString() throws IOException |
330 | |
{ |
331 | 0 | if (!hasBody()) return ""; |
332 | |
|
333 | 0 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
334 | |
|
335 | 0 | outputHandler.write(RequestContext.getEvent(), out); |
336 | |
|
337 | |
try |
338 | |
{ |
339 | 0 | return new String(out.toByteArray(), getCharset()); |
340 | |
} |
341 | 0 | catch (UnsupportedEncodingException e) |
342 | |
{ |
343 | 0 | return new String(out.toByteArray()); |
344 | |
} |
345 | |
} |
346 | |
|
347 | |
public boolean isKeepAlive() |
348 | |
{ |
349 | 154 | return !disableKeepAlive && keepAlive; |
350 | |
} |
351 | |
|
352 | |
public void setKeepAlive(boolean keepAlive) |
353 | |
{ |
354 | 154 | this.keepAlive = keepAlive; |
355 | 154 | } |
356 | |
|
357 | |
public void disableKeepAlive(boolean keepalive) |
358 | |
{ |
359 | 146 | disableKeepAlive = keepalive; |
360 | 146 | } |
361 | |
|
362 | |
public String getFallbackCharset() |
363 | |
{ |
364 | 146 | return fallbackCharset; |
365 | |
} |
366 | |
|
367 | |
public void setFallbackCharset(String overrideCharset) |
368 | |
{ |
369 | 146 | this.fallbackCharset = overrideCharset; |
370 | 146 | } |
371 | |
|
372 | |
} |