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.StrutsConstants;
46 import org.apache.struts2.dispatcher.mapper.ActionMapping;
47 import org.apache.struts2.portlet.PortletActionConstants;
48 import org.apache.struts2.portlet.context.PortletActionContext;
49
50 import com.opensymphony.xwork2.inject.Inject;
51
52 /***
53 * Wrapper object exposing a {@link PortletRequest} as a
54 * {@link HttpServletRequest} instance. Clients accessing this request object
55 * will in fact operate on the {@link PortletRequest} object wrapped by this
56 * request object.
57 */
58 public class PortletServletRequest implements HttpServletRequest, PortletActionConstants {
59
60 private PortletRequest portletRequest;
61
62 private PortletContext portletContext;
63
64 private String extension;
65
66 public PortletServletRequest(PortletRequest portletRequest, PortletContext portletContext) {
67 this.portletRequest = portletRequest;
68 this.portletContext = portletContext;
69 }
70
71
72
73
74
75
76 public String getAuthType() {
77 return portletRequest.getAuthType();
78 }
79
80
81
82
83
84
85 public String getContextPath() {
86 return portletRequest.getContextPath();
87 }
88
89 /***
90 * Not allowed in a portlet.
91 *
92 * @throws IllegalStateException
93 * Not allowed in a portlet.
94 */
95 public Cookie[] getCookies() {
96 if (portletRequest instanceof HttpServletRequest) {
97 return ((HttpServletRequest) portletRequest).getCookies();
98 }
99 throw new IllegalStateException("Not allowed in a portlet");
100 }
101
102 /***
103 * Not allowed in a portlet.
104 *
105 * @throws IllegalStateException
106 * Not allowed in a portlet.
107 */
108 public long getDateHeader(String name) {
109 throw new IllegalStateException("Not allowed in a portlet");
110 }
111
112 /***
113 * Gets a property from the {@link PortletRequest}. Note that a
114 * {@link PortletRequest} is not guaranteed to map properties to headers.
115 *
116 * @see PortletRequest#getProperty(String)
117 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
118 */
119 public String getHeader(String name) {
120 return portletRequest.getProperty(name);
121 }
122
123 /***
124 * Gets the property names from the {@link PortletRequest}. Note that a
125 * {@link PortletRequest} is not guaranteed to map properties to headers.
126 *
127 * @see PortletRequest#getPropertyNames()
128 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
129 */
130 public Enumeration getHeaderNames() {
131 return portletRequest.getPropertyNames();
132 }
133
134 /***
135 * Gets the values for the specified property from the
136 * {@link PortletRequest}. Note that a {@link PortletRequest} is not
137 * guaranteed to map properties to headers.
138 *
139 * @see PortletRequest#getProperties(String)
140 * @see HttpServletRequest#getHeaders(String)
141 */
142 public Enumeration getHeaders(String name) {
143 return portletRequest.getProperties(name);
144 }
145
146 /***
147 * Not allowed in a portlet.
148 *
149 * @throws IllegalStateException
150 * Not allowed in a portlet.
151 */
152 public int getIntHeader(String name) {
153 throw new IllegalStateException("Not allowed in a portlet");
154 }
155
156
157
158
159
160
161 public String getMethod() {
162 return null;
163 }
164
165
166
167
168
169
170 public String getPathInfo() {
171 return null;
172 }
173
174
175
176
177
178
179 public String getPathTranslated() {
180 return null;
181 }
182
183
184
185
186
187
188 public String getQueryString() {
189 return null;
190 }
191
192
193
194
195
196
197 public String getRemoteUser() {
198 return portletRequest.getRemoteUser();
199 }
200
201 /***
202 * Not allowed in a portlet.
203 *
204 * @throws IllegalStateException
205 * Not allowed in a portlet.
206 */
207 public String getRequestURI() {
208 throw new IllegalStateException("Not allowed in a portlet");
209 }
210
211 /***
212 * Not allowed in a portlet.
213 *
214 * @throws IllegalStateException
215 * Not allowed in a portlet.
216 */
217 public StringBuffer getRequestURL() {
218 throw new IllegalStateException("Not allowed in a portlet");
219 }
220
221
222
223
224
225
226 public String getRequestedSessionId() {
227 return portletRequest.getRequestedSessionId();
228 }
229
230 /***
231 * A {@link PortletRequest} has no servlet path. But for compatibility with
232 * Struts 2 components and interceptors, the action parameter on the request
233 * is mapped to the servlet path.
234 *
235 * @see javax.servlet.http.HttpServletRequest#getServletPath()
236 */
237 public String getServletPath() {
238 String actionPath = getParameter(ACTION_PARAM);
239 if (!hasExtension(actionPath)) {
240 actionPath += "." + extension;
241 }
242 return actionPath;
243 }
244
245 private boolean hasExtension(String actionPath) {
246 return extension == null || "".equals(extension)
247 || (actionPath != null && actionPath.endsWith("." + extension));
248 }
249
250 /***
251 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
252 *
253 * @see javax.servlet.http.HttpServletRequest#getSession()
254 */
255 public HttpSession getSession() {
256 return new PortletHttpSession(portletRequest.getPortletSession());
257 }
258
259 /***
260 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
261 *
262 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
263 */
264 public HttpSession getSession(boolean create) {
265 return new PortletHttpSession(portletRequest.getPortletSession(create));
266 }
267
268
269
270
271
272
273 public Principal getUserPrincipal() {
274 return portletRequest.getUserPrincipal();
275 }
276
277 /***
278 * Not allowed in a portlet.
279 *
280 * @throws IllegalStateException
281 * Not allowed in a portlet.
282 */
283 public boolean isRequestedSessionIdFromCookie() {
284 throw new IllegalStateException("Not allowed in a portlet");
285 }
286
287 /***
288 * Not allowed in a portlet.
289 *
290 * @throws IllegalStateException
291 * Not allowed in a portlet.
292 */
293 public boolean isRequestedSessionIdFromURL() {
294 throw new IllegalStateException("Not allowed in a portlet");
295 }
296
297 /***
298 * Not allowed in a portlet.
299 *
300 * @throws IllegalStateException
301 * Not allowed in a portlet.
302 */
303 public boolean isRequestedSessionIdFromUrl() {
304 throw new IllegalStateException("Not allowed in a portlet");
305 }
306
307
308
309
310
311
312 public boolean isRequestedSessionIdValid() {
313 return portletRequest.isRequestedSessionIdValid();
314 }
315
316
317
318
319
320
321 public boolean isUserInRole(String role) {
322 return portletRequest.isUserInRole(role);
323 }
324
325 /***
326 * Gets an attribute value on the {@link PortletRequest}. If the attribute
327 * name is <tt>javax.servlet.include.servlet_path</tt>, it returns the
328 * same as {@link PortletServletRequest#getServletPath()}
329 *
330 * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
331 */
332 public Object getAttribute(String name) {
333 if ("javax.servlet.include.servlet_path".equals(name)) {
334 return getServletPath();
335 } else {
336 return portletRequest.getAttribute(name);
337 }
338 }
339
340
341
342
343
344
345 public Enumeration getAttributeNames() {
346 return portletRequest.getAttributeNames();
347 }
348
349 /***
350 * Can only be invoked in the event phase.
351 *
352 * @see ServletRequest#getCharacterEncoding()
353 * @throws IllegalStateException
354 * If the portlet is not in the event phase.
355 */
356 public String getCharacterEncoding() {
357 if (portletRequest instanceof ActionRequest) {
358 return ((ActionRequest) portletRequest).getCharacterEncoding();
359 } else {
360 throw new IllegalStateException("Not allowed in render phase");
361 }
362 }
363
364 /***
365 * Can only be invoked in the event phase.
366 *
367 * @see ServletRequest#getContentLength()
368 * @throws IllegalStateException
369 * If the portlet is not in the event phase.
370 */
371 public int getContentLength() {
372 if (portletRequest instanceof ActionRequest) {
373 return ((ActionRequest) portletRequest).getContentLength();
374 } else {
375 throw new IllegalStateException("Not allowed in render phase");
376 }
377 }
378
379 /***
380 * Can only be invoked in the event phase.
381 *
382 * @see ServletRequest#getContentType()
383 * @throws IllegalStateException
384 * If the portlet is not in the event phase.
385 */
386 public String getContentType() {
387 if (portletRequest instanceof ActionRequest) {
388 return ((ActionRequest) portletRequest).getContentType();
389 } else {
390 throw new IllegalStateException("Not allowed in render phase");
391 }
392 }
393
394 /***
395 * Can only be invoked in the event phase. When invoked in the event phase,
396 * it will wrap the portlet's {@link InputStream} as a
397 * {@link PortletServletInputStream}.
398 *
399 * @see ServletRequest#getInputStream()
400 * @throws IllegalStateException
401 * If the portlet is not in the event phase.
402 */
403 public ServletInputStream getInputStream() throws IOException {
404 if (portletRequest instanceof ActionRequest) {
405 return new PortletServletInputStream(((ActionRequest) portletRequest).getPortletInputStream());
406 } else {
407 throw new IllegalStateException("Not allowed in render phase");
408 }
409 }
410
411 /***
412 * Not allowed in a portlet.
413 *
414 * @throws IllegalStateException
415 * Not allowed in a portlet.
416 */
417 public String getLocalAddr() {
418 if (portletRequest instanceof HttpServletRequest) {
419 return ((HttpServletRequest) portletRequest).getLocalAddr();
420 }
421 throw new IllegalStateException("Not allowed in a portlet");
422 }
423
424 /***
425 * Not allowed in a portlet.
426 *
427 * @throws IllegalStateException
428 * Not allowed in a portlet.
429 */
430 public String getLocalName() {
431 if (portletRequest instanceof HttpServletRequest) {
432 return ((HttpServletRequest) portletRequest).getLocalName();
433 }
434 throw new IllegalStateException("Not allowed in a portlet");
435 }
436
437 /***
438 * Not allowed in a portlet.
439 *
440 * @throws IllegalStateException
441 * Not allowed in a portlet.
442 */
443 public int getLocalPort() {
444 if (portletRequest instanceof HttpServletRequest) {
445 return ((HttpServletRequest) portletRequest).getLocalPort();
446 }
447 throw new IllegalStateException("Not allowed in a portlet");
448 }
449
450
451
452
453
454
455 public Locale getLocale() {
456 return portletRequest.getLocale();
457 }
458
459
460
461
462
463
464 public Enumeration getLocales() {
465 return portletRequest.getLocales();
466 }
467
468
469
470
471
472
473 public String getParameter(String name) {
474 return portletRequest.getParameter(name);
475 }
476
477
478
479
480
481
482 public Map getParameterMap() {
483 return portletRequest.getParameterMap();
484 }
485
486
487
488
489
490
491 public Enumeration getParameterNames() {
492 return portletRequest.getParameterNames();
493 }
494
495
496
497
498
499
500 public String[] getParameterValues(String name) {
501 return portletRequest.getParameterValues(name);
502 }
503
504 /***
505 * Not allowed in a portlet.
506 *
507 * @throws IllegalStateException
508 * Not allowed in a portlet.
509 */
510 public String getProtocol() {
511 if (portletRequest instanceof HttpServletRequest) {
512 return ((HttpServletRequest) portletRequest).getProtocol();
513 }
514 throw new IllegalStateException("Not allowed in a portlet");
515 }
516
517 /***
518 * Can only be invoked in the event phase.
519 *
520 * @see ServletRequest#getReader()
521 * @throws IllegalStateException
522 * If the portlet is not in the event phase.
523 */
524 public BufferedReader getReader() throws IOException {
525 if (portletRequest instanceof ActionRequest) {
526 return ((ActionRequest) portletRequest).getReader();
527 } else {
528 throw new IllegalStateException("Not allowed in render phase");
529 }
530 }
531
532
533
534
535
536
537 public String getRealPath(String path) {
538 return portletContext.getRealPath(path);
539 }
540
541 /***
542 * Not allowed in a portlet.
543 *
544 * @throws IllegalStateException
545 * Not allowed in a portlet.
546 */
547 public String getRemoteAddr() {
548 if (portletRequest instanceof HttpServletRequest) {
549 return ((HttpServletRequest) portletRequest).getRemoteAddr();
550 }
551 throw new IllegalStateException("Not allowed in a portlet");
552 }
553
554 /***
555 * Not allowed in a portlet.
556 *
557 * @throws IllegalStateException
558 * Not allowed in a portlet.
559 */
560 public String getRemoteHost() {
561 if (portletRequest instanceof HttpServletRequest) {
562 return ((HttpServletRequest) portletRequest).getRemoteHost();
563 }
564 throw new IllegalStateException("Not allowed in a portlet");
565 }
566
567 /***
568 * Not allowed in a portlet.
569 *
570 * @throws IllegalStateException
571 * Not allowed in a portlet.
572 */
573 public int getRemotePort() {
574 if (portletRequest instanceof HttpServletRequest) {
575 return ((HttpServletRequest) portletRequest).getRemotePort();
576 }
577 throw new IllegalStateException("Not allowed in a portlet");
578 }
579
580 /***
581 * Get the {@link PortletRequestDispatcher} as a
582 * {@link PortletServletRequestDispatcher} instance.
583 *
584 * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
585 */
586 public RequestDispatcher getRequestDispatcher(String path) {
587 return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
588 }
589
590
591
592
593
594
595 public String getScheme() {
596 return portletRequest.getScheme();
597 }
598
599
600
601
602
603
604 public String getServerName() {
605 return portletRequest.getServerName();
606 }
607
608 /***
609 * Not allowed in a portlet.
610 *
611 * @throws IllegalStateException
612 * Not allowed in a portlet.
613 */
614 public int getServerPort() {
615 if (portletRequest instanceof HttpServletRequest) {
616 return ((HttpServletRequest) portletRequest).getServerPort();
617 }
618 throw new IllegalStateException("Not allowed in a portlet");
619 }
620
621
622
623
624
625
626 public boolean isSecure() {
627 return portletRequest.isSecure();
628 }
629
630
631
632
633
634
635 public void removeAttribute(String name) {
636 portletRequest.removeAttribute(name);
637 }
638
639
640
641
642
643
644
645 public void setAttribute(String name, Object o) {
646 portletRequest.setAttribute(name, o);
647 }
648
649 /***
650 * Can only be invoked in the event phase.
651 *
652 * @see ServletRequest#setCharacterEncoding(String)
653 * @throws IllegalStateException
654 * If the portlet is not in the event phase.
655 */
656 public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
657 if (portletRequest instanceof ActionRequest) {
658 ((ActionRequest) portletRequest).setCharacterEncoding(env);
659 } else {
660 throw new IllegalStateException("Not allowed in render phase");
661 }
662 }
663
664 /***
665 * Get the wrapped {@link PortletRequest} instance.
666 *
667 * @return The wrapped {@link PortletRequest} instance.
668 */
669 public PortletRequest getPortletRequest() {
670 return portletRequest;
671 }
672
673 @Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
674 public void setExtension(String extension) {
675 if (extension != null) {
676 this.extension = extension.split(",")[0];
677 }
678 }
679 }