001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.container.servlet;
007:
008: import java.util.Collections;
009: import java.util.Enumeration;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.Map;
013:
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletRequestWrapper;
016:
017: import org.jasig.portal.container.services.information.PortletStateManager;
018:
019: /**
020: * A {@link javax.servlet.http.HttpServletRequestWrapper} that only allows
021: * non-portal paramters to be seen. The determination is done by hidding
022: * parameters that begin with {@link PortletStateManager#UP_PARAM_PREFIX}.
023: * All other paramters are let through.
024: *
025: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
026: * @version $Revision: 36796 $
027: */
028: public class PortletParameterRequestWrapper extends
029: AttributeRequestWrapper {
030: private static final String ESCAPE_PREFIX = PortletStateManager.UP_PARAM_PREFIX
031: + PortletStateManager.UP_PARAM_PREFIX;
032:
033: /**
034: * Creates a new wrapper and wraps the specified request.
035: *
036: * @param request The requst to wrap.
037: * @see HttpServletRequestWrapper#HttpServletRequestWrapper(javax.servlet.http.HttpServletRequest)
038: */
039: public PortletParameterRequestWrapper(
040: final HttpServletRequest request) {
041: super (request);
042: }
043:
044: /*
045: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
046: */
047: public String getParameter(final String name) {
048: if (name != null
049: && name.startsWith(PortletStateManager.UP_PARAM_PREFIX)) {
050: return null;
051: } else {
052: return super .getParameter(name);
053: }
054: }
055:
056: /*
057: * @see javax.servlet.ServletRequest#getParameterMap()
058: */
059: public Map getParameterMap() {
060: return this .getPortletParameterMap();
061: }
062:
063: /*
064: * @see javax.servlet.ServletRequest#getParameterNames()
065: */
066: public Enumeration getParameterNames() {
067: return Collections.enumeration(this .getParameterMap().keySet());
068: }
069:
070: /*
071: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
072: */
073: public String[] getParameterValues(final String name) {
074: if (name != null
075: && name.startsWith(PortletStateManager.UP_PARAM_PREFIX)) {
076: return null;
077: } else {
078: return super .getParameterValues(name);
079: }
080: }
081:
082: /**
083: * Does the work of creating a {@link Map} of non-portal parameters
084: * for the public methods of this class to use.
085: *
086: * @return A {@link Map} of non-portal parameters.
087: */
088: private Map getPortletParameterMap() {
089: final Map allParams = super .getParameterMap();
090: final Map portletParams = new HashMap(allParams.size() * 2);
091:
092: for (final Iterator pNameItr = allParams.keySet().iterator(); pNameItr
093: .hasNext();) {
094: final String pName = (String) pNameItr.next();
095:
096: if (pName == null
097: || !pName
098: .startsWith(PortletStateManager.UP_PARAM_PREFIX)) {
099: final Object pVal = allParams.get(pName);
100:
101: portletParams.put(pName, pVal);
102: }
103: //Deals with parameters that are ok and start with the prefix
104: else if (pName.startsWith(ESCAPE_PREFIX)) {
105: final Object pVal = allParams.get(pName);
106:
107: portletParams.put(pName
108: .substring(PortletStateManager.UP_PARAM_PREFIX
109: .length()), pVal);
110: }
111: }
112:
113: return Collections.unmodifiableMap(portletParams);
114: }
115: }
|