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