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    
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, null, true);
036            }
037    
038            public DynamicServletRequest(HttpServletRequest request, boolean inherit) {
039                    this(request, null, 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                            _params.putAll(params);
059                    }
060    
061                    if (_inherit && (request instanceof DynamicServletRequest)) {
062                            DynamicServletRequest dynamicRequest =
063                                    (DynamicServletRequest)request;
064    
065                            setRequest(dynamicRequest.getRequest());
066    
067                            params = dynamicRequest.getDynamicParameterMap();
068    
069                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
070                                    String name = entry.getKey();
071                                    String[] oldValues = entry.getValue();
072    
073                                    String[] curValues = _params.get(name);
074    
075                                    if (curValues == null) {
076                                            _params.put(name, oldValues);
077                                    }
078                                    else {
079                                            String[] newValues = ArrayUtil.append(oldValues, curValues);
080    
081                                            _params.put(name, newValues);
082                                    }
083                            }
084                    }
085            }
086    
087            public Map<String, String[]> getDynamicParameterMap() {
088                    return _params;
089            }
090    
091            @Override
092            public String getParameter(String name) {
093                    String[] values = _params.get(name);
094    
095                    if (_inherit && (values == null)) {
096                            return super.getParameter(name);
097                    }
098    
099                    if ((values != null) && (values.length > 0)) {
100                            return values[0];
101                    }
102                    else {
103                            return null;
104                    }
105            }
106    
107            @Override
108            public Map<String, String[]> getParameterMap() {
109                    Map<String, String[]> map = new HashMap<String, String[]>();
110    
111                    if (_inherit) {
112                            map.putAll(super.getParameterMap());
113                    }
114    
115                    map.putAll(_params);
116    
117                    return map;
118            }
119    
120            @Override
121            public Enumeration<String> getParameterNames() {
122                    Set<String> names = new LinkedHashSet<String>();
123    
124                    if (_inherit) {
125                            Enumeration<String> enu = super.getParameterNames();
126    
127                            while (enu.hasMoreElements()) {
128                                    names.add(enu.nextElement());
129                            }
130                    }
131    
132                    names.addAll(_params.keySet());
133    
134                    return Collections.enumeration(names);
135            }
136    
137            @Override
138            public String[] getParameterValues(String name) {
139                    String[] values = _params.get(name);
140    
141                    if (_inherit && (values == null)) {
142                            return super.getParameterValues(name);
143                    }
144    
145                    return values;
146            }
147    
148            public void setParameter(String name, String value) {
149                    _params.put(name, new String[] {value});
150            }
151    
152            public void setParameterValues(String name, String[] values) {
153                    _params.put(name, values);
154            }
155    
156            private boolean _inherit;
157            private Map<String, String[]> _params;
158    
159    }