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.portal.kernel.portlet;
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.portlet.RenderRequest;
35  import javax.portlet.filter.RenderRequestWrapper;
36  
37  /**
38   * <a href="DynamicRenderRequest.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class DynamicRenderRequest extends RenderRequestWrapper {
44  
45      public DynamicRenderRequest(RenderRequest renderRequest) {
46          this(renderRequest, new HashMap<String, String[]>(), true);
47      }
48  
49      public DynamicRenderRequest(
50          RenderRequest renderRequest, Map<String, String[]> params) {
51  
52          this(renderRequest, params, true);
53      }
54  
55      public DynamicRenderRequest(RenderRequest renderRequest, boolean inherit) {
56          this(renderRequest, new HashMap<String, String[]>(), inherit);
57      }
58  
59      public DynamicRenderRequest(
60          RenderRequest renderRequest, Map<String, String[]> params,
61          boolean inherit) {
62  
63          super(renderRequest);
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 && (renderRequest instanceof DynamicRenderRequest)) {
75              DynamicRenderRequest dynamicRenderRequest =
76                  (DynamicRenderRequest)renderRequest;
77  
78              setRequest(dynamicRenderRequest.getRequest());
79  
80              params = dynamicRenderRequest.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 String getParameter(String name) {
104         String[] values = _params.get(name);
105 
106         if (_inherit && (values == null)) {
107             return super.getParameter(name);
108         }
109 
110         if ((values != null) && (values.length > 0)) {
111             return values[0];
112         }
113         else {
114             return null;
115         }
116     }
117 
118     public Map<String, String[]> getParameterMap() {
119         Map<String, String[]> map = new HashMap<String, String[]>();
120 
121         Enumeration<String> enu = getParameterNames();
122 
123         while (enu.hasMoreElements()) {
124             String s = enu.nextElement();
125 
126             map.put(s, getParameterValues(s));
127         }
128 
129         return map;
130     }
131 
132     public Enumeration<String> getParameterNames() {
133         List<String> names = new ArrayList<String>();
134 
135         if (_inherit) {
136             Enumeration<String> enu = super.getParameterNames();
137 
138             while (enu.hasMoreElements()) {
139                 names.add(enu.nextElement());
140             }
141         }
142 
143         for (String s : _params.keySet()) {
144             if (!names.contains(s)) {
145                 names.add(s);
146             }
147         }
148 
149         return Collections.enumeration(names);
150     }
151 
152     public String[] getParameterValues(String name) {
153         String[] values = _params.get(name);
154 
155         if (_inherit && (values == null)) {
156             return super.getParameterValues(name);
157         }
158 
159         return values;
160     }
161 
162     public void setParameter(String name, String value) {
163         _params.put(name, new String[] {value});
164     }
165 
166     public void setParameterValues(String name, String[] values) {
167         _params.put(name, values);
168     }
169 
170     public Map<String, String[]> getDynamicParameterMap() {
171         return _params;
172     }
173 
174     private Map<String, String[]> _params;
175     private boolean _inherit;
176 
177 }