1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.portlet;
21  
22  import com.liferay.portal.kernel.util.ArrayUtil;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.portlet.RenderRequest;
32  import javax.portlet.filter.RenderRequestWrapper;
33  
34  /**
35   * <a href="DynamicRenderRequest.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class DynamicRenderRequest extends RenderRequestWrapper {
41  
42      public DynamicRenderRequest(RenderRequest renderRequest) {
43          this(renderRequest, new HashMap<String, String[]>(), true);
44      }
45  
46      public DynamicRenderRequest(
47          RenderRequest renderRequest, Map<String, String[]> params) {
48  
49          this(renderRequest, params, true);
50      }
51  
52      public DynamicRenderRequest(RenderRequest renderRequest, boolean inherit) {
53          this(renderRequest, new HashMap<String, String[]>(), inherit);
54      }
55  
56      public DynamicRenderRequest(
57          RenderRequest renderRequest, Map<String, String[]> params,
58          boolean inherit) {
59  
60          super(renderRequest);
61  
62          _params = new HashMap<String, String[]>();
63          _inherit = inherit;
64  
65          if (params != null) {
66              for (Map.Entry<String, String[]> entry : params.entrySet()) {
67                  _params.put(entry.getKey(), entry.getValue());
68              }
69          }
70  
71          if (_inherit && (renderRequest instanceof DynamicRenderRequest)) {
72              DynamicRenderRequest dynamicRenderRequest =
73                  (DynamicRenderRequest)renderRequest;
74  
75              setRequest(dynamicRenderRequest.getRequest());
76  
77              params = dynamicRenderRequest.getDynamicParameterMap();
78  
79              if (params != null) {
80                  for (Map.Entry<String, String[]> entry : params.entrySet()) {
81                      String name = entry.getKey();
82                      String[] oldValues = entry.getValue();
83  
84                      String[] curValues = _params.get(name);
85  
86                      if (curValues == null) {
87                          _params.put(name, oldValues);
88                      }
89                      else {
90                          String[] newValues = ArrayUtil.append(
91                              oldValues, curValues);
92  
93                          _params.put(name, newValues);
94                      }
95                  }
96              }
97          }
98      }
99  
100     public String getParameter(String name) {
101         String[] values = _params.get(name);
102 
103         if (_inherit && (values == null)) {
104             return super.getParameter(name);
105         }
106 
107         if ((values != null) && (values.length > 0)) {
108             return values[0];
109         }
110         else {
111             return null;
112         }
113     }
114 
115     public Map<String, String[]> getParameterMap() {
116         Map<String, String[]> map = new HashMap<String, String[]>();
117 
118         Enumeration<String> enu = getParameterNames();
119 
120         while (enu.hasMoreElements()) {
121             String s = enu.nextElement();
122 
123             map.put(s, getParameterValues(s));
124         }
125 
126         return map;
127     }
128 
129     public Enumeration<String> getParameterNames() {
130         List<String> names = new ArrayList<String>();
131 
132         if (_inherit) {
133             Enumeration<String> enu = super.getParameterNames();
134 
135             while (enu.hasMoreElements()) {
136                 names.add(enu.nextElement());
137             }
138         }
139 
140         for (String s : _params.keySet()) {
141             if (!names.contains(s)) {
142                 names.add(s);
143             }
144         }
145 
146         return Collections.enumeration(names);
147     }
148 
149     public String[] getParameterValues(String name) {
150         String[] values = _params.get(name);
151 
152         if (_inherit && (values == null)) {
153             return super.getParameterValues(name);
154         }
155 
156         return values;
157     }
158 
159     public void setParameter(String name, String value) {
160         _params.put(name, new String[] {value});
161     }
162 
163     public void setParameterValues(String name, String[] values) {
164         _params.put(name, values);
165     }
166 
167     public Map<String, String[]> getDynamicParameterMap() {
168         return _params;
169     }
170 
171     private Map<String, String[]> _params;
172     private boolean _inherit;
173 
174 }