com.liferay.portal.kernel.servlet.DynamicServletRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.kernel Show documentation
Show all versions of com.liferay.portal.kernel Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.kernel.servlet;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* @author Brian Wing Shun Chan
* @author Shuyang Zhou
* @author Sampsa Sohlman
*/
public class DynamicServletRequest extends HttpServletRequestWrapper {
public static final String DYNAMIC_QUERY_STRING = "DYNAMIC_QUERY_STRING";
public static HttpServletRequest addQueryString(
HttpServletRequest request, Map parameterMap,
String queryString) {
return addQueryString(request, parameterMap, queryString, true);
}
public static HttpServletRequest addQueryString(
HttpServletRequest request, Map parameterMap,
String queryString, boolean inherit) {
String[] parameters = StringUtil.split(queryString, CharPool.AMPERSAND);
if (parameters.length == 0) {
return request;
}
parameterMap = new HashMap<>(parameterMap);
for (String parameter : parameters) {
String[] parameterParts = StringUtil.split(
parameter, CharPool.EQUAL);
String name = parameterParts[0];
String value = StringPool.BLANK;
if (parameterParts.length == 2) {
value = parameterParts[1];
}
String[] values = parameterMap.get(name);
if (values == null) {
parameterMap.put(name, new String[] {value});
}
else {
String[] newValues = new String[values.length + 1];
System.arraycopy(values, 0, newValues, 0, values.length);
newValues[newValues.length - 1] = value;
parameterMap.put(name, newValues);
}
}
request = new DynamicServletRequest(request, parameterMap, inherit);
request.setAttribute(DYNAMIC_QUERY_STRING, queryString);
return request;
}
public static HttpServletRequest addQueryString(
HttpServletRequest request, String queryString) {
return addQueryString(
request, new HashMap(), queryString, true);
}
public static HttpServletRequest addQueryString(
HttpServletRequest request, String queryString, boolean inherit) {
return addQueryString(
request, new HashMap(), queryString, inherit);
}
public DynamicServletRequest(HttpServletRequest request) {
this(request, null, true);
}
public DynamicServletRequest(HttpServletRequest request, boolean inherit) {
this(request, null, inherit);
}
public DynamicServletRequest(
HttpServletRequest request, Map params) {
this(request, params, true);
}
public DynamicServletRequest(
HttpServletRequest request, Map params,
boolean inherit) {
super(request);
_params = new HashMap<>();
_inherit = inherit;
if (params != null) {
_params.putAll(params);
}
if (_inherit && (request instanceof DynamicServletRequest)) {
DynamicServletRequest dynamicRequest =
(DynamicServletRequest)request;
dynamicRequest.injectInto(this);
params = dynamicRequest.getDynamicParameterMap();
for (Map.Entry entry : params.entrySet()) {
String name = entry.getKey();
String[] oldValues = entry.getValue();
String[] curValues = _params.get(name);
if (curValues == null) {
_params.put(name, oldValues);
}
else {
String[] newValues = ArrayUtil.append(oldValues, curValues);
_params.put(name, newValues);
}
}
}
}
public void appendParameter(String name, String value) {
String[] values = _params.get(name);
if (values == null) {
values = new String[] {value};
}
else {
String[] newValues = new String[values.length + 1];
System.arraycopy(values, 0, newValues, 0, values.length);
newValues[newValues.length - 1] = value;
values = newValues;
}
_params.put(name, values);
}
public Map getDynamicParameterMap() {
return _params;
}
@Override
public String getParameter(String name) {
String[] values = _params.get(name);
if (_inherit && (values == null)) {
return super.getParameter(name);
}
if (ArrayUtil.isNotEmpty(values)) {
return values[0];
}
else {
return null;
}
}
@Override
public Map getParameterMap() {
Map map = new HashMap<>();
if (_inherit) {
map.putAll(super.getParameterMap());
}
map.putAll(_params);
return map;
}
@Override
public Enumeration getParameterNames() {
Set names = new LinkedHashSet<>();
if (_inherit) {
Enumeration enu = super.getParameterNames();
while (enu.hasMoreElements()) {
names.add(enu.nextElement());
}
}
names.addAll(_params.keySet());
return Collections.enumeration(names);
}
@Override
public String[] getParameterValues(String name) {
String[] values = _params.get(name);
if (_inherit && (values == null)) {
return super.getParameterValues(name);
}
return values;
}
public void setParameter(String name, String value) {
_params.put(name, new String[] {value});
}
public void setParameterValues(String name, String[] values) {
_params.put(name, values);
}
protected void injectInto(DynamicServletRequest dynamicServletRequest) {
dynamicServletRequest.setRequest(getRequest());
}
private final boolean _inherit;
private final Map _params;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy