001    /**
002     * Copyright (c) 2000-present 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.servlet;
016    
017    import com.liferay.portal.kernel.model.Portlet;
018    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.PortalUtil;
021    
022    import java.util.Map;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author Adolfo P??rez
028     */
029    public class DynamicServletRequestUtil {
030    
031            public static HttpServletRequest createDynamicServletRequest(
032                    HttpServletRequest httpServletRequest, Portlet portlet,
033                    Map<String, String[]> parameterMap, boolean mergeParameters) {
034    
035                    DynamicServletRequest dynamicServletRequest = null;
036    
037                    if (portlet.isPrivateRequestAttributes()) {
038                            String portletNamespace = PortalUtil.getPortletNamespace(
039                                    portlet.getPortletName());
040    
041                            dynamicServletRequest = new NamespaceServletRequest(
042                                    httpServletRequest, portletNamespace, portletNamespace);
043                    }
044                    else {
045                            dynamicServletRequest = new DynamicServletRequest(
046                                    httpServletRequest);
047                    }
048    
049                    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
050                            String name = entry.getKey();
051    
052                            String[] values = entry.getValue();
053    
054                            String[] oldValues = dynamicServletRequest.getParameterValues(name);
055    
056                            if (mergeParameters && (oldValues != null)) {
057                                    values = ArrayUtil.append(values, oldValues);
058                            }
059    
060                            dynamicServletRequest.setParameterValues(name, values);
061                    }
062    
063                    return dynamicServletRequest;
064            }
065    
066    }