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