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.PropsKeys;
27  import com.liferay.portal.util.PropsUtil;
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.HashSet;
34  import java.util.List;
35  import java.util.Set;
36  
37  import javax.portlet.PortletRequest;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  /**
42   * <a href="NamespaceServletRequest.java.html"><b><i>View Source</i></b></a>
43   *
44   * <p>
45   * This class ensures that portlet attributes and parameters are private to the
46   * portlet.
47   * </p>
48   *
49   * @author Brian Myunghun Kim
50   *
51   */
52  public class NamespaceServletRequest extends DynamicServletRequest {
53  
54      static Set<String> reservedAttrs = new HashSet<String>();
55  
56      static {
57          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
58          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
59          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
60          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
61          reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
62      }
63  
64      public static final String[] CUSTOM_RESERVED_ATTRS = PropsUtil.getArray(
65          PropsKeys.REQUEST_SHARED_ATTRIBUTES);
66  
67      public NamespaceServletRequest(
68          HttpServletRequest request, String attrNamespace,
69          String paramNamespace) {
70  
71          this(request, attrNamespace, paramNamespace, true);
72      }
73  
74      public NamespaceServletRequest(
75          HttpServletRequest request, String attrNamespace, String paramNamespace,
76          boolean inherit) {
77  
78          super(request, inherit);
79  
80          _attrNamespace = attrNamespace;
81          _paramNamespace = paramNamespace;
82      }
83  
84      public Object getAttribute(String name) {
85          Object value = super.getAttribute(_attrNamespace + name);
86  
87          if (value == null) {
88              value = super.getAttribute(name);
89          }
90  
91          return value;
92      }
93  
94      public Enumeration<String> getAttributeNames() {
95          List<String> names = new ArrayList<String>();
96  
97          Enumeration<String> enu = super.getAttributeNames();
98  
99          while (enu.hasMoreElements()) {
100             String name = enu.nextElement();
101 
102             if (name.startsWith(_attrNamespace)) {
103                 names.add(
104                     name.substring(_attrNamespace.length(), name.length()));
105             }
106         }
107 
108         return Collections.enumeration(names);
109     }
110 
111     public void setAttribute(String name, Object value) {
112         if (_isReservedParam(name)) {
113             super.setAttribute(name, value);
114         }
115         else {
116             super.setAttribute(_attrNamespace + name, value);
117         }
118     }
119 
120     public void setAttribute(
121         String name, Object value, boolean privateRequestAttribute) {
122 
123         if (!privateRequestAttribute) {
124             super.setAttribute(name, value);
125         }
126         else {
127             setAttribute(name, value);
128         }
129     }
130 
131     public void removeAttribute(String name) {
132         if (_isReservedParam(name)) {
133             super.removeAttribute(name);
134         }
135         else {
136             super.removeAttribute(_attrNamespace + name);
137         }
138     }
139 
140     public String getParameter(String name) {
141         if (name == null) {
142             throw new IllegalArgumentException();
143         }
144 
145         String value = super.getParameter(name);
146 
147         if (value == null) {
148             value = super.getParameter(_paramNamespace + name);
149         }
150 
151         return value;
152     }
153 
154     private boolean _isReservedParam(String name) {
155         if (reservedAttrs.contains(name)) {
156             return true;
157         }
158         else {
159             for (int i = 0; i < CUSTOM_RESERVED_ATTRS.length; i++) {
160                 if (name.startsWith(CUSTOM_RESERVED_ATTRS[i])) {
161                     return true;
162                 }
163             }
164         }
165 
166         return false;
167     }
168 
169     private String _attrNamespace;
170     private String _paramNamespace;
171 
172 }