1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.kernel.util.JavaConstants;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.util.servlet.DynamicServletRequest;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.Enumeration;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  /**
41   * <a href="NamespaceServletRequest.java.html"><b><i>View Source</i></b></a>
42   *
43   * <p>
44   * This class ensures that portlet attributes and parameters are private to the
45   * portlet.
46   * </p>
47   *
48   * @author Brian Myunghun Kim
49   */
50  public class NamespaceServletRequest extends DynamicServletRequest {
51  
52      static Set<String> reservedAttrs = new HashSet<String>();
53  
54      static {
55          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
56          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
57          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
58          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
59          reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
60      }
61  
62      public NamespaceServletRequest(
63          HttpServletRequest request, String attrNamespace,
64          String paramNamespace) {
65  
66          this(request, attrNamespace, paramNamespace, true);
67      }
68  
69      public NamespaceServletRequest(
70          HttpServletRequest request, String attrNamespace, String paramNamespace,
71          boolean inherit) {
72  
73          super(request, inherit);
74  
75          _attrNamespace = attrNamespace;
76          _paramNamespace = paramNamespace;
77      }
78  
79      public Object getAttribute(String name) {
80          Object value = super.getAttribute(_attrNamespace + name);
81  
82          if (value == null) {
83              value = super.getAttribute(name);
84          }
85  
86          return value;
87      }
88  
89      public Enumeration<String> getAttributeNames() {
90          List<String> names = new ArrayList<String>();
91  
92          Enumeration<String> enu = super.getAttributeNames();
93  
94          while (enu.hasMoreElements()) {
95              String name = enu.nextElement();
96  
97              if (name.startsWith(_attrNamespace)) {
98                  names.add(
99                      name.substring(_attrNamespace.length(), name.length()));
100             }
101         }
102 
103         return Collections.enumeration(names);
104     }
105 
106     public void setAttribute(String name, Object value) {
107         if (_isReservedParam(name)) {
108             super.setAttribute(name, value);
109         }
110         else {
111             super.setAttribute(_attrNamespace + name, value);
112         }
113     }
114 
115     public void setAttribute(
116         String name, Object value, boolean privateRequestAttribute) {
117 
118         if (!privateRequestAttribute) {
119             super.setAttribute(name, value);
120         }
121         else {
122             setAttribute(name, value);
123         }
124     }
125 
126     public void removeAttribute(String name) {
127         if (_isReservedParam(name)) {
128             super.removeAttribute(name);
129         }
130         else {
131             super.removeAttribute(_attrNamespace + name);
132         }
133     }
134 
135     public String getParameter(String name) {
136         if (name == null) {
137             throw new IllegalArgumentException();
138         }
139 
140         String value = super.getParameter(name);
141 
142         if (value == null) {
143             value = super.getParameter(_paramNamespace + name);
144         }
145 
146         return value;
147     }
148 
149     private boolean _isReservedParam(String name) {
150         if (reservedAttrs.contains(name)) {
151             return true;
152         }
153         else {
154             for (String requestSharedAttribute :
155                     PropsValues.REQUEST_SHARED_ATTRIBUTES) {
156 
157                 if (name.startsWith(requestSharedAttribute)) {
158                     return true;
159                 }
160             }
161         }
162 
163         return false;
164     }
165 
166     private String _attrNamespace;
167     private String _paramNamespace;
168 
169 }