001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.servlet;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    
019    import java.util.Collections;
020    import java.util.Enumeration;
021    import java.util.HashMap;
022    import java.util.LinkedHashSet;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletRequestWrapper;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class DynamicServletRequest extends HttpServletRequestWrapper {
033    
034            public DynamicServletRequest(HttpServletRequest request) {
035                    this(request, new HashMap<String, String[]>(), true);
036            }
037    
038            public DynamicServletRequest(HttpServletRequest request, boolean inherit) {
039                    this(request, new HashMap<String, String[]>(), inherit);
040            }
041    
042            public DynamicServletRequest(
043                    HttpServletRequest request, Map<String, String[]> params) {
044    
045                    this(request, params, true);
046            }
047    
048            public DynamicServletRequest(
049                    HttpServletRequest request, Map<String, String[]> params,
050                    boolean inherit) {
051    
052                    super(request);
053    
054                    _params = new HashMap<String, String[]>();
055                    _inherit = inherit;
056    
057                    if (params != null) {
058                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
059                                    _params.put(entry.getKey(), entry.getValue());
060                            }
061                    }
062    
063                    if (_inherit && (request instanceof DynamicServletRequest)) {
064                            DynamicServletRequest dynamicRequest =
065                                    (DynamicServletRequest)request;
066    
067                            setRequest(dynamicRequest.getRequest());
068    
069                            params = dynamicRequest.getDynamicParameterMap();
070    
071                            if (params != null) {
072                                    for (Map.Entry<String, String[]> entry : params.entrySet()) {
073                                            String name = entry.getKey();
074                                            String[] oldValues = entry.getValue();
075    
076                                            String[] curValues = _params.get(name);
077    
078                                            if (curValues == null) {
079                                                    _params.put(name, oldValues);
080                                            }
081                                            else {
082                                                    String[] newValues = ArrayUtil.append(
083                                                            oldValues, curValues);
084    
085                                                    _params.put(name, newValues);
086                                            }
087                                    }
088                            }
089                    }
090            }
091    
092            public Map<String, String[]> getDynamicParameterMap() {
093                    return _params;
094            }
095    
096            public String getParameter(String name) {
097                    String[] values = _params.get(name);
098    
099                    if (_inherit && (values == null)) {
100                            return super.getParameter(name);
101                    }
102    
103                    if ((values != null) && (values.length > 0)) {
104                            return values[0];
105                    }
106                    else {
107                            return null;
108                    }
109            }
110    
111            public Map<String, String[]> getParameterMap() {
112                    Map<String, String[]> map = new HashMap<String, String[]>();
113    
114                    Enumeration<String> enu = getParameterNames();
115    
116                    while (enu.hasMoreElements()) {
117                            String s = enu.nextElement();
118    
119                            map.put(s, getParameterValues(s));
120                    }
121    
122                    return map;
123            }
124    
125            public Enumeration<String> getParameterNames() {
126                    Set<String> names = new LinkedHashSet<String>();
127    
128                    if (_inherit) {
129                            Enumeration<String> enu = super.getParameterNames();
130    
131                            while (enu.hasMoreElements()) {
132                                    names.add(enu.nextElement());
133                            }
134                    }
135    
136                    names.addAll(_params.keySet());
137    
138                    return Collections.enumeration(names);
139            }
140    
141            public String[] getParameterValues(String name) {
142                    String[] values = _params.get(name);
143    
144                    if (_inherit && (values == null)) {
145                            return super.getParameterValues(name);
146                    }
147    
148                    return values;
149            }
150    
151            public void setParameter(String name, String value) {
152                    _params.put(name, new String[] {value});
153            }
154    
155            public void setParameterValues(String name, String[] values) {
156                    _params.put(name, values);
157            }
158    
159            private boolean _inherit;
160            private Map<String, String[]> _params;
161    
162    }