001    /**
002     * Copyright (c) 2000-2012 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            @Override
097            public String getParameter(String name) {
098                    String[] values = _params.get(name);
099    
100                    if (_inherit && (values == null)) {
101                            return super.getParameter(name);
102                    }
103    
104                    if ((values != null) && (values.length > 0)) {
105                            return values[0];
106                    }
107                    else {
108                            return null;
109                    }
110            }
111    
112            @Override
113            public Map<String, String[]> getParameterMap() {
114                    Map<String, String[]> map = new HashMap<String, String[]>();
115    
116                    Enumeration<String> enu = getParameterNames();
117    
118                    while (enu.hasMoreElements()) {
119                            String s = enu.nextElement();
120    
121                            map.put(s, getParameterValues(s));
122                    }
123    
124                    return map;
125            }
126    
127            @Override
128            public Enumeration<String> getParameterNames() {
129                    Set<String> names = new LinkedHashSet<String>();
130    
131                    if (_inherit) {
132                            Enumeration<String> enu = super.getParameterNames();
133    
134                            while (enu.hasMoreElements()) {
135                                    names.add(enu.nextElement());
136                            }
137                    }
138    
139                    names.addAll(_params.keySet());
140    
141                    return Collections.enumeration(names);
142            }
143    
144            @Override
145            public String[] getParameterValues(String name) {
146                    String[] values = _params.get(name);
147    
148                    if (_inherit && (values == null)) {
149                            return super.getParameterValues(name);
150                    }
151    
152                    return values;
153            }
154    
155            public void setParameter(String name, String value) {
156                    _params.put(name, new String[] {value});
157            }
158    
159            public void setParameterValues(String name, String[] values) {
160                    _params.put(name, values);
161            }
162    
163            private boolean _inherit;
164            private Map<String, String[]> _params;
165    
166    }