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