1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.http.servlet; |
12 | |
|
13 | |
import org.mule.config.MuleProperties; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.impl.ThreadSafeAccess; |
16 | |
import org.mule.providers.AbstractMessageAdapter; |
17 | |
import org.mule.providers.http.HttpConstants; |
18 | |
import org.mule.providers.http.i18n.ServletMessages; |
19 | |
import org.mule.umo.MessagingException; |
20 | |
import org.mule.umo.provider.MessageTypeNotSupportedException; |
21 | |
import org.mule.umo.provider.UniqueIdNotSupportedException; |
22 | |
import org.mule.util.SystemUtils; |
23 | |
|
24 | |
import java.io.BufferedReader; |
25 | |
import java.io.IOException; |
26 | |
import java.util.Enumeration; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import javax.servlet.http.HttpServletRequest; |
31 | |
import javax.servlet.http.HttpSession; |
32 | |
|
33 | |
import org.apache.commons.io.IOUtils; |
34 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class HttpRequestMessageAdapter extends AbstractMessageAdapter |
42 | |
{ |
43 | |
|
44 | |
|
45 | |
|
46 | |
private static final long serialVersionUID = -4238448252206941125L; |
47 | |
|
48 | 0 | private Object message = null; |
49 | |
|
50 | |
private HttpServletRequest request; |
51 | |
|
52 | |
public HttpRequestMessageAdapter(Object message) throws MessagingException |
53 | 0 | { |
54 | 0 | if (message instanceof HttpServletRequest) |
55 | |
{ |
56 | 0 | setPayload((HttpServletRequest)message); |
57 | 0 | final Map parameterMap = request.getParameterMap(); |
58 | 0 | if (parameterMap != null && parameterMap.size() > 0) |
59 | |
{ |
60 | 0 | for (Iterator iterator = parameterMap.entrySet().iterator(); iterator.hasNext();) |
61 | |
{ |
62 | 0 | Map.Entry entry = (Map.Entry)iterator.next(); |
63 | 0 | String key = (String)entry.getKey(); |
64 | 0 | Object value = entry.getValue(); |
65 | 0 | if (value != null) |
66 | |
{ |
67 | 0 | if (value.getClass().isArray() && ((Object[])value).length == 1) |
68 | |
{ |
69 | 0 | setProperty(key, ((Object[])value)[0]); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | setProperty(key, value); |
74 | |
} |
75 | |
} |
76 | |
} |
77 | |
} |
78 | |
String key; |
79 | 0 | for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) |
80 | |
{ |
81 | 0 | key = (String)e.nextElement(); |
82 | 0 | properties.put(key, request.getAttribute(key)); |
83 | |
} |
84 | |
String realKey; |
85 | 0 | for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) |
86 | |
{ |
87 | 0 | key = (String)e.nextElement(); |
88 | 0 | realKey = key; |
89 | 0 | if (key.startsWith(HttpConstants.X_PROPERTY_PREFIX)) |
90 | |
{ |
91 | 0 | realKey = key.substring(2); |
92 | |
} |
93 | 0 | setProperty(realKey, request.getHeader(key)); |
94 | |
} |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | throw new MessageTypeNotSupportedException(message, getClass()); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
protected HttpRequestMessageAdapter(HttpRequestMessageAdapter template) |
103 | |
{ |
104 | 0 | super(template); |
105 | 0 | message = template.message; |
106 | 0 | request = template.request; |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public Object getPayload() |
115 | |
{ |
116 | 0 | return message; |
117 | |
} |
118 | |
|
119 | |
public boolean isBinary() |
120 | |
{ |
121 | 0 | return message instanceof byte[]; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public byte[] getPayloadAsBytes() throws Exception |
130 | |
{ |
131 | 0 | if (isBinary()) |
132 | |
{ |
133 | 0 | return (byte[])message; |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | 0 | return ((String)message).getBytes(); |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public String getPayloadAsString(String encoding) throws Exception |
150 | |
{ |
151 | 0 | if (isBinary()) |
152 | |
{ |
153 | 0 | return new String((byte[])message, encoding); |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | 0 | return (String)message; |
158 | |
} |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private void setPayload(HttpServletRequest message) throws MessagingException |
167 | |
{ |
168 | |
try |
169 | |
{ |
170 | |
|
171 | 0 | request = message; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | 0 | String payloadParam = (String)request |
185 | |
.getAttribute(AbstractReceiverServlet.PAYLOAD_PARAMETER_NAME); |
186 | |
|
187 | 0 | if (payloadParam == null) |
188 | |
{ |
189 | 0 | payloadParam = AbstractReceiverServlet.DEFAULT_PAYLOAD_PARAMETER_NAME; |
190 | |
} |
191 | 0 | String payload = request.getParameter(payloadParam); |
192 | 0 | if (payload == null) |
193 | |
{ |
194 | 0 | if (isText(request.getContentType())) |
195 | |
{ |
196 | 0 | BufferedReader reader = request.getReader(); |
197 | 0 | StringBuffer buffer = new StringBuffer(8192); |
198 | 0 | String line = reader.readLine(); |
199 | 0 | while (line != null) |
200 | |
{ |
201 | 0 | buffer.append(line); |
202 | 0 | line = reader.readLine(); |
203 | 0 | if (line != null) buffer.append(SystemUtils.LINE_SEPARATOR); |
204 | |
} |
205 | 0 | this.message = buffer.toString(); |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); |
210 | 0 | IOUtils.copy(request.getInputStream(), baos); |
211 | 0 | this.message = baos.toByteArray(); |
212 | |
} |
213 | |
} |
214 | |
else |
215 | |
{ |
216 | 0 | this.message = payload; |
217 | |
} |
218 | |
} |
219 | 0 | catch (IOException e) |
220 | |
{ |
221 | 0 | throw new MessagingException( |
222 | |
ServletMessages.failedToReadPayload(request.getRequestURL().toString()), e); |
223 | 0 | } |
224 | 0 | } |
225 | |
|
226 | |
public HttpServletRequest getRequest() |
227 | |
{ |
228 | 0 | return request; |
229 | |
} |
230 | |
|
231 | |
public String getUniqueId() |
232 | |
{ |
233 | 0 | HttpSession session = null; |
234 | |
|
235 | |
try |
236 | |
{ |
237 | |
|
238 | |
|
239 | 0 | session = getRequest().getSession(); |
240 | |
} |
241 | 0 | catch (Exception e) |
242 | |
{ |
243 | 0 | throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session")); |
244 | 0 | } |
245 | 0 | if (session == null) |
246 | |
{ |
247 | 0 | throw new UniqueIdNotSupportedException(this, CoreMessages.objectIsNull("Http session")); |
248 | |
} |
249 | 0 | return session.getId(); |
250 | |
} |
251 | |
|
252 | |
protected boolean isText(String contentType) |
253 | |
{ |
254 | 0 | if (contentType == null) |
255 | |
{ |
256 | 0 | return true; |
257 | |
} |
258 | 0 | return (contentType.startsWith("text/")); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public void setReplyTo(Object replyTo) |
270 | |
{ |
271 | 0 | if (replyTo != null && replyTo.toString().startsWith("http")) |
272 | |
{ |
273 | 0 | setProperty(HttpConstants.HEADER_LOCATION, replyTo); |
274 | |
} |
275 | 0 | setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, replyTo); |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public Object getReplyTo() |
287 | |
{ |
288 | 0 | String replyto = (String)getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
289 | 0 | if (replyto == null) |
290 | |
{ |
291 | 0 | replyto = (String)getProperty(HttpConstants.HEADER_LOCATION); |
292 | |
} |
293 | 0 | return replyto; |
294 | |
} |
295 | |
|
296 | |
public ThreadSafeAccess newThreadCopy() |
297 | |
{ |
298 | 0 | return new HttpRequestMessageAdapter(this); |
299 | |
} |
300 | |
|
301 | |
} |