001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.util.JavaConstants;
018    import com.liferay.portal.util.PropsValues;
019    import com.liferay.util.servlet.DynamicServletRequest;
020    
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    import javax.portlet.MimeResponse;
029    import javax.portlet.PortletRequest;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * <p>
035     * This class ensures that portlet attributes and parameters are private to the
036     * portlet.
037     * </p>
038     *
039     * @author Brian Myunghun Kim
040     */
041    public class NamespaceServletRequest extends DynamicServletRequest {
042    
043            static Set<String> reservedAttrs = new HashSet<String>();
044    
045            static {
046                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
047                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
048                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
049                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
050                    reservedAttrs.add(MimeResponse.MARKUP_HEAD_ELEMENT);
051                    reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
052            }
053    
054            public NamespaceServletRequest(
055                    HttpServletRequest request, String attrNamespace,
056                    String paramNamespace) {
057    
058                    this(request, attrNamespace, paramNamespace, true);
059            }
060    
061            public NamespaceServletRequest(
062                    HttpServletRequest request, String attrNamespace, String paramNamespace,
063                    boolean inherit) {
064    
065                    super(request, inherit);
066    
067                    _attrNamespace = attrNamespace;
068                    _paramNamespace = paramNamespace;
069            }
070    
071            public Object getAttribute(String name) {
072                    Object value = super.getAttribute(_attrNamespace + name);
073    
074                    if (value == null) {
075                            value = super.getAttribute(name);
076                    }
077    
078                    return value;
079            }
080    
081            public Enumeration<String> getAttributeNames() {
082                    List<String> names = new ArrayList<String>();
083    
084                    Enumeration<String> enu = super.getAttributeNames();
085    
086                    while (enu.hasMoreElements()) {
087                            String name = enu.nextElement();
088    
089                            if (name.startsWith(_attrNamespace)) {
090                                    names.add(
091                                            name.substring(_attrNamespace.length(), name.length()));
092                            }
093                            else if (_isReservedParam(name)) {
094                                    names.add(name);
095                            }
096                    }
097    
098                    return Collections.enumeration(names);
099            }
100    
101            public void setAttribute(String name, Object value) {
102                    if (_isReservedParam(name)) {
103                            super.setAttribute(name, value);
104                    }
105                    else {
106                            super.setAttribute(_attrNamespace + name, value);
107                    }
108            }
109    
110            public void setAttribute(
111                    String name, Object value, boolean privateRequestAttribute) {
112    
113                    if (!privateRequestAttribute) {
114                            super.setAttribute(name, value);
115                    }
116                    else {
117                            setAttribute(name, value);
118                    }
119            }
120    
121            public void removeAttribute(String name) {
122                    if (_isReservedParam(name)) {
123                            super.removeAttribute(name);
124                    }
125                    else {
126                            super.removeAttribute(_attrNamespace + name);
127                    }
128            }
129    
130            public String getParameter(String name) {
131                    if (name == null) {
132                            throw new IllegalArgumentException();
133                    }
134    
135                    String value = super.getParameter(name);
136    
137                    if (value == null) {
138                            value = super.getParameter(_paramNamespace + name);
139                    }
140    
141                    return value;
142            }
143    
144            private boolean _isReservedParam(String name) {
145                    if (reservedAttrs.contains(name)) {
146                            return true;
147                    }
148                    else {
149                            for (String requestSharedAttribute :
150                                            PropsValues.REQUEST_SHARED_ATTRIBUTES) {
151    
152                                    if (name.startsWith(requestSharedAttribute)) {
153                                            return true;
154                                    }
155                            }
156                    }
157    
158                    return false;
159            }
160    
161            private String _attrNamespace;
162            private String _paramNamespace;
163    
164    }