1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.atom.server; |
11 | |
|
12 | |
import org.mule.api.MuleEventContext; |
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.transport.PropertyScope; |
15 | |
import org.mule.transport.http.HttpConnector; |
16 | |
import org.mule.transport.http.HttpConstants; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.IOException; |
21 | |
import java.io.InputStream; |
22 | |
import java.io.InputStreamReader; |
23 | |
import java.io.Reader; |
24 | |
import java.io.StringReader; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collections; |
27 | |
import java.util.Date; |
28 | |
import java.util.List; |
29 | |
import java.util.Locale; |
30 | |
import java.util.Set; |
31 | |
|
32 | |
import javax.servlet.http.HttpSession; |
33 | |
|
34 | |
import org.apache.abdera.i18n.iri.IRI; |
35 | |
import org.apache.abdera.protocol.server.Provider; |
36 | |
import org.apache.abdera.protocol.server.RequestContext; |
37 | |
import org.apache.abdera.protocol.server.context.AbstractRequestContext; |
38 | |
|
39 | |
public class MuleRequestContext extends AbstractRequestContext implements RequestContext |
40 | |
{ |
41 | |
|
42 | |
private final MuleMessage request; |
43 | |
private HttpSession session; |
44 | |
private MuleEventContext event; |
45 | |
private IRI baseIri; |
46 | |
|
47 | |
public MuleRequestContext(Provider context, |
48 | |
MuleEventContext event, |
49 | |
MuleMessage request, |
50 | |
String contextPath, |
51 | |
IRI baseIri) |
52 | |
{ |
53 | 0 | super(context, |
54 | |
getMethod(request), |
55 | |
new IRI(contextPath), |
56 | |
baseIri); |
57 | |
|
58 | 0 | this.baseIri = baseIri; |
59 | 0 | this.request = request; |
60 | 0 | this.event = event; |
61 | |
|
62 | 0 | this.session = null; |
63 | |
|
64 | |
|
65 | 0 | principal = null; |
66 | 0 | subject = context.resolveSubject(this); |
67 | 0 | target = context.resolveTarget(this); |
68 | 0 | } |
69 | |
|
70 | |
private static String getMethod(MuleMessage request) |
71 | |
{ |
72 | 0 | return request.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, StringUtils.EMPTY); |
73 | |
} |
74 | |
|
75 | |
public Locale getPreferredLocale() |
76 | |
{ |
77 | 0 | return null; |
78 | |
} |
79 | |
|
80 | |
public Locale[] getPreferredLocales() |
81 | |
{ |
82 | 0 | return null; |
83 | |
} |
84 | |
|
85 | |
public String getTargetBasePath() |
86 | |
{ |
87 | 0 | return event.getEndpointURI().getPath(); |
88 | |
} |
89 | |
|
90 | |
public Object getProperty(Property property) |
91 | |
{ |
92 | 0 | switch (property) |
93 | |
{ |
94 | |
case SESSIONID: |
95 | 0 | return (session != null) ? session.getId() : null; |
96 | |
case SESSIONCREATED: |
97 | 0 | return (session != null) ? new Date(session.getCreationTime()) : null; |
98 | |
case SESSIONACCESSED: |
99 | 0 | return (session != null) ? new Date(session.getLastAccessedTime()) : null; |
100 | |
case SESSIONTIMEOUT: |
101 | 0 | return (session != null) ? session.getMaxInactiveInterval() : -1; |
102 | |
case CHARACTERENCODING: |
103 | 0 | return request.getEncoding(); |
104 | |
case LOCALES: |
105 | 0 | return null; |
106 | |
case PROTOCOL: |
107 | 0 | return request.getInboundProperty(HttpConnector.HTTP_VERSION_PROPERTY); |
108 | |
case REMOTEADDRESS: |
109 | 0 | return null; |
110 | |
case REMOTEHOST: |
111 | 0 | return baseIri.getHost(); |
112 | |
case REMOTEUSER: |
113 | 0 | return baseIri.getUserInfo(); |
114 | |
case SCHEME: |
115 | 0 | return baseIri.getScheme(); |
116 | |
case PRINCIPAL: |
117 | 0 | return null; |
118 | |
case AUTHTYPE: |
119 | 0 | return null; |
120 | |
case CONTENTLENGTH: |
121 | 0 | return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_LENGTH); |
122 | |
case CONTENTTYPE: |
123 | 0 | return request.getOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE); |
124 | |
case CONTEXTPATH: |
125 | 0 | return ""; |
126 | |
case LOCALADDR: |
127 | 0 | return ""; |
128 | |
case LOCALNAME: |
129 | 0 | return ""; |
130 | |
case SERVERNAME: |
131 | 0 | return ""; |
132 | |
case SERVERPORT: |
133 | 0 | return ""; |
134 | |
default: |
135 | 0 | throw new UnsupportedOperationException("Property " + property.name() + " is not supported."); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
public Reader getReader() throws IOException |
140 | |
{ |
141 | 0 | Object payload = request.getPayload(); |
142 | 0 | if (payload instanceof Reader) |
143 | |
{ |
144 | 0 | return (Reader) payload; |
145 | |
} |
146 | 0 | else if (payload instanceof InputStream) |
147 | |
{ |
148 | 0 | return new InputStreamReader((InputStream) payload); |
149 | |
} |
150 | 0 | else if (payload instanceof byte[]) |
151 | |
{ |
152 | 0 | return new InputStreamReader(new ByteArrayInputStream((byte[]) payload)); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | |
try |
157 | |
{ |
158 | 0 | return new StringReader(request.getPayloadAsString()); |
159 | |
} |
160 | 0 | catch (Exception e) |
161 | |
{ |
162 | 0 | IOException e2 = new IOException("Could not convert message to String."); |
163 | 0 | e2.initCause(e); |
164 | 0 | throw e2; |
165 | |
} |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
public InputStream getInputStream() throws IOException |
170 | |
{ |
171 | 0 | Object payload = request.getPayload(); |
172 | 0 | if (payload instanceof InputStream) |
173 | |
{ |
174 | 0 | return (InputStream) payload; |
175 | |
} |
176 | 0 | else if (payload instanceof byte[]) |
177 | |
{ |
178 | 0 | return new ByteArrayInputStream((byte[]) payload); |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | |
try |
183 | |
{ |
184 | 0 | return new ByteArrayInputStream(request.getPayloadAsString().getBytes()); |
185 | |
} |
186 | 0 | catch (Exception e) |
187 | |
{ |
188 | 0 | IOException e2 = new IOException("Could not convert message to String."); |
189 | 0 | e2.initCause(e); |
190 | 0 | throw e2; |
191 | |
} |
192 | |
} |
193 | |
} |
194 | |
|
195 | |
public RequestContext setAttribute(Scope scope, String name, Object value) |
196 | |
{ |
197 | 0 | switch (scope) |
198 | |
{ |
199 | |
case REQUEST: |
200 | |
|
201 | 0 | request.setProperty(name, value, PropertyScope.INBOUND); |
202 | 0 | break; |
203 | |
case SESSION: |
204 | 0 | event.getSession().setProperty(name, value); |
205 | |
break; |
206 | |
} |
207 | 0 | return this; |
208 | |
} |
209 | |
|
210 | |
public Object getAttribute(Scope scope, String name) |
211 | |
{ |
212 | 0 | switch (scope) |
213 | |
{ |
214 | |
case REQUEST: |
215 | 0 | return request.getInboundProperty(name); |
216 | |
case SESSION: |
217 | 0 | if (event.getSession() != null) |
218 | |
{ |
219 | 0 | return event.getSession().getProperty(name); |
220 | |
} |
221 | |
} |
222 | 0 | return null; |
223 | |
} |
224 | |
|
225 | |
@SuppressWarnings("unchecked") |
226 | |
public String[] getAttributeNames(Scope scope) |
227 | |
{ |
228 | 0 | switch (scope) |
229 | |
{ |
230 | |
case REQUEST: |
231 | 0 | Set names = request.getPropertyNames(); |
232 | 0 | return (String[]) names.toArray(new String[names.size()]); |
233 | |
case SESSION: |
234 | 0 | return new String[0]; |
235 | |
} |
236 | 0 | return null; |
237 | |
} |
238 | |
|
239 | |
public String getParameter(String name) |
240 | |
{ |
241 | 0 | return null; |
242 | |
} |
243 | |
|
244 | |
@SuppressWarnings("unchecked") |
245 | |
public String[] getParameterNames() |
246 | |
{ |
247 | 0 | return new String[0]; |
248 | |
} |
249 | |
|
250 | |
public List<String> getParameters(String name) |
251 | |
{ |
252 | 0 | return Collections.emptyList(); |
253 | |
} |
254 | |
|
255 | |
public Date getDateHeader(String name) |
256 | |
{ |
257 | |
|
258 | |
|
259 | 0 | throw new UnsupportedOperationException(); |
260 | |
} |
261 | |
|
262 | |
public String getHeader(String name) |
263 | |
{ |
264 | 0 | Object prop = request.getInboundProperty(name); |
265 | 0 | if (prop == null) |
266 | |
{ |
267 | 0 | return null; |
268 | |
} |
269 | 0 | return prop.toString(); |
270 | |
} |
271 | |
|
272 | |
@SuppressWarnings("unchecked") |
273 | |
public String[] getHeaderNames() |
274 | |
{ |
275 | 0 | Set propNames = request.getPropertyNames(); |
276 | 0 | return (String[]) propNames.toArray(new String[propNames.size()]); |
277 | |
} |
278 | |
|
279 | |
@SuppressWarnings("unchecked") |
280 | |
public Object[] getHeaders(String name) |
281 | |
{ |
282 | 0 | List<String> values = new ArrayList<String>(); |
283 | 0 | Set propNames = request.getPropertyNames(); |
284 | |
|
285 | 0 | for (Object n : propNames) |
286 | |
{ |
287 | 0 | Object prop = request.getProperty((String) n); |
288 | 0 | if (prop instanceof String) |
289 | |
{ |
290 | 0 | values.add((String) prop); |
291 | |
} |
292 | 0 | } |
293 | 0 | return values.toArray(); |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
public boolean isUserInRole(String role) |
311 | |
{ |
312 | 0 | return false; |
313 | |
} |
314 | |
|
315 | |
public String getContextPath() |
316 | |
{ |
317 | 0 | return event.getEndpointURI().getPath(); |
318 | |
} |
319 | |
} |