1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.http.transformers; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.transformer.AbstractMessageTransformer; |
16 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
import org.mule.transport.http.HttpConstants; |
18 | |
import org.mule.util.StringUtils; |
19 | |
|
20 | |
import java.net.URI; |
21 | |
import java.util.Arrays; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.apache.commons.codec.net.URLCodec; |
27 | |
|
28 | |
public class HttpRequestBodyToParamMap extends AbstractMessageTransformer |
29 | |
{ |
30 | |
public HttpRequestBodyToParamMap() |
31 | 0 | { |
32 | 0 | registerSourceType(DataTypeFactory.OBJECT); |
33 | 0 | setReturnDataType(DataTypeFactory.OBJECT); |
34 | 0 | } |
35 | |
|
36 | |
@Override |
37 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
38 | |
{ |
39 | 0 | Map<String, Object> paramMap = new HashMap<String, Object>(); |
40 | |
|
41 | |
try |
42 | |
{ |
43 | 0 | String httpMethod = message.getInboundProperty("http.method"); |
44 | 0 | String contentType = message.getInboundProperty("Content-Type"); |
45 | |
|
46 | 0 | boolean isGet = HttpConstants.METHOD_GET.equalsIgnoreCase(httpMethod); |
47 | 0 | boolean isPost = HttpConstants.METHOD_POST.equalsIgnoreCase(httpMethod); |
48 | 0 | boolean isUrlEncoded = false; |
49 | 0 | if (contentType != null) |
50 | |
{ |
51 | 0 | isUrlEncoded = contentType.startsWith("application/x-www-form-urlencoded"); |
52 | |
} |
53 | |
|
54 | 0 | if (!(isGet || (isPost && isUrlEncoded))) |
55 | |
{ |
56 | 0 | throw new Exception("The HTTP method or content type is unsupported!"); |
57 | |
} |
58 | |
|
59 | 0 | String queryString = null; |
60 | 0 | if (isGet) |
61 | |
{ |
62 | 0 | URI uri = new URI(message.getPayloadAsString(outputEncoding)); |
63 | 0 | queryString = uri.getRawQuery(); |
64 | 0 | } |
65 | 0 | else if (isPost) |
66 | |
{ |
67 | 0 | queryString = new String(message.getPayloadAsBytes()); |
68 | |
} |
69 | |
|
70 | 0 | if (StringUtils.isNotBlank(queryString)) |
71 | |
{ |
72 | 0 | addQueryStringToParameterMap(queryString, paramMap, outputEncoding); |
73 | |
} |
74 | |
} |
75 | 0 | catch (Exception e) |
76 | |
{ |
77 | 0 | throw new TransformerException(this, e); |
78 | 0 | } |
79 | |
|
80 | 0 | return paramMap; |
81 | |
} |
82 | |
|
83 | |
protected void addQueryStringToParameterMap(String queryString, Map<String, Object> paramMap, |
84 | |
String outputEncoding) throws Exception |
85 | |
{ |
86 | 0 | String[] pairs = queryString.split("&"); |
87 | 0 | for (String pair : pairs) |
88 | |
{ |
89 | 0 | String[] nameValue = pair.split("="); |
90 | 0 | if (nameValue.length == 2) |
91 | |
{ |
92 | 0 | URLCodec codec = new URLCodec(outputEncoding); |
93 | 0 | String key = codec.decode(nameValue[0]); |
94 | 0 | String value = codec.decode(nameValue[1]); |
95 | 0 | addToParameterMap(paramMap, key, value); |
96 | |
} |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | |
@SuppressWarnings("unchecked") |
101 | |
protected void addToParameterMap(Map<String, Object> paramMap, String key, String value) |
102 | |
{ |
103 | 0 | Object existingValue = paramMap.get(key); |
104 | 0 | if (existingValue != null) |
105 | |
{ |
106 | |
List<Object> values; |
107 | 0 | if (existingValue instanceof List<?>) |
108 | |
{ |
109 | 0 | values = (List<Object>) existingValue; |
110 | |
} |
111 | |
else |
112 | |
{ |
113 | 0 | values = Arrays.asList(existingValue); |
114 | |
} |
115 | |
|
116 | 0 | values.add(value); |
117 | 0 | paramMap.put(key, values); |
118 | 0 | } |
119 | |
else |
120 | |
{ |
121 | 0 | paramMap.put(key, value); |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
@Override |
126 | |
public boolean isAcceptNull() |
127 | |
{ |
128 | 0 | return false; |
129 | |
} |
130 | |
} |