1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.PortletRequest;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  /**
33   * <a href="NamespaceServletRequest.java.html"><b><i>View Source</i></b></a>
34   *
35   * <p>
36   * This class ensures that portlet attributes and parameters are private to the
37   * portlet.
38   * </p>
39   *
40   * @author Brian Myunghun Kim
41   */
42  public class NamespaceServletRequest extends DynamicServletRequest {
43  
44      static Set<String> reservedAttrs = new HashSet<String>();
45  
46      static {
47          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
48          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
49          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
50          reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
51          reservedAttrs.add(PortletRequest.LIFECYCLE_PHASE);
52      }
53  
54      public NamespaceServletRequest(
55          HttpServletRequest request, String attrNamespace,
56          String paramNamespace) {
57  
58          this(request, attrNamespace, paramNamespace, true);
59      }
60  
61      public NamespaceServletRequest(
62          HttpServletRequest request, String attrNamespace, String paramNamespace,
63          boolean inherit) {
64  
65          super(request, inherit);
66  
67          _attrNamespace = attrNamespace;
68          _paramNamespace = paramNamespace;
69      }
70  
71      public Object getAttribute(String name) {
72          Object value = super.getAttribute(_attrNamespace + name);
73  
74          if (value == null) {
75              value = super.getAttribute(name);
76          }
77  
78          return value;
79      }
80  
81      public Enumeration<String> getAttributeNames() {
82          List<String> names = new ArrayList<String>();
83  
84          Enumeration<String> enu = super.getAttributeNames();
85  
86          while (enu.hasMoreElements()) {
87              String name = enu.nextElement();
88  
89              if (name.startsWith(_attrNamespace)) {
90                  names.add(
91                      name.substring(_attrNamespace.length(), name.length()));
92              }
93              else if (_isReservedParam(name)) {
94                  names.add(name);
95              }
96          }
97  
98          return Collections.enumeration(names);
99      }
100 
101     public void setAttribute(String name, Object value) {
102         if (_isReservedParam(name)) {
103             super.setAttribute(name, value);
104         }
105         else {
106             super.setAttribute(_attrNamespace + name, value);
107         }
108     }
109 
110     public void setAttribute(
111         String name, Object value, boolean privateRequestAttribute) {
112 
113         if (!privateRequestAttribute) {
114             super.setAttribute(name, value);
115         }
116         else {
117             setAttribute(name, value);
118         }
119     }
120 
121     public void removeAttribute(String name) {
122         if (_isReservedParam(name)) {
123             super.removeAttribute(name);
124         }
125         else {
126             super.removeAttribute(_attrNamespace + name);
127         }
128     }
129 
130     public String getParameter(String name) {
131         if (name == null) {
132             throw new IllegalArgumentException();
133         }
134 
135         String value = super.getParameter(name);
136 
137         if (value == null) {
138             value = super.getParameter(_paramNamespace + name);
139         }
140 
141         return value;
142     }
143 
144     private boolean _isReservedParam(String name) {
145         if (reservedAttrs.contains(name)) {
146             return true;
147         }
148         else {
149             for (String requestSharedAttribute :
150                     PropsValues.REQUEST_SHARED_ATTRIBUTES) {
151 
152                 if (name.startsWith(requestSharedAttribute)) {
153                     return true;
154                 }
155             }
156         }
157 
158         return false;
159     }
160 
161     private String _attrNamespace;
162     private String _paramNamespace;
163 
164 }