View Javadoc

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