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