1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.http.transformers; |
12 | |
|
13 | |
import org.mule.providers.http.HttpConstants; |
14 | |
import org.mule.providers.http.HttpResponse; |
15 | |
import org.mule.providers.http.ResponseWriter; |
16 | |
import org.mule.transformers.AbstractTransformer; |
17 | |
import org.mule.umo.transformer.TransformerException; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.OutputStream; |
22 | |
import java.io.UnsupportedEncodingException; |
23 | |
import java.util.Iterator; |
24 | |
|
25 | |
import org.apache.commons.httpclient.ChunkedOutputStream; |
26 | |
import org.apache.commons.httpclient.Header; |
27 | |
import org.apache.commons.io.IOUtils; |
28 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class HttpResponseToString extends AbstractTransformer |
35 | |
{ |
36 | |
|
37 | |
public HttpResponseToString() |
38 | 6 | { |
39 | 10 | registerSourceType(HttpResponse.class); |
40 | 6 | setReturnClass(String.class); |
41 | 6 | } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
protected Object doTransform(Object src, String encoding) throws TransformerException |
47 | |
{ |
48 | |
try |
49 | |
{ |
50 | 6 | HttpResponse response = (HttpResponse)src; |
51 | 6 | ByteArrayOutputStream bos = new ByteArrayOutputStream(8192); |
52 | 6 | OutputStream outstream = bos; |
53 | 6 | ResponseWriter writer = new ResponseWriter(outstream, encoding); |
54 | 4 | writer.println(response.getStatusLine()); |
55 | 4 | Iterator item = response.getHeaderIterator(); |
56 | 14 | while (item.hasNext()) |
57 | |
{ |
58 | 10 | Header header = (Header)item.next(); |
59 | 10 | writer.print(header.toExternalForm()); |
60 | 10 | } |
61 | 4 | writer.println(); |
62 | 4 | writer.flush(); |
63 | |
|
64 | 4 | InputStream content = response.getBody(); |
65 | 4 | if (content != null) |
66 | |
{ |
67 | 4 | Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); |
68 | 4 | if (transferenc != null) |
69 | |
{ |
70 | 2 | response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); |
71 | 2 | if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) |
72 | |
{ |
73 | 2 | outstream = new ChunkedOutputStream(outstream); |
74 | |
} |
75 | |
} |
76 | |
|
77 | 4 | IOUtils.copy(content, outstream); |
78 | |
|
79 | 4 | if (outstream instanceof ChunkedOutputStream) |
80 | |
{ |
81 | 2 | ((ChunkedOutputStream)outstream).finish(); |
82 | |
} |
83 | |
} |
84 | |
|
85 | 4 | outstream.flush(); |
86 | 4 | bos.flush(); |
87 | 4 | byte[] result = bos.toByteArray(); |
88 | 4 | outstream.close(); |
89 | 4 | writer.close(); |
90 | 4 | bos.close(); |
91 | |
|
92 | 4 | String output = null; |
93 | |
try |
94 | |
{ |
95 | 4 | output = new String(result, encoding); |
96 | |
} |
97 | 0 | catch (UnsupportedEncodingException uee) |
98 | |
{ |
99 | |
|
100 | |
|
101 | |
|
102 | 0 | output = new String(result); |
103 | 4 | } |
104 | |
|
105 | 4 | return output; |
106 | |
} |
107 | 2 | catch (IOException e) |
108 | |
{ |
109 | 2 | throw new TransformerException(this, e); |
110 | |
} |
111 | |
} |
112 | |
} |