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