1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.PropsUtil;
27  import com.liferay.util.CollectionFactory;
28  import com.liferay.util.servlet.DynamicServletRequest;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.Enumeration;
33  import java.util.List;
34  import java.util.Set;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="NamespaceServletRequest.java.html"><b><i>View Source</i></b></a>
40   *
41   * <p>
42   * This class ensures that portlet attributes and parameters are private to the
43   * portlet.
44   * </p>
45   *
46   * @author Brian Myunghun Kim
47   *
48   */
49  public class NamespaceServletRequest extends DynamicServletRequest {
50  
51      static Set reservedAttrs = CollectionFactory.getHashSet();
52  
53      static {
54          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
55          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
56          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
57          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
58      }
59  
60      public static final String[] CUSTOM_RESERVED_ATTRS = PropsUtil.getArray(
61          PropsUtil.REQUEST_SHARED_ATTRIBUTES);
62  
63      public NamespaceServletRequest(HttpServletRequest req, String attrNamespace,
64                                     String paramNamespace) {
65  
66          this(req, attrNamespace, paramNamespace, true);
67      }
68  
69      public NamespaceServletRequest(HttpServletRequest req, String attrNamespace,
70                                     String paramNamespace, boolean inherit) {
71  
72          super(req, inherit);
73  
74          _attrNamespace = attrNamespace;
75          _paramNamespace = paramNamespace;
76      }
77  
78      public Object getAttribute(String name) {
79          Object value = super.getAttribute(_attrNamespace + name);
80  
81          if (value == null) {
82              value = super.getAttribute(name);
83          }
84  
85          return value;
86      }
87  
88      public Enumeration getAttributeNames() {
89          List names = new ArrayList();
90  
91          Enumeration enu = super.getAttributeNames();
92  
93          while (enu.hasMoreElements()) {
94              String name = (String)enu.nextElement();
95  
96              if (name.startsWith(_attrNamespace)) {
97                  names.add(
98                      name.substring(_attrNamespace.length(), name.length()));
99              }
100         }
101 
102         return Collections.enumeration(names);
103     }
104 
105     public void setAttribute(String name, Object value) {
106         if (_isReservedParam(name)) {
107             super.setAttribute(name, value);
108         }
109         else {
110             super.setAttribute(_attrNamespace + name, value);
111         }
112     }
113 
114     public void setAttribute(
115         String name, Object value, boolean privateRequestAttribute) {
116 
117         if (!privateRequestAttribute) {
118             super.setAttribute(name, value);
119         }
120         else {
121             setAttribute(name, value);
122         }
123     }
124 
125     public void removeAttribute(String name) {
126         if (_isReservedParam(name)) {
127             super.removeAttribute(name);
128         }
129         else {
130             super.removeAttribute(_attrNamespace + name);
131         }
132     }
133 
134     public String getParameter(String name) {
135         if (name == null) {
136             throw new IllegalArgumentException();
137         }
138 
139         String value = super.getParameter(name);
140 
141         if (value == null) {
142             value = super.getParameter(_paramNamespace + name);
143         }
144 
145         return value;
146     }
147 
148     private boolean _isReservedParam(String name) {
149         if (reservedAttrs.contains(name)) {
150             return true;
151         }
152         else {
153             for (int i = 0; i < CUSTOM_RESERVED_ATTRS.length; i++) {
154                 if (name.startsWith(CUSTOM_RESERVED_ATTRS[i])) {
155                     return true;
156                 }
157             }
158         }
159 
160         return false;
161     }
162 
163     private String _attrNamespace;
164     private String _paramNamespace;
165 
166 }