1   /**
2    * Copyright (c) 2000-2009 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.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   */
51  public class NamespaceServletRequest extends DynamicServletRequest {
52  
53      static Set<String> reservedAttrs = new HashSet<String>();
54  
55      static {
56          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
57          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
58          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
59          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
60          reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
61      }
62  
63      public NamespaceServletRequest(
64          HttpServletRequest request, String attrNamespace,
65          String paramNamespace) {
66  
67          this(request, attrNamespace, paramNamespace, true);
68      }
69  
70      public NamespaceServletRequest(
71          HttpServletRequest request, String attrNamespace, String paramNamespace,
72          boolean inherit) {
73  
74          super(request, inherit);
75  
76          _attrNamespace = attrNamespace;
77          _paramNamespace = paramNamespace;
78      }
79  
80      public Object getAttribute(String name) {
81          Object value = super.getAttribute(_attrNamespace + name);
82  
83          if (value == null) {
84              value = super.getAttribute(name);
85          }
86  
87          return value;
88      }
89  
90      public Enumeration<String> getAttributeNames() {
91          List<String> names = new ArrayList<String>();
92  
93          Enumeration<String> enu = super.getAttributeNames();
94  
95          while (enu.hasMoreElements()) {
96              String name = enu.nextElement();
97  
98              if (name.startsWith(_attrNamespace)) {
99                  names.add(
100                     name.substring(_attrNamespace.length(), name.length()));
101             }
102         }
103 
104         return Collections.enumeration(names);
105     }
106 
107     public void setAttribute(String name, Object value) {
108         if (_isReservedParam(name)) {
109             super.setAttribute(name, value);
110         }
111         else {
112             super.setAttribute(_attrNamespace + name, value);
113         }
114     }
115 
116     public void setAttribute(
117         String name, Object value, boolean privateRequestAttribute) {
118 
119         if (!privateRequestAttribute) {
120             super.setAttribute(name, value);
121         }
122         else {
123             setAttribute(name, value);
124         }
125     }
126 
127     public void removeAttribute(String name) {
128         if (_isReservedParam(name)) {
129             super.removeAttribute(name);
130         }
131         else {
132             super.removeAttribute(_attrNamespace + name);
133         }
134     }
135 
136     public String getParameter(String name) {
137         if (name == null) {
138             throw new IllegalArgumentException();
139         }
140 
141         String value = super.getParameter(name);
142 
143         if (value == null) {
144             value = super.getParameter(_paramNamespace + name);
145         }
146 
147         return value;
148     }
149 
150     private boolean _isReservedParam(String name) {
151         if (reservedAttrs.contains(name)) {
152             return true;
153         }
154         else {
155             for (String requestSharedAttribute :
156                     PropsValues.REQUEST_SHARED_ATTRIBUTES) {
157 
158                 if (name.startsWith(requestSharedAttribute)) {
159                     return true;
160                 }
161             }
162         }
163 
164         return false;
165     }
166 
167     private String _attrNamespace;
168     private String _paramNamespace;
169 
170 }