1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.http.transformers; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.transformer.AbstractTransformer; |
16 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
import org.mule.transport.NullPayload; |
18 | |
import org.mule.transport.http.HttpConstants; |
19 | |
import org.mule.transport.http.ReleasingInputStream; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.apache.commons.httpclient.Header; |
27 | |
import org.apache.commons.httpclient.HttpMethod; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class HttpClientMethodResponseToObject extends AbstractTransformer |
35 | |
{ |
36 | |
|
37 | |
public HttpClientMethodResponseToObject() |
38 | 0 | { |
39 | 0 | registerSourceType(DataTypeFactory.create(HttpMethod.class)); |
40 | 0 | setReturnDataType(DataTypeFactory.MULE_MESSAGE); |
41 | 0 | } |
42 | |
|
43 | |
@Override |
44 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
45 | |
{ |
46 | |
Object msg; |
47 | 0 | HttpMethod httpMethod = (HttpMethod)src; |
48 | |
|
49 | |
InputStream is; |
50 | |
try |
51 | |
{ |
52 | 0 | is = httpMethod.getResponseBodyAsStream(); |
53 | |
} |
54 | 0 | catch (IOException e) |
55 | |
{ |
56 | 0 | throw new TransformerException(this, e); |
57 | 0 | } |
58 | |
|
59 | 0 | if (is == null) |
60 | |
{ |
61 | 0 | msg = NullPayload.getInstance(); |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | msg = new ReleasingInputStream(is, httpMethod); |
66 | |
} |
67 | |
|
68 | |
|
69 | 0 | Map headerProps = new HashMap(); |
70 | 0 | Header[] headers = httpMethod.getResponseHeaders(); |
71 | |
String name; |
72 | 0 | for (int i = 0; i < headers.length; i++) |
73 | |
{ |
74 | 0 | name = headers[i].getName(); |
75 | 0 | if (name.startsWith(HttpConstants.X_PROPERTY_PREFIX)) |
76 | |
{ |
77 | 0 | name = name.substring(2); |
78 | |
} |
79 | 0 | headerProps.put(name, headers[i].getValue()); |
80 | |
} |
81 | |
|
82 | |
|
83 | 0 | return new DefaultMuleMessage(msg, headerProps, muleContext); |
84 | |
} |
85 | |
} |