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