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