001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.portlet;
022:
023: import com.liferay.portal.kernel.util.ArrayUtil;
024:
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import javax.portlet.RenderRequest;
034:
035: /**
036: * <a href="DynamicRenderRequest.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class DynamicRenderRequest extends RenderRequestWrapper {
042:
043: public DynamicRenderRequest(RenderRequest req) {
044: this (req, new HashMap(), true);
045: }
046:
047: public DynamicRenderRequest(RenderRequest req, Map params) {
048: this (req, params, true);
049: }
050:
051: public DynamicRenderRequest(RenderRequest req, boolean inherit) {
052: this (req, new HashMap(), inherit);
053: }
054:
055: public DynamicRenderRequest(RenderRequest req, Map params,
056: boolean inherit) {
057:
058: super (req);
059:
060: _params = new HashMap();
061: _inherit = inherit;
062:
063: if (params != null) {
064: Iterator itr = params.entrySet().iterator();
065:
066: while (itr.hasNext()) {
067: Map.Entry entry = (Map.Entry) itr.next();
068:
069: _params.put(entry.getKey(), entry.getValue());
070: }
071: }
072:
073: if (_inherit && (req instanceof DynamicRenderRequest)) {
074: DynamicRenderRequest dynamicReq = (DynamicRenderRequest) req;
075:
076: setPortletRequest(dynamicReq.getPortletRequest());
077:
078: params = dynamicReq.getDynamicParameterMap();
079:
080: if (params != null) {
081: Iterator itr = params.entrySet().iterator();
082:
083: while (itr.hasNext()) {
084: Map.Entry entry = (Map.Entry) itr.next();
085:
086: String name = (String) entry.getKey();
087: String[] oldValues = (String[]) entry.getValue();
088:
089: String[] curValues = (String[]) _params.get(name);
090:
091: if (curValues == null) {
092: _params.put(name, oldValues);
093: } else {
094: String[] newValues = ArrayUtil.append(
095: oldValues, curValues);
096:
097: _params.put(name, newValues);
098: }
099: }
100: }
101: }
102: }
103:
104: public String getParameter(String name) {
105: String[] values = (String[]) _params.get(name);
106:
107: if (_inherit && (values == null)) {
108: return super .getParameter(name);
109: }
110:
111: if ((values != null) && (values.length > 0)) {
112: return values[0];
113: } else {
114: return null;
115: }
116: }
117:
118: public Map getParameterMap() {
119: Map map = new HashMap();
120:
121: Enumeration enu = getParameterNames();
122:
123: while (enu.hasMoreElements()) {
124: String s = (String) enu.nextElement();
125:
126: map.put(s, getParameterValues(s));
127: }
128:
129: return map;
130: }
131:
132: public Enumeration getParameterNames() {
133: List names = new ArrayList();
134:
135: if (_inherit) {
136: Enumeration enu = super .getParameterNames();
137:
138: while (enu.hasMoreElements()) {
139: names.add(enu.nextElement());
140: }
141: }
142:
143: Iterator i = _params.keySet().iterator();
144:
145: while (i.hasNext()) {
146: String s = (String) i.next();
147:
148: if (!names.contains(s)) {
149: names.add(s);
150: }
151: }
152:
153: return Collections.enumeration(names);
154: }
155:
156: public String[] getParameterValues(String name) {
157: String[] values = (String[]) _params.get(name);
158:
159: if (_inherit && (values == null)) {
160: return super .getParameterValues(name);
161: }
162:
163: return values;
164: }
165:
166: public void setParameter(String name, String value) {
167: _params.put(name, new String[] { value });
168: }
169:
170: public void setParameterValues(String name, String[] values) {
171: _params.put(name, values);
172: }
173:
174: public Map getDynamicParameterMap() {
175: return _params;
176: }
177:
178: private Map _params;
179: private boolean _inherit;
180:
181: }
|