1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.samples.hello; |
12 | |
|
13 | |
import org.mule.transformers.AbstractTransformer; |
14 | |
import org.mule.umo.transformer.TransformerException; |
15 | |
|
16 | |
import java.io.UnsupportedEncodingException; |
17 | |
import java.net.URLDecoder; |
18 | |
|
19 | |
public class HttpRequestToNameString extends AbstractTransformer |
20 | |
{ |
21 | |
private static final String NAME_REQUEST_PARAMETER = "name="; |
22 | |
|
23 | |
public HttpRequestToNameString() |
24 | |
{ |
25 | 0 | super(); |
26 | 0 | this.registerSourceType(String.class); |
27 | 0 | this.registerSourceType(byte[].class); |
28 | 0 | this.setReturnClass(NameString.class); |
29 | 0 | } |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
37 | |
{ |
38 | 0 | return new NameString(extractNameValue(extractRequestQuery(convertRequestToString(src, encoding)))); |
39 | |
|
40 | |
} |
41 | |
|
42 | |
private String convertRequestToString(Object src, String encoding) |
43 | |
{ |
44 | 0 | String srcAsString = null; |
45 | |
|
46 | 0 | if (src instanceof byte[]) |
47 | |
{ |
48 | 0 | if (encoding != null) |
49 | |
{ |
50 | |
try |
51 | |
{ |
52 | 0 | srcAsString = new String((byte[])src, encoding); |
53 | |
} |
54 | 0 | catch (UnsupportedEncodingException ex) |
55 | |
{ |
56 | 0 | srcAsString = new String((byte[])src); |
57 | 0 | } |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | srcAsString = new String((byte[])src); |
62 | |
} |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | srcAsString = src.toString(); |
67 | |
} |
68 | |
|
69 | 0 | return srcAsString; |
70 | |
} |
71 | |
|
72 | |
private String extractRequestQuery(String request) |
73 | |
{ |
74 | 0 | String requestQuery = null; |
75 | |
|
76 | 0 | if (request != null && request.length() > 0 && request.indexOf('?') != -1) |
77 | |
{ |
78 | 0 | requestQuery = request.substring(request.indexOf('?') + 1).trim(); |
79 | |
} |
80 | |
|
81 | 0 | return requestQuery; |
82 | |
} |
83 | |
|
84 | |
private String extractNameValue(String requestQuery) throws TransformerException |
85 | |
{ |
86 | 0 | String nameValue = null; |
87 | |
|
88 | 0 | if (requestQuery != null && requestQuery.length() > 0) |
89 | |
{ |
90 | 0 | int nameParameterPos = requestQuery.indexOf(NAME_REQUEST_PARAMETER); |
91 | 0 | if (nameParameterPos != -1) |
92 | |
{ |
93 | 0 | int nextParameterValuePos = requestQuery.indexOf('&'); |
94 | 0 | if (nextParameterValuePos == -1 || nextParameterValuePos < nameParameterPos) |
95 | |
{ |
96 | 0 | nextParameterValuePos = requestQuery.length(); |
97 | |
} |
98 | |
|
99 | 0 | nameValue = requestQuery.substring(nameParameterPos + NAME_REQUEST_PARAMETER.length(), nextParameterValuePos); |
100 | |
} |
101 | |
|
102 | 0 | if (nameValue != null && nameValue.length() > 0) |
103 | |
{ |
104 | |
try |
105 | |
{ |
106 | 0 | nameValue = URLDecoder.decode(nameValue, "UTF-8"); |
107 | |
} |
108 | 0 | catch (UnsupportedEncodingException uee) |
109 | |
{ |
110 | 0 | logger.error(uee.getMessage()); |
111 | 0 | } |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | if (nameValue == null) |
116 | |
{ |
117 | 0 | nameValue = ""; |
118 | |
} |
119 | |
|
120 | 0 | return nameValue; |
121 | |
} |
122 | |
} |