|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.opencms.util.CmsRequestUtil
public final class CmsRequestUtil
Provides utility functions for dealing with values a
.HttpServletRequest
Field Summary | |
---|---|
static String |
ATTRIBUTE_ERRORCODE
Request attribute that contains the original error code. |
static String |
HEADER_ACCEPT
HTTP Accept Header for the cms:device-tag. |
static String |
HEADER_ACCEPT_CHARSET
HTTP Accept-Charset Header for internal requests used during static export. |
static String |
HEADER_ACCEPT_LANGUAGE
HTTP Accept-Language Header for internal requests used during static export. |
static String |
HEADER_CACHE_CONTROL
HTTP Header "Cache-Control". |
static String |
HEADER_CONNECTION
HTTP Header "Connection". |
static String |
HEADER_CONTENT_DISPOSITION
The "Content-Disposition" http header. |
static String |
HEADER_CONTENT_TYPE
The "Content-Type" http header. |
static String |
HEADER_EXPIRES
HTTP Header "Expires". |
static String |
HEADER_IF_MODIFIED_SINCE
HTTP Header "If-Modified-Since". |
static String |
HEADER_JSESSIONID
The Header that stores the session id (used by OpenCms upload applet). |
static String |
HEADER_LAST_MODIFIED
HTTP Header "Last-Modified". |
static String |
HEADER_LOCATION
HTTP Header "Location". |
static String |
HEADER_OPENCMS_EXPORT
HTTP Header for internal requests used during static export. |
static String |
HEADER_PRAGMA
HTTP Header "Pragma". |
static String |
HEADER_SERVER
HTTP Header "Server". |
static String |
HEADER_USER_AGENT
HTTP Header "user-agent". |
static String |
HEADER_VALUE_MAX_AGE
HTTP Header value "max-age=" (for "Cache-Control"). |
static String |
HEADER_VALUE_MUST_REVALIDATE
HTTP Header value "must-revalidate" (for "Cache-Control"). |
static String |
HEADER_VALUE_NO_CACHE
HTTP Header value "no-cache" (for "Cache-Control"). |
static String |
HEADER_WWW_AUTHENTICATE
HTTP Header "WWW-Authenticate". |
static String |
HEADER_X_FORWARDED_FOR
Identifier for x-forwarded-for (i.e. |
static String |
PARAMETER_ASSIGNMENT
Assignment char between parameter name and values. |
static String |
PARAMETER_DELIMITER
Delimiter char between parameters. |
static String |
URL_DELIMITER
Delimiter char between url and query. |
Method Summary | |
---|---|
static String |
appendParameter(String url,
String paramName,
String paramValue)
Appends a request parameter to the given URL. |
static String |
appendParameters(String url,
Map<String,String[]> params,
boolean encode)
Appends a map of request parameters to the given URL. |
static Map<String,String[]> |
createParameterMap(Map<String,?> params)
Creates a valid request parameter map from the given map, most notably changing the values form String
to String[] if required. |
static Map<String,String[]> |
createParameterMap(String query)
Parses the parameters of the given request query part and creates a parameter map out of them. |
static String |
encodeParams(javax.servlet.http.HttpServletRequest req)
Returns all parameters of the given request as a request parameter URL String, that is in the form key1=value1&key2=value2 etc. |
static String |
encodeParamsWithUri(String uri,
javax.servlet.http.HttpServletRequest req)
Encodes the given URI, with all parameters from the given request appended. |
static void |
forwardRequest(String target,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
Forwards the response to the given target, which may contain parameters appended like for example ?a=b&c=d . |
static void |
forwardRequest(String target,
Map<String,String[]> params,
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
Forwards the response to the given target, with the provided parameter map. |
static Map<String,Object> |
getAtrributeMap(javax.servlet.ServletRequest req)
Returns a map with all request attributes. |
static String |
getCookieValue(CmsJspActionElement jsp,
String name)
Returns the value of the cookie with the given name. |
static JSONObject |
getJsonParameterMap(Map<String,String[]> params)
Converts the given parameter map into an JSON object. |
static String |
getNotEmptyDecodedParameter(javax.servlet.http.HttpServletRequest request,
String paramName)
Reads value from the request parameters, will return null if the value is not available or only white space. |
static String |
getNotEmptyParameter(javax.servlet.http.HttpServletRequest request,
String paramName)
Reads value from the request parameters, will return null if the value is not available or only white space. |
static Map<String,String[]> |
getParameterMapFromJSON(JSONObject params)
Converts the given JSON object into a valid parameter map. |
static String |
getRequestLink(String url)
Returns the link without parameters from a String that is formatted for a GET request. |
static Object |
getSessionValue(javax.servlet.http.HttpServletRequest request,
String key)
Reads an object from the session of the given HTTP request. |
static List<org.apache.commons.fileupload.FileItem> |
readMultipartFileItems(javax.servlet.http.HttpServletRequest request)
Parses a request of the form multipart/form-data . |
static Map<String,String[]> |
readParameterMapFromMultiPart(String encoding,
List<org.apache.commons.fileupload.FileItem> multiPartFileItems)
Creates a "standard" request parameter map from the values of a multipart/form-data request. |
static void |
redirectPermanently(CmsJspActionElement jsp,
String target)
Redirects the response to the target link using a "301 - Moved Permanently" header. |
static void |
redirectRequestSecure(CmsJspActionElement jsp,
String target)
Redirects the response to the target link. |
static void |
removeSessionValue(javax.servlet.http.HttpServletRequest request,
String key)
Removes an object from the session of the given http request. |
static void |
setCookieValue(CmsJspActionElement jsp,
String name,
String value)
Sets the value of a specific cookie. |
static void |
setNoCacheHeaders(javax.servlet.http.HttpServletResponse res)
Sets headers to the given response to prevent client side caching. |
static void |
setSessionValue(javax.servlet.http.HttpServletRequest request,
String key,
Object value)
Adds an object to the session of the given HTTP request. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String ATTRIBUTE_ERRORCODE
public static final String HEADER_ACCEPT
public static final String HEADER_ACCEPT_CHARSET
public static final String HEADER_ACCEPT_LANGUAGE
public static final String HEADER_CACHE_CONTROL
public static final String HEADER_CONNECTION
public static final String HEADER_CONTENT_DISPOSITION
public static final String HEADER_CONTENT_TYPE
public static final String HEADER_EXPIRES
public static final String HEADER_IF_MODIFIED_SINCE
public static final String HEADER_JSESSIONID
public static final String HEADER_LAST_MODIFIED
public static final String HEADER_LOCATION
public static final String HEADER_OPENCMS_EXPORT
public static final String HEADER_PRAGMA
public static final String HEADER_SERVER
public static final String HEADER_USER_AGENT
public static final String HEADER_VALUE_MAX_AGE
public static final String HEADER_VALUE_MUST_REVALIDATE
public static final String HEADER_VALUE_NO_CACHE
public static final String HEADER_WWW_AUTHENTICATE
public static final String HEADER_X_FORWARDED_FOR
public static final String PARAMETER_ASSIGNMENT
public static final String PARAMETER_DELIMITER
public static final String URL_DELIMITER
Method Detail |
---|
public static String appendParameter(String url, String paramName, String paramValue)
This method takes care about the adding the parameter as an additional
parameter (appending ¶m=value
) or as the first parameter
(appending ?param=value
).
url
- the URL where to append the parameter toparamName
- the paramter name to appendparamValue
- the parameter value to append
public static String appendParameters(String url, Map<String,String[]> params, boolean encode)
The map can contains values of String[]
or
simple String
values.
This method takes care about the adding the parameter as an additional
parameter (appending ¶m=value
) or as the first parameter
(appending ?param=value
).
url
- the URL where to append the parameter toparams
- the parameters to appendencode
- if true
, the parameter values are encoded before they are appended
public static Map<String,String[]> createParameterMap(Map<String,?> params)
String
to String[]
if required.
If the given parameter map is null
, then null
is returned.
params
- the map of parameters to create a parameter map from
String[]
public static Map<String,String[]> createParameterMap(String query)
Please note: This does not parse a full request URI/URL, only the query part that
starts after the "?". For example, in the URI /system/index.html?a=b&c=d
,
the query part is a=b&c=d
.
If the given String is empty, an empty map is returned.
query
- the query to parse
public static String encodeParams(javax.servlet.http.HttpServletRequest req)
key1=value1&key2=value2
etc.
The result will be encoded using the CmsEncoder.encode(String)
function.
req
- the request to read the parameters from
public static String encodeParamsWithUri(String uri, javax.servlet.http.HttpServletRequest req)
The result will be encoded using the
function.CmsEncoder.encode(String)
req
- the request where to read the parameters fromuri
- the URI to encode
public static void forwardRequest(String target, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws IOException, javax.servlet.ServletException
?a=b&c=d
.
Please note: If possible, use
where the parameters are passed as a map, since the parsing of the parameters may introduce issues with encoding
and is in general much less effective.forwardRequest(String, Map, HttpServletRequest, HttpServletResponse)
The parsing of parameters will likely fail for "large values" (e.g. full blown web forms with <textarea> elements etc. Use this method only if you know that the target will just contain up to 3 parameters which are relatively short and have no encoding or line break issues.
target
- the target to forward to (may contain parameters like ?a=b&c=d
)req
- the request to forwardres
- the response to forward
IOException
- in case the forwarding fails
javax.servlet.ServletException
- in case the forwarding failspublic static void forwardRequest(String target, Map<String,String[]> params, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws IOException, javax.servlet.ServletException
The target URI must NOT have parameters appended like for example ?a=b&c=d
.
The values in the provided map must be of type String[]
. If required, use
before calling this method to make sure
all values are actually of the required array type.createParameterMap(Map)
target
- the target to forward to (may NOT contain parameters like ?a=b&c=d
)params
- the parameter map (the values must be of type String[]
req
- the request to forwardres
- the response to forward
IOException
- in case the forwarding fails
javax.servlet.ServletException
- in case the forwarding failspublic static Map<String,Object> getAtrributeMap(javax.servlet.ServletRequest req)
req
- the request
public static String getCookieValue(CmsJspActionElement jsp, String name)
jsp
- the CmsJspActionElement to usename
- the name of the cookie
public static JSONObject getJsonParameterMap(Map<String,String[]> params)
params
- the parameters map to convert
public static String getNotEmptyDecodedParameter(javax.servlet.http.HttpServletRequest request, String paramName)
null
if the value is not available or only white space.
The value of the request will also be decoded using
and also trimmed using CmsEncoder.decode(String)
.String.trim()
request
- the request to read the parameter fromparamName
- the parameter name to read
public static String getNotEmptyParameter(javax.servlet.http.HttpServletRequest request, String paramName)
null
if the value is not available or only white space.
request
- the request to read the parameter fromparamName
- the parameter name to read
public static Map<String,String[]> getParameterMapFromJSON(JSONObject params)
params
- the JSON object to convert
public static String getRequestLink(String url)
url
- the URL to remove the parameters from
public static Object getSessionValue(javax.servlet.http.HttpServletRequest request, String key)
A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.
Will return null
if no corresponding object is found in the session.
request
- the request to get the session fromkey
- the key of the object to read from the session
null
public static List<org.apache.commons.fileupload.FileItem> readMultipartFileItems(javax.servlet.http.HttpServletRequest request)
multipart/form-data
.
The result list will contain items of type FileItem
.
If the request is not of type multipart/form-data
, then null
is returned.
request
- the HTTP servlet request to parse
FileItem
extracted from the multipart request,
or null
if the request was not of type multipart/form-data
public static Map<String,String[]> readParameterMapFromMultiPart(String encoding, List<org.apache.commons.fileupload.FileItem> multiPartFileItems)
multipart/form-data
request.
encoding
- the encoding to use when creating the valuesmultiPartFileItems
- the list of parsed multi part file items
readMultipartFileItems(HttpServletRequest)
public static void redirectPermanently(CmsJspActionElement jsp, String target)
This implementation will work only on JSP pages in OpenCms that use the default JSP loader implementation.
jsp
- the OpenCms JSP contexttarget
- the target linkpublic static void redirectRequestSecure(CmsJspActionElement jsp, String target) throws IOException
Use this method instead of HttpServletResponse.sendRedirect(java.lang.String)
to avoid relative links with secure sites (and issues with apache).
jsp
- the OpenCms JSP contexttarget
- the target link
IOException
- if something goes wrong during redirectionpublic static void removeSessionValue(javax.servlet.http.HttpServletRequest request, String key)
A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.
request
- the request to get the session fromkey
- the key of the object to be removed from the sessionpublic static void setCookieValue(CmsJspActionElement jsp, String name, String value)
If no cookie exists with the value, a new cookie will be created.
jsp
- the CmsJspActionElement to usename
- the name of the cookievalue
- the value of the cookiepublic static void setNoCacheHeaders(javax.servlet.http.HttpServletResponse res)
The following headers are set:
Cache-Control: max-age=0
Cache-Control: must-revalidate
Pragma: no-cache
res
- the request where to set the no-cache headerspublic static void setSessionValue(javax.servlet.http.HttpServletRequest request, String key, Object value)
A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.
request
- the request to get the session fromkey
- the key of the object to be stored in the sessionvalue
- the object to be stored in the session
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |