View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  /*
17   *  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.io.BufferedReader;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.Vector;
30  
31  import javax.portlet.PortalContext;
32  import javax.portlet.PortletMode;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletSession;
36  import javax.portlet.WindowState;
37  import javax.servlet.http.Cookie;
38  import javax.servlet.http.HttpSession;
39  
40  import org.apache.pluto.core.InternalPortletRequest;
41  import org.apache.pluto.factory.PortletObjectAccess;
42  import org.apache.pluto.om.common.SecurityRoleRef;
43  import org.apache.pluto.om.common.SecurityRoleRefSet;
44  import org.apache.pluto.om.entity.PortletEntity;
45  import org.apache.pluto.om.portlet.PortletDefinition;
46  import org.apache.pluto.om.window.PortletWindow;
47  import org.apache.pluto.services.information.DynamicInformationProvider;
48  import org.apache.pluto.services.information.InformationProviderAccess;
49  import org.apache.pluto.services.property.PropertyManager;
50  import org.apache.pluto.util.Enumerator;
51  import org.apache.pluto.util.NamespaceMapperAccess;
52  import org.apache.pluto.util.StringUtils;
53  
54  public abstract class PortletRequestImpl extends
55  		javax.servlet.http.HttpServletRequestWrapper implements PortletRequest,
56  		InternalPortletRequest {
57  
58  	private PortletWindow portletWindow;
59  
60  	/***
61  	 * Holds the portlet session
62  	 */
63  	private PortletSession portletSession;
64  
65  	private DynamicInformationProvider provider;
66  
67  	/***
68  	 * true if the HTTP-Body has been accessed
69  	 */
70  	private boolean bodyAccessed;
71  
72  	/***
73  	 * true if we are in an include call
74  	 */
75  	private boolean included;
76  
77  	public PortletRequestImpl(PortletWindow portletWindow,
78  			javax.servlet.http.HttpServletRequest servletRequest) {
79  		super(servletRequest);
80  		this.portletWindow = portletWindow;
81  
82  		provider = InformationProviderAccess
83  				.getDynamicProvider(_getHttpServletRequest());
84  	}
85  
86  	// javax.portlet.PortletRequest implementation
87  	// ------------------------------------------------
88  	public boolean isWindowStateAllowed(WindowState state) {
89  		return provider.isWindowStateAllowed(state);
90  	}
91  
92  	public boolean isPortletModeAllowed(PortletMode portletMode) {
93  		// check if portal supports portlet mode
94  		boolean supported = provider.isPortletModeAllowed(portletMode);
95  
96  		// check if portlet supports portlet mode as well
97  		if (supported) {
98  			supported = PortletModeHelper.isPortletModeAllowedByPortlet(
99  					portletWindow, portletMode);
100 		}
101 
102 		return supported;
103 	}
104 
105 	public PortletMode getPortletMode() {
106 		return provider.getPortletMode(portletWindow);
107 	}
108 
109 	public WindowState getWindowState() {
110 		return provider.getWindowState(portletWindow);
111 	}
112 
113 	// needs to be implemented in each subclass
114 	public abstract PortletPreferences getPreferences();
115 
116 	public PortletSession getPortletSession() {
117 		return getPortletSession(true);
118 	}
119 
120 	public PortletSession getPortletSession(boolean create) {
121 		// check if the session was invalidated
122 		javax.servlet.http.HttpSession httpSession = this
123 				._getHttpServletRequest().getSession(false);
124 
125 		if ((portletSession != null) && (httpSession == null)) {
126 			portletSession = null;
127 		} else if (httpSession != null) {
128 			create = true;
129 		}
130 
131 		if (create && portletSession == null) {
132 			httpSession = this._getHttpServletRequest().getSession(create);
133 			if (httpSession != null) {
134 				portletSession = PortletObjectAccess.getPortletSession(
135 						portletWindow, httpSession);
136 			}
137 		}
138 
139 		return portletSession;
140 	}
141 
142 	public String getProperty(String name) {
143 		if (name == null) {
144 			throw new IllegalArgumentException("Attribute name == null");
145 		}
146 
147 		// get properties from request header
148 		String prop = this._getHttpServletRequest().getHeader(name);
149 		if (prop == null) {
150 			// get properties from PropertyManager
151 			Map map = PropertyManager.getRequestProperties(portletWindow, this
152 					._getHttpServletRequest());
153 			if (map != null) {
154 				String[] properties = (String[]) map.get(name);
155 				if ((properties != null) && (properties.length > 0)) {
156 					prop = properties[0];
157 				}
158 			}
159 		}
160 
161 		return prop;
162 	}
163 
164 	public Enumeration getProperties(String name) {
165 		if (name == null) {
166 			throw new IllegalArgumentException("Property name == null");
167 		}
168 
169 		Set v = new HashSet();
170 
171 		// get properties from request header
172 		Enumeration props = this._getHttpServletRequest().getHeaders(name);
173 		if (props != null) {
174 			while (props.hasMoreElements()) {
175 				v.add(props.nextElement());
176 			}
177 		}
178 
179 		// get properties from PropertyManager
180 		Map map = PropertyManager.getRequestProperties(portletWindow, this
181 				._getHttpServletRequest());
182 		if (map != null) {
183 			String[] properties = (String[]) map.get(name);
184 
185 			if (properties != null) {
186 				// add properties to vector
187 				for (int i = 0; i < properties.length; i++) {
188 					v.add(properties[i]);
189 				}
190 			}
191 		}
192 
193 		return new Enumerator(v.iterator());
194 	}
195 
196 	public Enumeration getPropertyNames() {
197 		Set v = new HashSet();
198 
199 		// get properties from PropertyManager
200 		Map map = PropertyManager.getRequestProperties(portletWindow, this
201 				._getHttpServletRequest());
202 		if (map != null) {
203 			v.addAll(map.keySet());
204 		}
205 
206 		// get properties from request header
207 		Enumeration props = this._getHttpServletRequest().getHeaderNames();
208 		if (props != null) {
209 			while (props.hasMoreElements()) {
210 				v.add(props.nextElement());
211 			}
212 		}
213 
214 		return new Enumerator(v.iterator());
215 	}
216 
217 	public PortalContext getPortalContext() {
218 		return PortletObjectAccess.getPortalContext();
219 	}
220 
221 	public String getAuthType() {
222 		return this._getHttpServletRequest().getAuthType();
223 	}
224 
225 	public String getContextPath() {
226 		return portletWindow.getPortletEntity().getPortletDefinition()
227 				.getPortletApplicationDefinition()
228 				.getWebApplicationDefinition().getContextRoot();
229 
230 		// we cannot use that because of a bug in tomcat
231 		// return this._getHttpServletRequest().getContextPath();
232 	}
233 
234 	public String getRemoteUser() {
235 		return this._getHttpServletRequest().getRemoteUser();
236 	}
237 
238 	public java.security.Principal getUserPrincipal() {
239 		return this._getHttpServletRequest().getUserPrincipal();
240 	}
241 
242 	/***
243 	 * Determines whether a user is mapped to the specified role. As specified
244 	 * in PLT-20-3, we must reference the &lt;security-role-ref&gt; mappings
245 	 * within the deployment descriptor. If no mapping is available, then, and
246 	 * only then, do we check use the actual role name specified against the web
247 	 * application deployment descriptor.
248 	 * 
249 	 * @param roleName
250 	 *            the name of the role
251 	 * @return true if it is determined the user has the given role.
252 	 *  
253 	 */
254 	public boolean isUserInRole(String roleName) {
255 		PortletEntity entity = portletWindow.getPortletEntity();
256 		PortletDefinition def = entity.getPortletDefinition();
257 		SecurityRoleRefSet set = def.getInitSecurityRoleRefSet();
258 		SecurityRoleRef ref = set.get(roleName);
259 
260 		String link = null;
261 		if (ref != null && ref.getRoleLink() != null) {
262 			link = ref.getRoleLink();
263 		} else {
264 			link = roleName;
265 		}
266 
267 		return this._getHttpServletRequest().isUserInRole(link);
268 	}
269 
270 	public Object getAttribute(String name) {
271 		if (name == null) {
272 			throw new IllegalArgumentException("Attribute name == null");
273 		}
274 
275 		Object attribute = this._getHttpServletRequest().getAttribute(
276 				NamespaceMapperAccess.getNamespaceMapper().encode(
277 						portletWindow.getId(), name));
278 
279 		if (attribute == null) {
280 			attribute = this._getHttpServletRequest().getAttribute(name);
281 		}
282 		return attribute;
283 	}
284 
285 	public Enumeration getAttributeNames() {
286 		Enumeration attributes = this._getHttpServletRequest()
287 				.getAttributeNames();
288 
289 		Vector portletAttributes = new Vector();
290 
291 		while (attributes.hasMoreElements()) {
292 			String attribute = (String) attributes.nextElement();
293 
294 			String portletAttribute = NamespaceMapperAccess
295 					.getNamespaceMapper().decode(portletWindow.getId(),
296 							attribute);
297 
298 			if (portletAttribute != null) { // it is in the portlet's namespace
299 				portletAttributes.add(portletAttribute);
300 			}
301 		}
302 
303 		return portletAttributes.elements();
304 	}
305 
306 	public String getParameter(String name) {
307 		if (name == null) {
308 			throw new IllegalArgumentException("Parameter name == null");
309 		}
310 
311 		bodyAccessed = true;
312 
313 		Map parameters = this._getHttpServletRequest().getParameterMap();
314 		String[] values = (String[]) parameters.get(name);
315 		if (values != null) {
316 			return values[0];
317 		}
318 		return null;
319 	}
320 
321 	public java.util.Enumeration getParameterNames() {
322 		bodyAccessed = true;
323 
324 		Map parameters = this._getHttpServletRequest().getParameterMap();
325 		return Collections.enumeration(parameters.keySet());
326 	}
327 
328 	public String[] getParameterValues(String name) {
329 		if (name == null) {
330 			throw new IllegalArgumentException("Parameter name == null");
331 		}
332 
333 		bodyAccessed = true;
334 
335 		String[] values = (String[]) this._getHttpServletRequest()
336 				.getParameterMap().get(name);
337 		if (values != null)
338 			values = StringUtils.copy(values);
339 		return values;
340 	}
341 
342 	public Map getParameterMap() {
343 		bodyAccessed = true;
344 		Map result = StringUtils.copyParameters(this._getHttpServletRequest()
345 				.getParameterMap());
346 		return result;
347 	}
348 
349 	public boolean isSecure() {
350 		return this._getHttpServletRequest().isSecure();
351 	}
352 
353 	public void setAttribute(String name, Object o) {
354 		if (name == null) {
355 			throw new IllegalArgumentException("Attribute name == null");
356 		}
357 
358 		if (o == null) {
359 			this.removeAttribute(name);
360 		} else if (isNameReserved(name)) {
361 			// Reserved names go directly in the underlying request
362 			_getHttpServletRequest().setAttribute(name, o);
363 		} else {
364 			this._getHttpServletRequest().setAttribute(
365 					NamespaceMapperAccess.getNamespaceMapper().encode(
366 							portletWindow.getId(), name), o);
367 		}
368 	}
369 
370 	public void removeAttribute(String name) {
371 		if (name == null) {
372 			throw new IllegalArgumentException("Attribute name == null");
373 		}
374 		if (isNameReserved(name)) {
375 			// Reserved names go directly in the underlying request
376 			_getHttpServletRequest().removeAttribute(name);
377 		} else {
378 
379 			this._getHttpServletRequest().removeAttribute(
380 					NamespaceMapperAccess.getNamespaceMapper().encode(
381 							portletWindow.getId(), name));
382 		}
383 	}
384 
385 	public String getRequestedSessionId() {
386 		return this._getHttpServletRequest().getRequestedSessionId();
387 	}
388 
389 	public boolean isRequestedSessionIdValid() {
390 		return this._getHttpServletRequest().isRequestedSessionIdValid();
391 	}
392 
393 	public String getResponseContentType() {
394 		// get the default response content type from the container
395 		String responseContentType = provider.getResponseContentType();
396 
397 		return responseContentType;
398 	}
399 
400 	public Enumeration getResponseContentTypes() {
401 		// get the default response content types from the container
402 		Iterator responseContentTypes = provider.getResponseContentTypes();
403 
404 		return new Enumerator(responseContentTypes);
405 	}
406 
407 	public java.util.Locale getLocale() {
408 		return this._getHttpServletRequest().getLocale();
409 	}
410 
411 	public Enumeration getLocales() {
412 		return this._getHttpServletRequest().getLocales();
413 	}
414 
415 	public String getScheme() {
416 		return this._getHttpServletRequest().getScheme();
417 	}
418 
419 	public String getServerName() {
420 		return this._getHttpServletRequest().getServerName();
421 	}
422 
423 	public int getServerPort() {
424 		return this._getHttpServletRequest().getServerPort();
425 	}
426 
427 	// --------------------------------------------------------------------------------------------
428 
429 	// org.apache.pluto.core.InternalPortletRequest implementation
430 	// --------------------------------
431 	public void lateInit(
432 			javax.servlet.http.HttpServletRequest webModuleServletRequest) {
433 		this.setRequest(webModuleServletRequest);
434 	}
435 
436 	public PortletWindow getInternalPortletWindow() {
437 		return portletWindow;
438 	}
439 
440 	public void setIncluded(boolean included) {
441 		this.included = included;
442 	}
443 
444 	public boolean isIncluded() {
445 		return included;
446 	}
447 
448 	// --------------------------------------------------------------------------------------------
449 
450 	// internal methods
451 	// ---------------------------------------------------------------------------
452 	private javax.servlet.http.HttpServletRequest _getHttpServletRequest() {
453 		return (javax.servlet.http.HttpServletRequest) super.getRequest();
454 	}
455 
456 	/***
457 	 * Is this attribute name a reserved name (by the J2EE spec)?. Reserved
458 	 * names begin with "java." or "javax.".
459 	 */
460 	private boolean isNameReserved(String name) {
461 		return name.startsWith("java.") || name.startsWith("javax.");
462 	}
463 
464 	// --------------------------------------------------------------------------------------------
465 
466 	// additional methods
467 	// javax.servlet.http.HttpServletRequestWrapper
468 	public java.lang.String getCharacterEncoding() {
469 		return this._getHttpServletRequest().getCharacterEncoding();
470 	}
471 
472 	public java.lang.String getContentType() {
473 		if (included) {
474 			return null;
475 		} else {
476 			return this._getHttpServletRequest().getContentType();
477 		}
478 	}
479 
480 	public int getContentLength() {
481 		if (included) {
482 			return 0;
483 		} else {
484 			return _getHttpServletRequest().getContentLength();
485 		}
486 	}
487 
488 	public BufferedReader getReader()
489 			throws java.io.UnsupportedEncodingException, java.io.IOException {
490 		if (included) {
491 			return null;
492 		} else {
493 			// the super class will ensure that a IllegalStateException is
494 			// thrown if getInputStream() was called earlier
495 			BufferedReader reader = _getHttpServletRequest().getReader();
496 
497 			bodyAccessed = true;
498 
499 			return reader;
500 		}
501 	}
502 
503 	public Cookie[] getCookies() {
504 		return this._getHttpServletRequest().getCookies();
505 	}
506 
507 	public long getDateHeader(String name) {
508 		return this._getHttpServletRequest().getDateHeader(name);
509 	}
510 
511 	public String getHeader(String name) {
512 		return this._getHttpServletRequest().getHeader(name);
513 	}
514 
515 	public Enumeration getHeaders(String name) {
516 		return this._getHttpServletRequest().getHeaders(name);
517 	}
518 
519 	public Enumeration getHeaderNames() {
520 		return this._getHttpServletRequest().getHeaderNames();
521 	}
522 
523 	public int getIntHeader(String name) {
524 		return this._getHttpServletRequest().getIntHeader(name);
525 
526 	}
527 
528 	public String getPathInfo() {
529 		String attr = (String) super
530 				.getAttribute("javax.servlet.include.path_info");
531 		return (attr != null) ? attr : super.getPathInfo();
532 	}
533 
534 	public String getQueryString() {
535 		String attr = (String) super
536 				.getAttribute("javax.servlet.include.query_string");
537 		return (attr != null) ? attr : super.getQueryString();
538 	}
539 
540 	public String getPathTranslated() {
541 		return null;
542 	}
543 
544 	public String getRequestURI() {
545 		String attr = (String) super
546 				.getAttribute("javax.servlet.include.request_uri");
547 		return (attr != null) ? attr : super.getRequestURI();
548 	}
549 
550 	public StringBuffer getRequestURL() {
551 		return null;
552 	}
553 
554 	public String getServletPath() {
555 		String attr = (String) super
556 				.getAttribute("javax.servlet.include.servlet_path");
557 		return (attr != null) ? attr : super.getServletPath();
558 	}
559 
560 	public HttpSession getSession(boolean create) {
561 		return this._getHttpServletRequest().getSession(true);
562 	}
563 
564 	public HttpSession getSession() {
565 		return this._getHttpServletRequest().getSession();
566 	}
567 
568 	public String getMethod() {
569 		// TBD
570 		return this._getHttpServletRequest().getMethod();
571 	}
572 
573 	public boolean isRequestedSessionIdFromURL() {
574 		// TBD
575 		return this._getHttpServletRequest().isRequestedSessionIdFromURL();
576 	}
577 
578 	public boolean isRequestedSessionIdFromUrl() {
579 		return this._getHttpServletRequest().isRequestedSessionIdFromUrl();
580 	}
581 
582 	public boolean isRequestedSessionIdFromCookie() {
583 		return this._getHttpServletRequest().isRequestedSessionIdFromCookie();
584 	}
585 
586 	public String getProtocol() {
587 		return null;
588 	}
589 
590 	public String getRemoteAddr() {
591 		return null;
592 	}
593 
594 	public String getRemoteHost() {
595 		return null;
596 	}
597 
598 	public String getRealPath(String path) {
599 		return null;
600 	}
601 
602 	public void setCharacterEncoding(String env)
603 			throws java.io.UnsupportedEncodingException {
604 		if (bodyAccessed) {
605 			throw new IllegalStateException(
606 					"This method must not be called after the HTTP-Body was accessed !");
607 		}
608 
609 		this._getHttpServletRequest().setCharacterEncoding(env);
610 		return;
611 	}
612 
613 	public javax.servlet.ServletInputStream getInputStream()
614 			throws java.io.IOException {
615 		if (included) {
616 			return null;
617 		} else {
618 			// the super class will ensure that a IllegalStateException is
619 			// thrown if getReader() was called earlier
620 			javax.servlet.ServletInputStream stream = _getHttpServletRequest()
621 					.getInputStream();
622 
623 			bodyAccessed = true;
624 
625 			return stream;
626 		}
627 	}
628 
629 	public javax.servlet.RequestDispatcher getRequestDispatcher(String path) {
630 		return this._getHttpServletRequest().getRequestDispatcher(path);
631 	}
632 }