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.ResourceRequest;
027    import javax.portlet.filter.ResourceRequestWrapper;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @see    DynamicActionRequest
032     * @see    DynamicEventRequest
033     * @see    DynamicRenderRequest
034     */
035    public class DynamicResourceRequest extends ResourceRequestWrapper {
036    
037            public DynamicResourceRequest(ResourceRequest resourceRequest) {
038                    this(resourceRequest, null, true);
039            }
040    
041            public DynamicResourceRequest(
042                    ResourceRequest resourceRequest, boolean inherit) {
043    
044                    this(resourceRequest, null, inherit);
045            }
046    
047            public DynamicResourceRequest(
048                    ResourceRequest resourceRequest, Map<String, String[]> params) {
049    
050                    this(resourceRequest, params, true);
051            }
052    
053            public DynamicResourceRequest(
054                    ResourceRequest resourceRequest, Map<String, String[]> params,
055                    boolean inherit) {
056    
057                    super(resourceRequest);
058    
059                    _params = new HashMap<String, String[]>();
060                    _inherit = inherit;
061    
062                    if (params != null) {
063                            _params.putAll(params);
064                    }
065    
066                    if (_inherit && (resourceRequest instanceof DynamicResourceRequest)) {
067                            DynamicResourceRequest dynamicResourceRequest =
068                                    (DynamicResourceRequest)resourceRequest;
069    
070                            setRequest(dynamicResourceRequest.getRequest());
071    
072                            params = dynamicResourceRequest.getDynamicParameterMap();
073    
074                            for (Map.Entry<String, String[]> entry : params.entrySet()) {
075                                    String name = entry.getKey();
076                                    String[] oldValues = entry.getValue();
077    
078                                    String[] curValues = _params.get(name);
079    
080                                    if (curValues == null) {
081                                            _params.put(name, oldValues);
082                                    }
083                                    else {
084                                            String[] newValues = ArrayUtil.append(oldValues, curValues);
085    
086                                            _params.put(name, newValues);
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 (ArrayUtil.isNotEmpty(values)) {
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                    if (_inherit) {
117                            map.putAll(super.getParameterMap());
118                    }
119    
120                    map.putAll(_params);
121    
122                    return map;
123            }
124    
125            @Override
126            public Enumeration<String> getParameterNames() {
127                    Set<String> names = new LinkedHashSet<String>();
128    
129                    if (_inherit) {
130                            Enumeration<String> enu = super.getParameterNames();
131    
132                            while (enu.hasMoreElements()) {
133                                    names.add(enu.nextElement());
134                            }
135                    }
136    
137                    names.addAll(_params.keySet());
138    
139                    return Collections.enumeration(names);
140            }
141    
142            @Override
143            public String[] getParameterValues(String name) {
144                    String[] values = _params.get(name);
145    
146                    if (_inherit && (values == null)) {
147                            return super.getParameterValues(name);
148                    }
149    
150                    return values;
151            }
152    
153            public void setParameter(String name, String value) {
154                    _params.put(name, new String[] {value});
155            }
156    
157            public void setParameterValues(String name, String[] values) {
158                    _params.put(name, values);
159            }
160    
161            private boolean _inherit;
162            private Map<String, String[]> _params;
163    
164    }