1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.servlet; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.ThreadSafeAccess; |
15 | |
import org.mule.api.config.MuleProperties; |
16 | |
import org.mule.api.transport.MessageTypeNotSupportedException; |
17 | |
import org.mule.transport.AbstractMessageAdapter; |
18 | |
import org.mule.transport.http.HttpConstants; |
19 | |
import org.mule.util.UUID; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Enumeration; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
import javax.servlet.http.HttpSession; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class HttpRequestMessageAdapter extends AbstractMessageAdapter |
35 | |
{ |
36 | |
|
37 | |
|
38 | |
|
39 | |
private static final long serialVersionUID = -4238448252206941125L; |
40 | |
|
41 | |
private HttpServletRequest request; |
42 | |
|
43 | |
public HttpRequestMessageAdapter(Object message) throws MessagingException |
44 | 6 | { |
45 | 6 | if (message instanceof HttpServletRequest) |
46 | |
{ |
47 | 6 | setPayload((HttpServletRequest)message); |
48 | 6 | setContentEncoding((HttpServletRequest)message); |
49 | |
|
50 | 6 | final Map parameterMap = request.getParameterMap(); |
51 | 6 | if (parameterMap != null && parameterMap.size() > 0) |
52 | |
{ |
53 | 0 | for (Iterator iterator = parameterMap.entrySet().iterator(); iterator.hasNext();) |
54 | |
{ |
55 | 0 | Map.Entry entry = (Map.Entry)iterator.next(); |
56 | 0 | String key = (String)entry.getKey(); |
57 | 0 | Object value = entry.getValue(); |
58 | 0 | if (value != null) |
59 | |
{ |
60 | 0 | if (value.getClass().isArray() && ((Object[])value).length == 1) |
61 | |
{ |
62 | 0 | setProperty(key, ((Object[])value)[0]); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | setProperty(key, value); |
67 | |
} |
68 | |
} |
69 | 0 | } |
70 | |
} |
71 | |
String key; |
72 | 6 | for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) |
73 | |
{ |
74 | 0 | key = (String)e.nextElement(); |
75 | 0 | properties.setProperty(key, request.getAttribute(key)); |
76 | |
} |
77 | |
String realKey; |
78 | 6 | for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) |
79 | |
{ |
80 | 0 | key = (String)e.nextElement(); |
81 | 0 | realKey = key; |
82 | 0 | if (key.startsWith(HttpConstants.X_PROPERTY_PREFIX)) |
83 | |
{ |
84 | 0 | realKey = key.substring(2); |
85 | |
} |
86 | 0 | setProperty(realKey, request.getHeader(key)); |
87 | |
} |
88 | 6 | } |
89 | |
else |
90 | |
{ |
91 | 0 | throw new MessageTypeNotSupportedException(message, getClass()); |
92 | |
} |
93 | 6 | } |
94 | |
|
95 | |
protected void setContentEncoding(HttpServletRequest request) |
96 | |
{ |
97 | 6 | String contentType = request.getContentType(); |
98 | 6 | if (contentType != null) |
99 | |
{ |
100 | 0 | int i = contentType.indexOf("charset"); |
101 | 0 | if (i > -1) |
102 | |
{ |
103 | 0 | int x = contentType.lastIndexOf(";"); |
104 | 0 | if (x > i) |
105 | |
{ |
106 | 0 | setEncoding(contentType.substring(i + 8, x)); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 0 | setEncoding(contentType.substring(i + 8)); |
111 | |
} |
112 | |
} |
113 | |
} |
114 | 6 | } |
115 | |
|
116 | |
protected HttpRequestMessageAdapter(HttpRequestMessageAdapter template) |
117 | |
{ |
118 | 0 | super(template); |
119 | 0 | request = template.request; |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public Object getPayload() |
128 | |
{ |
129 | |
try |
130 | |
{ |
131 | 10 | return request.getInputStream(); |
132 | |
} |
133 | 0 | catch (IOException e) |
134 | |
{ |
135 | 0 | throw new RuntimeException(e); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
public boolean isBinary() |
140 | |
{ |
141 | 0 | return !request.getContentType().startsWith("text"); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
private void setPayload(HttpServletRequest message) throws MessagingException |
150 | |
{ |
151 | 6 | request = message; |
152 | 6 | } |
153 | |
|
154 | |
public HttpServletRequest getRequest() |
155 | |
{ |
156 | 2 | return request; |
157 | |
} |
158 | |
|
159 | |
public String getUniqueId() |
160 | |
{ |
161 | 2 | HttpSession session = null; |
162 | |
|
163 | |
try |
164 | |
{ |
165 | |
|
166 | |
|
167 | 2 | session = getRequest().getSession(false); |
168 | |
} |
169 | 0 | catch (Exception e) |
170 | |
{ |
171 | 0 | return UUID.getUUID(); |
172 | |
|
173 | 2 | } |
174 | 2 | if (session == null) |
175 | |
{ |
176 | |
|
177 | 2 | return UUID.getUUID(); |
178 | |
} |
179 | 0 | return session.getId(); |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public void setReplyTo(Object replyTo) |
191 | |
{ |
192 | 0 | if (replyTo != null && replyTo.toString().startsWith("http")) |
193 | |
{ |
194 | 0 | setProperty(HttpConstants.HEADER_LOCATION, replyTo); |
195 | |
} |
196 | 0 | setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo); |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public Object getReplyTo() |
208 | |
{ |
209 | 0 | Object replyto = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
210 | 0 | if (replyto == null) |
211 | |
{ |
212 | 0 | replyto = getProperty(HttpConstants.HEADER_LOCATION); |
213 | |
} |
214 | 0 | return replyto; |
215 | |
} |
216 | |
|
217 | |
public ThreadSafeAccess newThreadCopy() |
218 | |
{ |
219 | 0 | return new HttpRequestMessageAdapter(this); |
220 | |
} |
221 | |
|
222 | |
} |