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