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.portlet;
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.portlet.ActionRequest;
027    import javax.portlet.filter.ActionRequestWrapper;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Eudaldo Alonso
032     * @see    DynamicEventRequest
033     * @see    DynamicRenderRequest
034     * @see    DynamicResourceRequest
035     */
036    public class DynamicActionRequest extends ActionRequestWrapper {
037    
038            public DynamicActionRequest(ActionRequest actionRequest) {
039                    this(actionRequest, null, true);
040            }
041    
042            public DynamicActionRequest(ActionRequest actionRequest, boolean inherit) {
043                    this(actionRequest, null, inherit);
044            }
045    
046            public DynamicActionRequest(
047                    ActionRequest actionRequest, Map<String, String[]> params) {
048    
049                    this(actionRequest, params, true);
050            }
051    
052            public DynamicActionRequest(
053                    ActionRequest actionRequest, Map<String, String[]> params,
054                    boolean inherit) {
055    
056                    super(actionRequest);
057    
058                    _params = new HashMap<String, String[]>();
059                    _inherit = inherit;
060    
061                    if (params != null) {
062                            _params.putAll(params);
063                    }
064    
065                    if (_inherit && (actionRequest instanceof DynamicActionRequest)) {
066                            DynamicActionRequest dynamicActionRequest =
067                                    (DynamicActionRequest)actionRequest;
068    
069                            setRequest(dynamicActionRequest.getRequest());
070    
071                            params = dynamicActionRequest.getDynamicParameterMap();
072    
073                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
074                                    String name = entry.getKey();
075                                    String[] oldValues = entry.getValue();
076    
077                                    String[] curValues = _params.get(name);
078    
079                                    if (curValues == null) {
080                                            _params.put(name, oldValues);
081                                    }
082                                    else {
083                                            String[] newValues = ArrayUtil.append(oldValues, curValues);
084    
085                                            _params.put(name, newValues);
086                                    }
087                            }
088                    }
089            }
090    
091            public Map<String, String[]> getDynamicParameterMap() {
092                    return _params;
093            }
094    
095            @Override
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            @Override
112            public Map<String, String[]> getParameterMap() {
113                    Map<String, String[]> map = new HashMap<String, String[]>();
114    
115                    if (_inherit) {
116                            map.putAll(super.getParameterMap());
117                    }
118    
119                    map.putAll(_params);
120    
121                    return map;
122            }
123    
124            @Override
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            @Override
142            public String[] getParameterValues(String name) {
143                    String[] values = _params.get(name);
144    
145                    if (_inherit && (values == null)) {
146                            return super.getParameterValues(name);
147                    }
148    
149                    return values;
150            }
151    
152            public void setParameter(String name, String value) {
153                    _params.put(name, new String[] {value});
154            }
155    
156            public void setParameterValues(String name, String[] values) {
157                    _params.put(name, values);
158            }
159    
160            private boolean _inherit;
161            private Map<String, String[]> _params;
162    
163    }