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.MuleMessage; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.transformer.AbstractTransformer; |
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 | 2 | { |
39 | 2 | registerSourceType(HttpMethod.class); |
40 | 2 | setReturnClass(MuleMessage.class); |
41 | 2 | } |
42 | |
|
43 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
44 | |
{ |
45 | |
Object msg; |
46 | 0 | HttpMethod httpMethod = (HttpMethod)src; |
47 | |
|
48 | |
InputStream is; |
49 | |
try |
50 | |
{ |
51 | 0 | is = httpMethod.getResponseBodyAsStream(); |
52 | |
} |
53 | 0 | catch (IOException e) |
54 | |
{ |
55 | 0 | throw new TransformerException(this, e); |
56 | 0 | } |
57 | |
|
58 | 0 | if (is == null) |
59 | |
{ |
60 | 0 | msg = NullPayload.getInstance(); |
61 | |
} |
62 | |
else |
63 | |
{ |
64 | 0 | msg = new ReleasingInputStream(is, httpMethod); |
65 | |
} |
66 | |
|
67 | |
|
68 | 0 | Map headerProps = new HashMap(); |
69 | 0 | Header[] headers = httpMethod.getResponseHeaders(); |
70 | |
String name; |
71 | 0 | for (int i = 0; i < headers.length; i++) |
72 | |
{ |
73 | 0 | name = headers[i].getName(); |
74 | 0 | if (name.startsWith(HttpConstants.X_PROPERTY_PREFIX)) |
75 | |
{ |
76 | 0 | name = name.substring(2); |
77 | |
} |
78 | 0 | headerProps.put(name, headers[i].getValue()); |
79 | |
} |
80 | |
|
81 | |
|
82 | 0 | return new DefaultMuleMessage(msg, headerProps); |
83 | |
} |
84 | |
} |