1   /**
2    * Copyright (c) 2000-2007 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.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.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletRequestWrapper;
37  
38  /**
39   * <a href="DynamicServletRequest.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class DynamicServletRequest extends HttpServletRequestWrapper {
45  
46      public DynamicServletRequest(HttpServletRequest req) {
47          this(req, new HashMap(), true);
48      }
49  
50      public DynamicServletRequest(HttpServletRequest req, Map params) {
51          this(req, params, true);
52      }
53  
54      public DynamicServletRequest(HttpServletRequest req, boolean inherit) {
55          this(req, new HashMap(), inherit);
56      }
57  
58      public DynamicServletRequest(HttpServletRequest req, Map params,
59                                   boolean inherit) {
60  
61          super(req);
62  
63          _params = new HashMap();
64          _inherit = inherit;
65  
66          if (params != null) {
67              Iterator itr = params.entrySet().iterator();
68  
69              while (itr.hasNext()) {
70                  Map.Entry entry = (Map.Entry)itr.next();
71  
72                  _params.put(entry.getKey(), entry.getValue());
73              }
74          }
75  
76          if (_inherit && (req instanceof DynamicServletRequest)) {
77              DynamicServletRequest dynamicReq = (DynamicServletRequest)req;
78  
79              setRequest(dynamicReq.getRequest());
80  
81              params = dynamicReq.getDynamicParameterMap();
82  
83              if (params != null) {
84                  Iterator itr = params.entrySet().iterator();
85  
86                  while (itr.hasNext()) {
87                      Map.Entry entry = (Map.Entry)itr.next();
88  
89                      String name = (String)entry.getKey();
90                      String[] oldValues = (String[])entry.getValue();
91  
92                      String[] curValues = (String[])_params.get(name);
93  
94                      if (curValues == null) {
95                          _params.put(name, oldValues);
96                      }
97                      else {
98                          String[] newValues = ArrayUtil.append(
99                              oldValues, curValues);
100 
101                         _params.put(name, newValues);
102                     }
103                 }
104             }
105         }
106     }
107 
108     public String getParameter(String name) {
109         String[] values = (String[])_params.get(name);
110 
111         if (_inherit && (values == null)) {
112             return super.getParameter(name);
113         }
114 
115         if ((values != null) && (values.length > 0)) {
116             return values[0];
117         }
118         else {
119             return null;
120         }
121     }
122 
123     public Map getParameterMap() {
124         Map map = new HashMap();
125 
126         Enumeration enu = getParameterNames();
127 
128         while (enu.hasMoreElements()) {
129             String s = (String)enu.nextElement();
130 
131             map.put(s, getParameterValues(s));
132         }
133 
134         return map;
135     }
136 
137     public Enumeration getParameterNames() {
138         List names = new ArrayList();
139 
140         if (_inherit) {
141             Enumeration enu = super.getParameterNames();
142 
143             while (enu.hasMoreElements()) {
144                 names.add(enu.nextElement());
145             }
146         }
147 
148         Iterator i = _params.keySet().iterator();
149 
150         while (i.hasNext()) {
151             String s = (String)i.next();
152 
153             if (!names.contains(s)) {
154                 names.add(s);
155             }
156         }
157 
158         return Collections.enumeration(names);
159     }
160 
161     public String[] getParameterValues(String name) {
162         String[] values = (String[])_params.get(name);
163 
164         if (_inherit && (values == null)) {
165             return super.getParameterValues(name);
166         }
167 
168         return values;
169     }
170 
171     public void setParameter(String name, String value) {
172         _params.put(name, new String[] {value});
173     }
174 
175     public void setParameterValues(String name, String[] values) {
176         _params.put(name, values);
177     }
178 
179     public Map getDynamicParameterMap() {
180         return _params;
181     }
182 
183     private Map _params;
184     private boolean _inherit;
185 
186 }