1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.portlet.servlet;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.UnsupportedEncodingException;
28 import java.security.Principal;
29 import java.util.Enumeration;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import javax.portlet.ActionRequest;
34 import javax.portlet.PortletContext;
35 import javax.portlet.PortletRequest;
36 import javax.portlet.PortletRequestDispatcher;
37 import javax.portlet.PortletSession;
38 import javax.servlet.RequestDispatcher;
39 import javax.servlet.ServletInputStream;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.http.Cookie;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpSession;
44
45 import org.apache.struts2.portlet.PortletActionConstants;
46
47 /***
48 * Wrapper object exposing a {@link PortletRequest} as a {@link HttpServletRequest} instance.
49 * Clients accessing this request object will in fact operate on the
50 * {@link PortletRequest} object wrapped by this request object.
51 */
52 public class PortletServletRequest implements HttpServletRequest, PortletActionConstants {
53
54 private PortletRequest portletRequest;
55 private PortletContext portletContext;
56
57 public PortletServletRequest(PortletRequest portletRequest, PortletContext portletContext) {
58 this.portletRequest = portletRequest;
59 this.portletContext = portletContext;
60 }
61
62
63
64
65 public String getAuthType() {
66 return portletRequest.getAuthType();
67 }
68
69
70
71
72 public String getContextPath() {
73 return portletRequest.getContextPath();
74 }
75
76 /***
77 * Not allowed in a portlet.
78 * @throws IllegalStateException Not allowed in a portlet.
79 */
80 public Cookie[] getCookies() {
81 if(portletRequest instanceof HttpServletRequest) {
82 return ((HttpServletRequest)portletRequest).getCookies();
83 }
84 throw new IllegalStateException("Not allowed in a portlet");
85 }
86
87 /***
88 * Not allowed in a portlet.
89 * @throws IllegalStateException Not allowed in a portlet.
90 */
91 public long getDateHeader(String name) {
92 throw new IllegalStateException("Not allowed in a portlet");
93 }
94
95 /***
96 * Gets a property from the {@link PortletRequest}. Note that a {@link PortletRequest} is not
97 * guaranteed to map properties to headers.
98 * @see PortletRequest#getProperty(String)
99 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
100 */
101 public String getHeader(String name) {
102 return portletRequest.getProperty(name);
103 }
104
105 /***
106 * Gets the property names from the {@link PortletRequest}. Note that a {@link PortletRequest} is not
107 * guaranteed to map properties to headers.
108 * @see PortletRequest#getPropertyNames()
109 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
110 */
111 public Enumeration getHeaderNames() {
112 return portletRequest.getPropertyNames();
113 }
114
115 /***
116 * Gets the values for the specified property from the {@link PortletRequest}. Note that a
117 * {@link PortletRequest} is not guaranteed to map properties to headers.
118 * @see PortletRequest#getProperties(String)
119 * @see HttpServletRequest#getHeaders(String)
120 */
121 public Enumeration getHeaders(String name) {
122 return portletRequest.getProperties(name);
123 }
124
125 /***
126 * Not allowed in a portlet.
127 * @throws IllegalStateException Not allowed in a portlet.
128 */
129 public int getIntHeader(String name) {
130 throw new IllegalStateException("Not allowed in a portlet");
131 }
132
133
134
135
136 public String getMethod() {
137 return null;
138 }
139
140
141
142
143 public String getPathInfo() {
144 return null;
145 }
146
147
148
149
150 public String getPathTranslated() {
151 return null;
152 }
153
154
155
156
157 public String getQueryString() {
158 return null;
159 }
160
161
162
163
164 public String getRemoteUser() {
165 return portletRequest.getRemoteUser();
166 }
167
168 /***
169 * Not allowed in a portlet.
170 * @throws IllegalStateException Not allowed in a portlet.
171 */
172 public String getRequestURI() {
173 throw new IllegalStateException("Not allowed in a portlet");
174 }
175
176 /***
177 * Not allowed in a portlet.
178 * @throws IllegalStateException Not allowed in a portlet.
179 */
180 public StringBuffer getRequestURL() {
181 throw new IllegalStateException("Not allowed in a portlet");
182 }
183
184
185
186
187 public String getRequestedSessionId() {
188 return portletRequest.getRequestedSessionId();
189 }
190
191 /***
192 * A {@link PortletRequest} has no servlet path. But for compatibility with Struts 2 components and
193 * interceptors, the action parameter on the request is mapped to the servlet path.
194 * @see javax.servlet.http.HttpServletRequest#getServletPath()
195 */
196 public String getServletPath() {
197 String actionPath = getParameter(ACTION_PARAM);
198 if(actionPath != null && !actionPath.endsWith(".action")) {
199 actionPath += ".action";
200 }
201 return actionPath;
202 }
203
204 /***
205 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
206 * @see javax.servlet.http.HttpServletRequest#getSession()
207 */
208 public HttpSession getSession() {
209 return new PortletHttpSession(portletRequest.getPortletSession());
210 }
211
212 /***
213 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
214 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
215 */
216 public HttpSession getSession(boolean create) {
217 return new PortletHttpSession(portletRequest.getPortletSession(create));
218 }
219
220
221
222
223 public Principal getUserPrincipal() {
224 return portletRequest.getUserPrincipal();
225 }
226
227 /***
228 * Not allowed in a portlet.
229 * @throws IllegalStateException Not allowed in a portlet.
230 */
231 public boolean isRequestedSessionIdFromCookie() {
232 throw new IllegalStateException("Not allowed in a portlet");
233 }
234
235 /***
236 * Not allowed in a portlet.
237 * @throws IllegalStateException Not allowed in a portlet.
238 */
239 public boolean isRequestedSessionIdFromURL() {
240 throw new IllegalStateException("Not allowed in a portlet");
241 }
242
243 /***
244 * Not allowed in a portlet.
245 * @throws IllegalStateException Not allowed in a portlet.
246 */
247 public boolean isRequestedSessionIdFromUrl() {
248 throw new IllegalStateException("Not allowed in a portlet");
249 }
250
251
252
253
254 public boolean isRequestedSessionIdValid() {
255 return portletRequest.isRequestedSessionIdValid();
256 }
257
258
259
260
261 public boolean isUserInRole(String role) {
262 return portletRequest.isUserInRole(role);
263 }
264
265 /***
266 * Gets an attribute value on the {@link PortletRequest}. If the attribute name is
267 * <tt>javax.servlet.include.servlet_path</tt>, it returns the same as
268 * {@link PortletServletRequest#getServletPath()}
269 * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
270 */
271 public Object getAttribute(String name) {
272 if("javax.servlet.include.servlet_path".equals(name)) {
273 return getServletPath();
274 }
275 else {
276 return portletRequest.getAttribute(name);
277 }
278 }
279
280
281
282
283 public Enumeration getAttributeNames() {
284 return portletRequest.getAttributeNames();
285 }
286
287 /***
288 * Can only be invoked in the event phase.
289 * @see ServletRequest#getCharacterEncoding()
290 * @throws IllegalStateException If the portlet is not in the event phase.
291 */
292 public String getCharacterEncoding() {
293 if(portletRequest instanceof ActionRequest) {
294 return ((ActionRequest)portletRequest).getCharacterEncoding();
295 }
296 else {
297 throw new IllegalStateException("Not allowed in render phase");
298 }
299 }
300
301 /***
302 * Can only be invoked in the event phase.
303 * @see ServletRequest#getContentLength()
304 * @throws IllegalStateException If the portlet is not in the event phase.
305 */
306 public int getContentLength() {
307 if(portletRequest instanceof ActionRequest) {
308 return ((ActionRequest)portletRequest).getContentLength();
309 }
310 else {
311 throw new IllegalStateException("Not allowed in render phase");
312 }
313 }
314
315 /***
316 * Can only be invoked in the event phase.
317 * @see ServletRequest#getContentType()
318 * @throws IllegalStateException If the portlet is not in the event phase.
319 */
320 public String getContentType() {
321 if(portletRequest instanceof ActionRequest) {
322 return ((ActionRequest)portletRequest).getContentType();
323 }
324 else {
325 throw new IllegalStateException("Not allowed in render phase");
326 }
327 }
328
329 /***
330 * Can only be invoked in the event phase. When invoked in the event phase, it will wrap the
331 * portlet's {@link InputStream} as a {@link PortletServletInputStream}.
332 * @see ServletRequest#getInputStream()
333 * @throws IllegalStateException If the portlet is not in the event phase.
334 */
335 public ServletInputStream getInputStream() throws IOException {
336 if(portletRequest instanceof ActionRequest) {
337 return new PortletServletInputStream(((ActionRequest)portletRequest).getPortletInputStream());
338 }
339 else {
340 throw new IllegalStateException("Not allowed in render phase");
341 }
342 }
343
344 /***
345 * Not allowed in a portlet.
346 * @throws IllegalStateException Not allowed in a portlet.
347 */
348 public String getLocalAddr() {
349 if(portletRequest instanceof HttpServletRequest) {
350 return ((HttpServletRequest)portletRequest).getLocalAddr();
351 }
352 throw new IllegalStateException("Not allowed in a portlet");
353 }
354
355 /***
356 * Not allowed in a portlet.
357 * @throws IllegalStateException Not allowed in a portlet.
358 */
359 public String getLocalName() {
360 if(portletRequest instanceof HttpServletRequest) {
361 return ((HttpServletRequest)portletRequest).getLocalName();
362 }
363 throw new IllegalStateException("Not allowed in a portlet");
364 }
365
366 /***
367 * Not allowed in a portlet.
368 * @throws IllegalStateException Not allowed in a portlet.
369 */
370 public int getLocalPort() {
371 if(portletRequest instanceof HttpServletRequest) {
372 return ((HttpServletRequest)portletRequest).getLocalPort();
373 }
374 throw new IllegalStateException("Not allowed in a portlet");
375 }
376
377
378
379
380 public Locale getLocale() {
381 return portletRequest.getLocale();
382 }
383
384
385
386
387 public Enumeration getLocales() {
388 return portletRequest.getLocales();
389 }
390
391
392
393
394 public String getParameter(String name) {
395 return portletRequest.getParameter(name);
396 }
397
398
399
400
401 public Map getParameterMap() {
402 return portletRequest.getParameterMap();
403 }
404
405
406
407
408 public Enumeration getParameterNames() {
409 return portletRequest.getParameterNames();
410 }
411
412
413
414
415 public String[] getParameterValues(String name) {
416 return portletRequest.getParameterValues(name);
417 }
418
419 /***
420 * Not allowed in a portlet.
421 * @throws IllegalStateException Not allowed in a portlet.
422 */
423 public String getProtocol() {
424 if(portletRequest instanceof HttpServletRequest) {
425 return ((HttpServletRequest)portletRequest).getProtocol();
426 }
427 throw new IllegalStateException("Not allowed in a portlet");
428 }
429
430 /***
431 * Can only be invoked in the event phase.
432 * @see ServletRequest#getReader()
433 * @throws IllegalStateException If the portlet is not in the event phase.
434 */
435 public BufferedReader getReader() throws IOException {
436 if(portletRequest instanceof ActionRequest) {
437 return ((ActionRequest)portletRequest).getReader();
438 }
439 else {
440 throw new IllegalStateException("Not allowed in render phase");
441 }
442 }
443
444
445
446
447 public String getRealPath(String path) {
448 return portletContext.getRealPath(path);
449 }
450
451 /***
452 * Not allowed in a portlet.
453 * @throws IllegalStateException Not allowed in a portlet.
454 */
455 public String getRemoteAddr() {
456 if(portletRequest instanceof HttpServletRequest) {
457 return ((HttpServletRequest)portletRequest).getRemoteAddr();
458 }
459 throw new IllegalStateException("Not allowed in a portlet");
460 }
461
462 /***
463 * Not allowed in a portlet.
464 * @throws IllegalStateException Not allowed in a portlet.
465 */
466 public String getRemoteHost() {
467 if(portletRequest instanceof HttpServletRequest) {
468 return ((HttpServletRequest)portletRequest).getRemoteHost();
469 }
470 throw new IllegalStateException("Not allowed in a portlet");
471 }
472
473 /***
474 * Not allowed in a portlet.
475 * @throws IllegalStateException Not allowed in a portlet.
476 */
477 public int getRemotePort() {
478 if(portletRequest instanceof HttpServletRequest) {
479 return ((HttpServletRequest)portletRequest).getRemotePort();
480 }
481 throw new IllegalStateException("Not allowed in a portlet");
482 }
483
484 /***
485 * Get the {@link PortletRequestDispatcher} as a {@link PortletServletRequestDispatcher} instance.
486 * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
487 */
488 public RequestDispatcher getRequestDispatcher(String path) {
489 return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
490 }
491
492
493
494
495 public String getScheme() {
496 return portletRequest.getScheme();
497 }
498
499
500
501
502 public String getServerName() {
503 return portletRequest.getServerName();
504 }
505
506 /***
507 * Not allowed in a portlet.
508 * @throws IllegalStateException Not allowed in a portlet.
509 */
510 public int getServerPort() {
511 if(portletRequest instanceof HttpServletRequest) {
512 return ((HttpServletRequest)portletRequest).getServerPort();
513 }
514 throw new IllegalStateException("Not allowed in a portlet");
515 }
516
517
518
519
520 public boolean isSecure() {
521 return portletRequest.isSecure();
522 }
523
524
525
526
527 public void removeAttribute(String name) {
528 portletRequest.removeAttribute(name);
529 }
530
531
532
533
534 public void setAttribute(String name, Object o) {
535 portletRequest.setAttribute(name, o);
536 }
537
538 /***
539 * Can only be invoked in the event phase.
540 * @see ServletRequest#setCharacterEncoding(String)
541 * @throws IllegalStateException If the portlet is not in the event phase.
542 */
543 public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
544 if(portletRequest instanceof ActionRequest) {
545 ((ActionRequest)portletRequest).setCharacterEncoding(env);
546 }
547 else {
548 throw new IllegalStateException("Not allowed in render phase");
549 }
550 }
551
552 /***
553 * Get the wrapped {@link PortletRequest} instance.
554 * @return The wrapped {@link PortletRequest} instance.
555 */
556 public PortletRequest getPortletRequest() {
557 return portletRequest;
558 }
559 }