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