001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.LinkedHashSet;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletRequestWrapper;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class DynamicServletRequest extends HttpServletRequestWrapper {
037    
038            public static final String DYNAMIC_QUERY_STRING = "DYNAMIC_QUERY_STRING";
039    
040            public static HttpServletRequest addQueryString(
041                    HttpServletRequest request, String queryString) {
042    
043                    return addQueryString(request, queryString, true);
044            }
045    
046            public static HttpServletRequest addQueryString(
047                    HttpServletRequest request, String queryString, boolean inherit) {
048    
049                    String[] parameters = StringUtil.split(queryString, CharPool.AMPERSAND);
050    
051                    if (parameters.length == 0) {
052                            return request;
053                    }
054    
055                    Map<String, String[]> parameterMap = new HashMap<String, String[]>();
056    
057                    for (String parameter : parameters) {
058                            String[] parameterParts = StringUtil.split(
059                                    parameter, CharPool.EQUAL);
060    
061                            String name = parameterParts[0];
062                            String value = StringPool.BLANK;
063    
064                            if (parameterParts.length == 2) {
065                                    value = parameterParts[1];
066                            }
067    
068                            String[] values = parameterMap.get(name);
069    
070                            if (values == null) {
071                                    parameterMap.put(name, new String[] {value});
072                            }
073                            else {
074                                    String[] newValues = new String[values.length + 1];
075    
076                                    System.arraycopy(values, 0, newValues, 0, values.length);
077    
078                                    newValues[newValues.length - 1] = value;
079    
080                                    parameterMap.put(name, newValues);
081                            }
082                    }
083    
084                    request = new DynamicServletRequest(request, parameterMap, inherit);
085    
086                    request.setAttribute(DYNAMIC_QUERY_STRING, queryString);
087    
088                    return request;
089            }
090    
091            public DynamicServletRequest(HttpServletRequest request) {
092                    this(request, null, true);
093            }
094    
095            public DynamicServletRequest(HttpServletRequest request, boolean inherit) {
096                    this(request, null, inherit);
097            }
098    
099            public DynamicServletRequest(
100                    HttpServletRequest request, Map<String, String[]> params) {
101    
102                    this(request, params, true);
103            }
104    
105            public DynamicServletRequest(
106                    HttpServletRequest request, Map<String, String[]> params,
107                    boolean inherit) {
108    
109                    super(request);
110    
111                    _params = new HashMap<String, String[]>();
112                    _inherit = inherit;
113    
114                    if (params != null) {
115                            _params.putAll(params);
116                    }
117    
118                    if (_inherit && (request instanceof DynamicServletRequest)) {
119                            DynamicServletRequest dynamicRequest =
120                                    (DynamicServletRequest)request;
121    
122                            setRequest(dynamicRequest.getRequest());
123    
124                            params = dynamicRequest.getDynamicParameterMap();
125    
126                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
127                                    String name = entry.getKey();
128                                    String[] oldValues = entry.getValue();
129    
130                                    String[] curValues = _params.get(name);
131    
132                                    if (curValues == null) {
133                                            _params.put(name, oldValues);
134                                    }
135                                    else {
136                                            String[] newValues = ArrayUtil.append(oldValues, curValues);
137    
138                                            _params.put(name, newValues);
139                                    }
140                            }
141                    }
142            }
143    
144            public void appendParameter(String name, String value) {
145                    String[] values = _params.get(name);
146    
147                    if (values == null) {
148                            values = new String[] {value};
149                    }
150                    else {
151                            String[] newValues = new String[values.length + 1];
152    
153                            System.arraycopy(values, 0, newValues, 0, values.length);
154    
155                            newValues[newValues.length - 1] = value;
156    
157                            values = newValues;
158                    }
159    
160                    _params.put(name, values);
161            }
162    
163            public Map<String, String[]> getDynamicParameterMap() {
164                    return _params;
165            }
166    
167            @Override
168            public String getParameter(String name) {
169                    String[] values = _params.get(name);
170    
171                    if (_inherit && (values == null)) {
172                            return super.getParameter(name);
173                    }
174    
175                    if (ArrayUtil.isNotEmpty(values)) {
176                            return values[0];
177                    }
178                    else {
179                            return null;
180                    }
181            }
182    
183            @Override
184            public Map<String, String[]> getParameterMap() {
185                    Map<String, String[]> map = new HashMap<String, String[]>();
186    
187                    if (_inherit) {
188                            map.putAll(super.getParameterMap());
189                    }
190    
191                    map.putAll(_params);
192    
193                    return map;
194            }
195    
196            @Override
197            public Enumeration<String> getParameterNames() {
198                    Set<String> names = new LinkedHashSet<String>();
199    
200                    if (_inherit) {
201                            Enumeration<String> enu = super.getParameterNames();
202    
203                            while (enu.hasMoreElements()) {
204                                    names.add(enu.nextElement());
205                            }
206                    }
207    
208                    names.addAll(_params.keySet());
209    
210                    return Collections.enumeration(names);
211            }
212    
213            @Override
214            public String[] getParameterValues(String name) {
215                    String[] values = _params.get(name);
216    
217                    if (_inherit && (values == null)) {
218                            return super.getParameterValues(name);
219                    }
220    
221                    return values;
222            }
223    
224            public void setParameter(String name, String value) {
225                    _params.put(name, new String[] {value});
226            }
227    
228            public void setParameterValues(String name, String[] values) {
229                    _params.put(name, values);
230            }
231    
232            private boolean _inherit;
233            private Map<String, String[]> _params;
234    
235    }