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