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