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