001: /*
002: * $Id: $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.servlet;
022:
023: import java.io.BufferedReader;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.UnsupportedEncodingException;
027: import java.security.Principal;
028: import java.util.Enumeration;
029: import java.util.Locale;
030: import java.util.Map;
031:
032: import javax.portlet.ActionRequest;
033: import javax.portlet.PortletContext;
034: import javax.portlet.PortletRequest;
035: import javax.portlet.PortletRequestDispatcher;
036: import javax.portlet.PortletSession;
037: import javax.servlet.RequestDispatcher;
038: import javax.servlet.ServletInputStream;
039: import javax.servlet.ServletRequest;
040: import javax.servlet.http.Cookie;
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpSession;
043:
044: import org.apache.struts2.portlet.PortletActionConstants;
045:
046: /**
047: * Wrapper object exposing a {@link PortletRequest} as a {@link HttpServletRequest} instance.
048: * Clients accessing this request object will in fact operate on the
049: * {@link PortletRequest} object wrapped by this request object.
050: */
051: public class PortletServletRequest implements HttpServletRequest,
052: PortletActionConstants {
053:
054: private PortletRequest portletRequest;
055: private PortletContext portletContext;
056:
057: public PortletServletRequest(PortletRequest portletRequest,
058: PortletContext portletContext) {
059: this .portletRequest = portletRequest;
060: this .portletContext = portletContext;
061: }
062:
063: /* (non-Javadoc)
064: * @see javax.servlet.http.HttpServletRequest#getAuthType()
065: */
066: public String getAuthType() {
067: return portletRequest.getAuthType();
068: }
069:
070: /* (non-Javadoc)
071: * @see javax.servlet.http.HttpServletRequest#getContextPath()
072: */
073: public String getContextPath() {
074: return portletRequest.getContextPath();
075: }
076:
077: /**
078: * Not allowed in a portlet.
079: * @throws IllegalStateException Not allowed in a portlet.
080: */
081: public Cookie[] getCookies() {
082: if (portletRequest instanceof HttpServletRequest) {
083: return ((HttpServletRequest) portletRequest).getCookies();
084: }
085: throw new IllegalStateException("Not allowed in a portlet");
086: }
087:
088: /**
089: * Not allowed in a portlet.
090: * @throws IllegalStateException Not allowed in a portlet.
091: */
092: public long getDateHeader(String name) {
093: throw new IllegalStateException("Not allowed in a portlet");
094: }
095:
096: /**
097: * Gets a property from the {@link PortletRequest}. Note that a {@link PortletRequest} is not
098: * guaranteed to map properties to headers.
099: * @see PortletRequest#getProperty(String)
100: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
101: */
102: public String getHeader(String name) {
103: return portletRequest.getProperty(name);
104: }
105:
106: /**
107: * Gets the property names from the {@link PortletRequest}. Note that a {@link PortletRequest} is not
108: * guaranteed to map properties to headers.
109: * @see PortletRequest#getPropertyNames()
110: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
111: */
112: public Enumeration getHeaderNames() {
113: return portletRequest.getPropertyNames();
114: }
115:
116: /**
117: * Gets the values for the specified property from the {@link PortletRequest}. Note that a
118: * {@link PortletRequest} is not guaranteed to map properties to headers.
119: * @see PortletRequest#getProperties(String)
120: * @see HttpServletRequest#getHeaders(String)
121: */
122: public Enumeration getHeaders(String name) {
123: return portletRequest.getProperties(name);
124: }
125:
126: /**
127: * Not allowed in a portlet.
128: * @throws IllegalStateException Not allowed in a portlet.
129: */
130: public int getIntHeader(String name) {
131: throw new IllegalStateException("Not allowed in a portlet");
132: }
133:
134: /* (non-Javadoc)
135: * @see javax.servlet.http.HttpServletRequest#getMethod()
136: */
137: public String getMethod() {
138: return null;
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
143: */
144: public String getPathInfo() {
145: return null;
146: }
147:
148: /* (non-Javadoc)
149: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
150: */
151: public String getPathTranslated() {
152: return null;
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.http.HttpServletRequest#getQueryString()
157: */
158: public String getQueryString() {
159: return null;
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
164: */
165: public String getRemoteUser() {
166: return portletRequest.getRemoteUser();
167: }
168:
169: /**
170: * Not allowed in a portlet.
171: * @throws IllegalStateException Not allowed in a portlet.
172: */
173: public String getRequestURI() {
174: throw new IllegalStateException("Not allowed in a portlet");
175: }
176:
177: /**
178: * Not allowed in a portlet.
179: * @throws IllegalStateException Not allowed in a portlet.
180: */
181: public StringBuffer getRequestURL() {
182: throw new IllegalStateException("Not allowed in a portlet");
183: }
184:
185: /* (non-Javadoc)
186: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
187: */
188: public String getRequestedSessionId() {
189: return portletRequest.getRequestedSessionId();
190: }
191:
192: /**
193: * A {@link PortletRequest} has no servlet path. But for compatibility with Struts 2 components and
194: * interceptors, the action parameter on the request is mapped to the servlet path.
195: * @see javax.servlet.http.HttpServletRequest#getServletPath()
196: */
197: public String getServletPath() {
198: String actionPath = getParameter(ACTION_PARAM);
199: if (actionPath != null && !actionPath.endsWith(".action")) {
200: actionPath += ".action";
201: }
202: return actionPath;
203: }
204:
205: /**
206: * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
207: * @see javax.servlet.http.HttpServletRequest#getSession()
208: */
209: public HttpSession getSession() {
210: return new PortletHttpSession(portletRequest
211: .getPortletSession());
212: }
213:
214: /**
215: * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
216: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
217: */
218: public HttpSession getSession(boolean create) {
219: return new PortletHttpSession(portletRequest
220: .getPortletSession(create));
221: }
222:
223: /* (non-Javadoc)
224: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
225: */
226: public Principal getUserPrincipal() {
227: return portletRequest.getUserPrincipal();
228: }
229:
230: /**
231: * Not allowed in a portlet.
232: * @throws IllegalStateException Not allowed in a portlet.
233: */
234: public boolean isRequestedSessionIdFromCookie() {
235: throw new IllegalStateException("Not allowed in a portlet");
236: }
237:
238: /**
239: * Not allowed in a portlet.
240: * @throws IllegalStateException Not allowed in a portlet.
241: */
242: public boolean isRequestedSessionIdFromURL() {
243: throw new IllegalStateException("Not allowed in a portlet");
244: }
245:
246: /**
247: * Not allowed in a portlet.
248: * @throws IllegalStateException Not allowed in a portlet.
249: */
250: public boolean isRequestedSessionIdFromUrl() {
251: throw new IllegalStateException("Not allowed in a portlet");
252: }
253:
254: /* (non-Javadoc)
255: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
256: */
257: public boolean isRequestedSessionIdValid() {
258: return portletRequest.isRequestedSessionIdValid();
259: }
260:
261: /* (non-Javadoc)
262: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
263: */
264: public boolean isUserInRole(String role) {
265: return portletRequest.isUserInRole(role);
266: }
267:
268: /**
269: * Gets an attribute value on the {@link PortletRequest}. If the attribute name is
270: * <tt>javax.servlet.include.servlet_path</tt>, it returns the same as
271: * {@link PortletServletRequest#getServletPath()}
272: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
273: */
274: public Object getAttribute(String name) {
275: if ("javax.servlet.include.servlet_path".equals(name)) {
276: return getServletPath();
277: } else {
278: return portletRequest.getAttribute(name);
279: }
280: }
281:
282: /* (non-Javadoc)
283: * @see javax.servlet.ServletRequest#getAttributeNames()
284: */
285: public Enumeration getAttributeNames() {
286: return portletRequest.getAttributeNames();
287: }
288:
289: /**
290: * Can only be invoked in the event phase.
291: * @see ServletRequest#getCharacterEncoding()
292: * @throws IllegalStateException If the portlet is not in the event phase.
293: */
294: public String getCharacterEncoding() {
295: if (portletRequest instanceof ActionRequest) {
296: return ((ActionRequest) portletRequest)
297: .getCharacterEncoding();
298: } else {
299: throw new IllegalStateException(
300: "Not allowed in render phase");
301: }
302: }
303:
304: /**
305: * Can only be invoked in the event phase.
306: * @see ServletRequest#getContentLength()
307: * @throws IllegalStateException If the portlet is not in the event phase.
308: */
309: public int getContentLength() {
310: if (portletRequest instanceof ActionRequest) {
311: return ((ActionRequest) portletRequest).getContentLength();
312: } else {
313: throw new IllegalStateException(
314: "Not allowed in render phase");
315: }
316: }
317:
318: /**
319: * Can only be invoked in the event phase.
320: * @see ServletRequest#getContentType()
321: * @throws IllegalStateException If the portlet is not in the event phase.
322: */
323: public String getContentType() {
324: if (portletRequest instanceof ActionRequest) {
325: return ((ActionRequest) portletRequest).getContentType();
326: } else {
327: throw new IllegalStateException(
328: "Not allowed in render phase");
329: }
330: }
331:
332: /**
333: * Can only be invoked in the event phase. When invoked in the event phase, it will wrap the
334: * portlet's {@link InputStream} as a {@link PortletServletInputStream}.
335: * @see ServletRequest#getInputStream()
336: * @throws IllegalStateException If the portlet is not in the event phase.
337: */
338: public ServletInputStream getInputStream() throws IOException {
339: if (portletRequest instanceof ActionRequest) {
340: return new PortletServletInputStream(
341: ((ActionRequest) portletRequest)
342: .getPortletInputStream());
343: } else {
344: throw new IllegalStateException(
345: "Not allowed in render phase");
346: }
347: }
348:
349: /**
350: * Not allowed in a portlet.
351: * @throws IllegalStateException Not allowed in a portlet.
352: */
353: public String getLocalAddr() {
354: if (portletRequest instanceof HttpServletRequest) {
355: return ((HttpServletRequest) portletRequest).getLocalAddr();
356: }
357: throw new IllegalStateException("Not allowed in a portlet");
358: }
359:
360: /**
361: * Not allowed in a portlet.
362: * @throws IllegalStateException Not allowed in a portlet.
363: */
364: public String getLocalName() {
365: if (portletRequest instanceof HttpServletRequest) {
366: return ((HttpServletRequest) portletRequest).getLocalName();
367: }
368: throw new IllegalStateException("Not allowed in a portlet");
369: }
370:
371: /**
372: * Not allowed in a portlet.
373: * @throws IllegalStateException Not allowed in a portlet.
374: */
375: public int getLocalPort() {
376: if (portletRequest instanceof HttpServletRequest) {
377: return ((HttpServletRequest) portletRequest).getLocalPort();
378: }
379: throw new IllegalStateException("Not allowed in a portlet");
380: }
381:
382: /* (non-Javadoc)
383: * @see javax.servlet.ServletRequest#getLocale()
384: */
385: public Locale getLocale() {
386: return portletRequest.getLocale();
387: }
388:
389: /* (non-Javadoc)
390: * @see javax.servlet.ServletRequest#getLocales()
391: */
392: public Enumeration getLocales() {
393: return portletRequest.getLocales();
394: }
395:
396: /* (non-Javadoc)
397: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
398: */
399: public String getParameter(String name) {
400: return portletRequest.getParameter(name);
401: }
402:
403: /* (non-Javadoc)
404: * @see javax.servlet.ServletRequest#getParameterMap()
405: */
406: public Map getParameterMap() {
407: return portletRequest.getParameterMap();
408: }
409:
410: /* (non-Javadoc)
411: * @see javax.servlet.ServletRequest#getParameterNames()
412: */
413: public Enumeration getParameterNames() {
414: return portletRequest.getParameterNames();
415: }
416:
417: /* (non-Javadoc)
418: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
419: */
420: public String[] getParameterValues(String name) {
421: return portletRequest.getParameterValues(name);
422: }
423:
424: /**
425: * Not allowed in a portlet.
426: * @throws IllegalStateException Not allowed in a portlet.
427: */
428: public String getProtocol() {
429: if (portletRequest instanceof HttpServletRequest) {
430: return ((HttpServletRequest) portletRequest).getProtocol();
431: }
432: throw new IllegalStateException("Not allowed in a portlet");
433: }
434:
435: /**
436: * Can only be invoked in the event phase.
437: * @see ServletRequest#getReader()
438: * @throws IllegalStateException If the portlet is not in the event phase.
439: */
440: public BufferedReader getReader() throws IOException {
441: if (portletRequest instanceof ActionRequest) {
442: return ((ActionRequest) portletRequest).getReader();
443: } else {
444: throw new IllegalStateException(
445: "Not allowed in render phase");
446: }
447: }
448:
449: /* (non-Javadoc)
450: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
451: */
452: public String getRealPath(String path) {
453: return portletContext.getRealPath(path);
454: }
455:
456: /**
457: * Not allowed in a portlet.
458: * @throws IllegalStateException Not allowed in a portlet.
459: */
460: public String getRemoteAddr() {
461: if (portletRequest instanceof HttpServletRequest) {
462: return ((HttpServletRequest) portletRequest)
463: .getRemoteAddr();
464: }
465: throw new IllegalStateException("Not allowed in a portlet");
466: }
467:
468: /**
469: * Not allowed in a portlet.
470: * @throws IllegalStateException Not allowed in a portlet.
471: */
472: public String getRemoteHost() {
473: if (portletRequest instanceof HttpServletRequest) {
474: return ((HttpServletRequest) portletRequest)
475: .getRemoteHost();
476: }
477: throw new IllegalStateException("Not allowed in a portlet");
478: }
479:
480: /**
481: * Not allowed in a portlet.
482: * @throws IllegalStateException Not allowed in a portlet.
483: */
484: public int getRemotePort() {
485: if (portletRequest instanceof HttpServletRequest) {
486: return ((HttpServletRequest) portletRequest)
487: .getRemotePort();
488: }
489: throw new IllegalStateException("Not allowed in a portlet");
490: }
491:
492: /**
493: * Get the {@link PortletRequestDispatcher} as a {@link PortletServletRequestDispatcher} instance.
494: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
495: */
496: public RequestDispatcher getRequestDispatcher(String path) {
497: return new PortletServletRequestDispatcher(portletContext
498: .getRequestDispatcher(path));
499: }
500:
501: /* (non-Javadoc)
502: * @see javax.servlet.ServletRequest#getScheme()
503: */
504: public String getScheme() {
505: return portletRequest.getScheme();
506: }
507:
508: /* (non-Javadoc)
509: * @see javax.servlet.ServletRequest#getServerName()
510: */
511: public String getServerName() {
512: return portletRequest.getServerName();
513: }
514:
515: /**
516: * Not allowed in a portlet.
517: * @throws IllegalStateException Not allowed in a portlet.
518: */
519: public int getServerPort() {
520: if (portletRequest instanceof HttpServletRequest) {
521: return ((HttpServletRequest) portletRequest)
522: .getServerPort();
523: }
524: throw new IllegalStateException("Not allowed in a portlet");
525: }
526:
527: /* (non-Javadoc)
528: * @see javax.servlet.ServletRequest#isSecure()
529: */
530: public boolean isSecure() {
531: return portletRequest.isSecure();
532: }
533:
534: /* (non-Javadoc)
535: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
536: */
537: public void removeAttribute(String name) {
538: portletRequest.removeAttribute(name);
539: }
540:
541: /* (non-Javadoc)
542: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
543: */
544: public void setAttribute(String name, Object o) {
545: portletRequest.setAttribute(name, o);
546: }
547:
548: /**
549: * Can only be invoked in the event phase.
550: * @see ServletRequest#setCharacterEncoding(String)
551: * @throws IllegalStateException If the portlet is not in the event phase.
552: */
553: public void setCharacterEncoding(String env)
554: throws UnsupportedEncodingException {
555: if (portletRequest instanceof ActionRequest) {
556: ((ActionRequest) portletRequest).setCharacterEncoding(env);
557: } else {
558: throw new IllegalStateException(
559: "Not allowed in render phase");
560: }
561: }
562:
563: /**
564: * Get the wrapped {@link PortletRequest} instance.
565: * @return The wrapped {@link PortletRequest} instance.
566: */
567: public PortletRequest getPortletRequest() {
568: return portletRequest;
569: }
570: }
|