📄 cmsrequestutil.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsRequestUtil.java,v $
* Date : $Date: 2006/04/28 15:20:52 $
* Version: $Revision: 1.19 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* 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.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.util;
import org.opencms.flex.CmsFlexRequest;
import org.opencms.i18n.CmsEncoder;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.logging.Log;
/**
* Provides utility functions for dealing with values a <code>{@link HttpServletRequest}</code>.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.19 $
*
* @since 6.0.0
*/
public final class CmsRequestUtil {
/** Request attribute that contains the original error code. */
public static final String ATTRIBUTE_ERRORCODE = "org.opencms.util.CmsErrorCode";
/** HTTP Accept-Charset Header for internal requests used during static export. */
public static final String HEADER_ACCEPT_CHARSET = "Accept-Charset";
/** HTTP Accept-Language Header for internal requests used during static export. */
public static final String HEADER_ACCEPT_LANGUAGE = "Accept-Language";
/** HTTP Header "Cache-Control". */
public static final String HEADER_CACHE_CONTROL = "Cache-Control";
/** The "Content-Disposition" http header. */
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
/** The "Content-Type" http header. */
public static final String HEADER_CONTENT_TYPE = "Content-Type";
/** HTTP Header "Expires". */
public static final String HEADER_EXPIRES = "Expires";
/** HTTP Header "If-Modified-Since". */
public static final String HEADER_IF_MODIFIED_SINCE = "If-Modified-Since";
/** The Header that stores the session id (used by OpenCms upload applet). */
public static final String HEADER_JSESSIONID = "JSESSIONID";
/** HTTP Header "Last-Modified". */
public static final String HEADER_LAST_MODIFIED = "Last-Modified";
/** HTTP Header for internal requests used during static export. */
public static final String HEADER_OPENCMS_EXPORT = "OpenCms-Export";
/** HTTP Header "Pragma". */
public static final String HEADER_PRAGMA = "Pragma";
/** HTTP Header "Server". */
public static final String HEADER_SERVER = "Server";
/** HTTP Header "user-agent". */
public static final String HEADER_USER_AGENT = "user-agent";
/** HTTP Header value "max-age=" (for "Cache-Control"). */
public static final String HEADER_VALUE_MAX_AGE = "max-age=";
/** HTTP Header value "must-revalidate" (for "Cache-Control"). */
public static final String HEADER_VALUE_MUST_REVALIDATE = "must-revalidate";
/** HTTP Header value "no-cache" (for "Cache-Control"). */
public static final String HEADER_VALUE_NO_CACHE = "no-cache";
/** HTTP Header "WWW-Authenticate". */
public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
/** Identifier for x-forwarded-for (i.e. proxied) request headers. */
public static final String HEADER_X_FORWARDED_FOR = "x-forwarded-for";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsRequestUtil.class);
/**
* Default constructor (empty), private because this class has only
* static methods.<p>
*/
private CmsRequestUtil() {
// empty
}
/**
* Appends a request parameter to the given URL.<p>
*
* This method takes care about the adding the parameter as an additional
* parameter (appending <code>¶m=value</code>) or as the first parameter
* (appending <code>?param=value</code>).<p>
*
* @param url the URL where to append the parameter to
* @param paramName the paramter name to append
* @param paramValue the parameter value to append
*
* @return the URL with the given parameter appended
*/
public static String appendParameter(String url, String paramName, String paramValue) {
if (CmsStringUtil.isEmpty(url)) {
return null;
}
int pos = url.indexOf('?');
StringBuffer result = new StringBuffer(256);
result.append(url);
if (pos >= 0) {
// url already has parameters
result.append('&');
} else {
// url does not have parameters
result.append('?');
}
result.append(paramName);
result.append('=');
result.append(paramValue);
return result.toString();
}
/**
* Appends a map of request parameters to the given URL.<p>
*
* The map can cointains values of <code>String[]</code> or
* simple <code>String</code> values.<p>
*
* This method takes care about the adding the parameter as an additional
* parameter (appending <code>¶m=value</code>) or as the first parameter
* (appending <code>?param=value</code>).<p>
*
* @param url the URL where to append the parameter to
* @param params the paramters to append
* @param encode if <code>true</code>, the parameter values are encoded before they are appended
*
* @return the URL with the given parameter appended
*/
public static String appendParameters(String url, Map params, boolean encode) {
if (CmsStringUtil.isEmpty(url)) {
return null;
}
if ((params == null) || params.isEmpty()) {
return url;
}
int pos = url.indexOf('?');
StringBuffer result = new StringBuffer(256);
result.append(url);
if (pos >= 0) {
// url already has parameters
result.append('&');
} else {
// url does not have parameters
result.append('?');
}
// ensure all values are of type String[]
Map newParams = createParameterMap(params);
Iterator i = newParams.keySet().iterator();
while (i.hasNext()) {
String key = (String)i.next();
Object value = newParams.get(key);
String[] values = (String[])value;
for (int j = 0; j < values.length; j++) {
String strValue = values[j];
if (encode) {
strValue = CmsEncoder.encode(strValue);
}
result.append(key);
result.append('=');
result.append(strValue);
if ((j + 1) < values.length) {
result.append('&');
}
}
if (i.hasNext()) {
result.append('&');
}
}
return result.toString();
}
/**
* Creates a valid request parameter map from the given map,
* most notably changing the values form <code>String</code>
* to <code>String[]</code> if required.<p>
*
* If the given parameter map is <code>null</code>, then <code>null</code> is returned.<p>
*
* @param params the map of parameters to create a parameter map from
* @return the created parameter map, all values will be instances of <code>String[]</code>
*/
public static Map createParameterMap(Map params) {
if (params == null) {
return null;
}
HashMap result = new HashMap();
Iterator i = params.keySet().iterator();
while (i.hasNext()) {
String key = i.next().toString();
Object values = params.get(key);
if (values instanceof String[]) {
result.put(key, values);
} else {
result.put(key, new String[] {values.toString()});
}
}
return result;
}
/**
* Parses the parameters of the given request query part and creaes a parameter map out of them.<p>
*
* Please note: This does not parse a full request URI/URL, only the query part that
* starts after the "?". For example, in the URI <code>/system/index.html?a=b&c=d</code>,
* the query part is <code>a=b&c=d</code>.<p>
*
* If the given String is empty, an empty map is returned.<p>
*
* @param query the query to parse
* @return the parameter map created from the query
*/
public static Map createParameterMap(String query) {
if (CmsStringUtil.isEmpty(query)) {
// empty query
return new HashMap();
}
if (query.charAt(0) == '?') {
// remove leading '?' if required
query = query.substring(1);
}
HashMap parameters = new HashMap();
// cut along the different parameters
String[] params = CmsStringUtil.splitAsArray(query, '&');
for (int i = 0; i < params.length; i++) {
String key = null;
String value = null;
// get key and value, separated by a '='
int pos = params[i].indexOf('=');
if (pos > 0) {
key = params[i].substring(0, pos);
value = params[i].substring(pos + 1);
} else if (pos < 0) {
key = params[i];
value = "";
}
// now make sure the values are of type String[]
if (key != null) {
String[] values = (String[])parameters.get(key);
if (values == null) {
// this is the first value, create new array
values = new String[] {value};
} else {
// append to the existing value array
String[] copy = new String[values.length + 1];
System.arraycopy(values, 0, copy, 0, values.length);
copy[copy.length - 1] = value;
values = copy;
}
parameters.put(key, values);
}
}
return parameters;
}
/**
* Returns all parameters of the given request
* as a request parameter URL String, that is in the form <code>key1=value1&key2=value2</code> etc.
*
* The result will be encoded using the <code>{@link CmsEncoder#encode(String)}</code> function.<p>
*
* @param req the request to read the parameters from
*
* @return all initialized parameters of the given request as request parameter URL String
*/
public static String encodeParams(HttpServletRequest req) {
StringBuffer result = new StringBuffer(512);
Map params = req.getParameterMap();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -