1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util.servlet;
24  
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletRequestWrapper;
36  
37  /**
38   * <a href="DynamicServletRequest.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class DynamicServletRequest extends HttpServletRequestWrapper {
43  
44      public DynamicServletRequest(HttpServletRequest request) {
45          this(request, new HashMap<String, String[]>(), true);
46      }
47  
48      public DynamicServletRequest(HttpServletRequest request, boolean inherit) {
49          this(request, new HashMap<String, String[]>(), inherit);
50      }
51  
52      public DynamicServletRequest(
53          HttpServletRequest request, Map<String, String[]> params) {
54  
55          this(request, params, true);
56      }
57  
58      public DynamicServletRequest(
59          HttpServletRequest request, Map<String, String[]> params,
60          boolean inherit) {
61  
62          super(request);
63  
64          _params = new HashMap<String, String[]>();
65          _inherit = inherit;
66  
67          if (params != null) {
68              for (Map.Entry<String, String[]> entry : params.entrySet()) {
69                  _params.put(entry.getKey(), entry.getValue());
70              }
71          }
72  
73          if (_inherit && (request instanceof DynamicServletRequest)) {
74              DynamicServletRequest dynamicRequest =
75                  (DynamicServletRequest)request;
76  
77              setRequest(dynamicRequest.getRequest());
78  
79              params = dynamicRequest.getDynamicParameterMap();
80  
81              if (params != null) {
82                  for (Map.Entry<String, String[]> entry : params.entrySet()) {
83                      String name = entry.getKey();
84                      String[] oldValues = entry.getValue();
85  
86                      String[] curValues = _params.get(name);
87  
88                      if (curValues == null) {
89                          _params.put(name, oldValues);
90                      }
91                      else {
92                          String[] newValues = ArrayUtil.append(
93                              oldValues, curValues);
94  
95                          _params.put(name, newValues);
96                      }
97                  }
98              }
99          }
100     }
101 
102     public Map<String, String[]> getDynamicParameterMap() {
103         return _params;
104     }
105 
106     public String getParameter(String name) {
107         String[] values = _params.get(name);
108 
109         if (_inherit && (values == null)) {
110             return super.getParameter(name);
111         }
112 
113         if ((values != null) && (values.length > 0)) {
114             return values[0];
115         }
116         else {
117             return null;
118         }
119     }
120 
121     public Map<String, String[]> getParameterMap() {
122         Map<String, String[]> map = new HashMap<String, String[]>();
123 
124         Enumeration<String> enu = getParameterNames();
125 
126         while (enu.hasMoreElements()) {
127             String s = enu.nextElement();
128 
129             map.put(s, getParameterValues(s));
130         }
131 
132         return map;
133     }
134 
135     public Enumeration<String> getParameterNames() {
136         List<String> names = new ArrayList<String>();
137 
138         if (_inherit) {
139             Enumeration<String> enu = super.getParameterNames();
140 
141             while (enu.hasMoreElements()) {
142                 names.add(enu.nextElement());
143             }
144         }
145 
146         for (String s : _params.keySet()) {
147             if (!names.contains(s)) {
148                 names.add(s);
149             }
150         }
151 
152         return Collections.enumeration(names);
153     }
154 
155     public String[] getParameterValues(String name) {
156         String[] values = _params.get(name);
157 
158         if (_inherit && (values == null)) {
159             return super.getParameterValues(name);
160         }
161 
162         return values;
163     }
164 
165     public void setParameter(String name, String value) {
166         _params.put(name, new String[] {value});
167     }
168 
169     public void setParameterValues(String name, String[] values) {
170         _params.put(name, values);
171     }
172 
173     private boolean _inherit;
174     private Map<String, String[]> _params;
175 
176 }