001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.PortletRequest;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * <p>
034     * This class ensures that portlet attributes and parameters are private to the
035     * portlet.
036     * </p>
037     *
038     * @author Brian Myunghun Kim
039     */
040    public class NamespaceServletRequest extends DynamicServletRequest {
041    
042            static Set<String> reservedAttrs = new HashSet<String>();
043    
044            static {
045                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
046                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
047                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
048                    reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
049                    reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
050            }
051    
052            public NamespaceServletRequest(
053                    HttpServletRequest request, String attrNamespace,
054                    String paramNamespace) {
055    
056                    this(request, attrNamespace, paramNamespace, true);
057            }
058    
059            public NamespaceServletRequest(
060                    HttpServletRequest request, String attrNamespace, String paramNamespace,
061                    boolean inherit) {
062    
063                    super(request, inherit);
064    
065                    _attrNamespace = attrNamespace;
066                    _paramNamespace = paramNamespace;
067            }
068    
069            public Object getAttribute(String name) {
070                    Object value = super.getAttribute(_attrNamespace + name);
071    
072                    if (value == null) {
073                            value = super.getAttribute(name);
074                    }
075    
076                    return value;
077            }
078    
079            public Enumeration<String> getAttributeNames() {
080                    List<String> names = new ArrayList<String>();
081    
082                    Enumeration<String> enu = super.getAttributeNames();
083    
084                    while (enu.hasMoreElements()) {
085                            String name = enu.nextElement();
086    
087                            if (name.startsWith(_attrNamespace)) {
088                                    names.add(
089                                            name.substring(_attrNamespace.length(), name.length()));
090                            }
091                            else if (_isReservedParam(name)) {
092                                    names.add(name);
093                            }
094                    }
095    
096                    return Collections.enumeration(names);
097            }
098    
099            public void setAttribute(String name, Object value) {
100                    if (_isReservedParam(name)) {
101                            super.setAttribute(name, value);
102                    }
103                    else {
104                            super.setAttribute(_attrNamespace + name, value);
105                    }
106            }
107    
108            public void setAttribute(
109                    String name, Object value, boolean privateRequestAttribute) {
110    
111                    if (!privateRequestAttribute) {
112                            super.setAttribute(name, value);
113                    }
114                    else {
115                            setAttribute(name, value);
116                    }
117            }
118    
119            public void removeAttribute(String name) {
120                    if (_isReservedParam(name)) {
121                            super.removeAttribute(name);
122                    }
123                    else {
124                            super.removeAttribute(_attrNamespace + name);
125                    }
126            }
127    
128            public String getParameter(String name) {
129                    if (name == null) {
130                            throw new IllegalArgumentException();
131                    }
132    
133                    String value = super.getParameter(name);
134    
135                    if (value == null) {
136                            value = super.getParameter(_paramNamespace + name);
137                    }
138    
139                    return value;
140            }
141    
142            private boolean _isReservedParam(String name) {
143                    if (reservedAttrs.contains(name)) {
144                            return true;
145                    }
146                    else {
147                            for (String requestSharedAttribute :
148                                            PropsValues.REQUEST_SHARED_ATTRIBUTES) {
149    
150                                    if (name.startsWith(requestSharedAttribute)) {
151                                            return true;
152                                    }
153                            }
154                    }
155    
156                    return false;
157            }
158    
159            private String _attrNamespace;
160            private String _paramNamespace;
161    
162    }