1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DynamicRenderRequest extends RenderRequestWrapper {
43  
44      public DynamicRenderRequest(RenderRequest renderRequest) {
45          this(renderRequest, new HashMap<String, String[]>(), true);
46      }
47  
48      public DynamicRenderRequest(
49          RenderRequest renderRequest, Map<String, String[]> params) {
50  
51          this(renderRequest, params, true);
52      }
53  
54      public DynamicRenderRequest(RenderRequest renderRequest, boolean inherit) {
55          this(renderRequest, new HashMap<String, String[]>(), inherit);
56      }
57  
58      public DynamicRenderRequest(
59          RenderRequest renderRequest, Map<String, String[]> params,
60          boolean inherit) {
61  
62          super(renderRequest);
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 && (renderRequest instanceof DynamicRenderRequest)) {
74              DynamicRenderRequest dynamicRenderRequest =
75                  (DynamicRenderRequest)renderRequest;
76  
77              setRequest(dynamicRenderRequest.getRequest());
78  
79              params = dynamicRenderRequest.getDynamicParameterMap();
80  
81              if (params != null) {
82                  for (Map.Entry<String, String[]> entry : params.entrySet()) {
83                      String name = entry.getKey();
84                      String[] oldValues = entry.getValue();
85  
86                      String[] curValues = _params.get(name);
87  
88                      if (curValues == null) {
89                          _params.put(name, oldValues);
90                      }
91                      else {
92                          String[] newValues = ArrayUtil.append(
93                              oldValues, curValues);
94  
95                          _params.put(name, newValues);
96                      }
97                  }
98              }
99          }
100     }
101 
102     public String getParameter(String name) {
103         String[] values = _params.get(name);
104 
105         if (_inherit && (values == null)) {
106             return super.getParameter(name);
107         }
108 
109         if ((values != null) && (values.length > 0)) {
110             return values[0];
111         }
112         else {
113             return null;
114         }
115     }
116 
117     public Map<String, String[]> getParameterMap() {
118         Map<String, String[]> map = new HashMap<String, String[]>();
119 
120         Enumeration<String> enu = getParameterNames();
121 
122         while (enu.hasMoreElements()) {
123             String s = enu.nextElement();
124 
125             map.put(s, getParameterValues(s));
126         }
127 
128         return map;
129     }
130 
131     public Enumeration<String> getParameterNames() {
132         List<String> names = new ArrayList<String>();
133 
134         if (_inherit) {
135             Enumeration<String> enu = super.getParameterNames();
136 
137             while (enu.hasMoreElements()) {
138                 names.add(enu.nextElement());
139             }
140         }
141 
142         for (String s : _params.keySet()) {
143             if (!names.contains(s)) {
144                 names.add(s);
145             }
146         }
147 
148         return Collections.enumeration(names);
149     }
150 
151     public String[] getParameterValues(String name) {
152         String[] values = _params.get(name);
153 
154         if (_inherit && (values == null)) {
155             return super.getParameterValues(name);
156         }
157 
158         return values;
159     }
160 
161     public void setParameter(String name, String value) {
162         _params.put(name, new String[] {value});
163     }
164 
165     public void setParameterValues(String name, String[] values) {
166         _params.put(name, values);
167     }
168 
169     public Map<String, String[]> getDynamicParameterMap() {
170         return _params;
171     }
172 
173     private Map<String, String[]> _params;
174     private boolean _inherit;
175 
176 }