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