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.util.servlet;
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.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletRequestWrapper;
035:
036: /**
037: * <a href="DynamicServletRequest.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class DynamicServletRequest extends HttpServletRequestWrapper {
043:
044: public DynamicServletRequest(HttpServletRequest req) {
045: this (req, new HashMap(), true);
046: }
047:
048: public DynamicServletRequest(HttpServletRequest req, Map params) {
049: this (req, params, true);
050: }
051:
052: public DynamicServletRequest(HttpServletRequest req, boolean inherit) {
053: this (req, new HashMap(), inherit);
054: }
055:
056: public DynamicServletRequest(HttpServletRequest req, Map params,
057: boolean inherit) {
058:
059: super (req);
060:
061: _params = new HashMap();
062: _inherit = inherit;
063:
064: if (params != null) {
065: Iterator itr = params.entrySet().iterator();
066:
067: while (itr.hasNext()) {
068: Map.Entry entry = (Map.Entry) itr.next();
069:
070: _params.put(entry.getKey(), entry.getValue());
071: }
072: }
073:
074: if (_inherit && (req instanceof DynamicServletRequest)) {
075: DynamicServletRequest dynamicReq = (DynamicServletRequest) req;
076:
077: setRequest(dynamicReq.getRequest());
078:
079: params = dynamicReq.getDynamicParameterMap();
080:
081: if (params != null) {
082: Iterator itr = params.entrySet().iterator();
083:
084: while (itr.hasNext()) {
085: Map.Entry entry = (Map.Entry) itr.next();
086:
087: String name = (String) entry.getKey();
088: String[] oldValues = (String[]) entry.getValue();
089:
090: String[] curValues = (String[]) _params.get(name);
091:
092: if (curValues == null) {
093: _params.put(name, oldValues);
094: } else {
095: String[] newValues = ArrayUtil.append(
096: oldValues, curValues);
097:
098: _params.put(name, newValues);
099: }
100: }
101: }
102: }
103: }
104:
105: public String getParameter(String name) {
106: String[] values = (String[]) _params.get(name);
107:
108: if (_inherit && (values == null)) {
109: return super .getParameter(name);
110: }
111:
112: if ((values != null) && (values.length > 0)) {
113: return values[0];
114: } else {
115: return null;
116: }
117: }
118:
119: public Map getParameterMap() {
120: Map map = new HashMap();
121:
122: Enumeration enu = getParameterNames();
123:
124: while (enu.hasMoreElements()) {
125: String s = (String) enu.nextElement();
126:
127: map.put(s, getParameterValues(s));
128: }
129:
130: return map;
131: }
132:
133: public Enumeration getParameterNames() {
134: List names = new ArrayList();
135:
136: if (_inherit) {
137: Enumeration enu = super .getParameterNames();
138:
139: while (enu.hasMoreElements()) {
140: names.add(enu.nextElement());
141: }
142: }
143:
144: Iterator i = _params.keySet().iterator();
145:
146: while (i.hasNext()) {
147: String s = (String) i.next();
148:
149: if (!names.contains(s)) {
150: names.add(s);
151: }
152: }
153:
154: return Collections.enumeration(names);
155: }
156:
157: public String[] getParameterValues(String name) {
158: String[] values = (String[]) _params.get(name);
159:
160: if (_inherit && (values == null)) {
161: return super .getParameterValues(name);
162: }
163:
164: return values;
165: }
166:
167: public void setParameter(String name, String value) {
168: _params.put(name, new String[] { value });
169: }
170:
171: public void setParameterValues(String name, String[] values) {
172: _params.put(name, values);
173: }
174:
175: public Map getDynamicParameterMap() {
176: return _params;
177: }
178:
179: private Map _params;
180: private boolean _inherit;
181:
182: }
|