Class CmsRequestUtil
HttpServletRequest.- Since:
- 6.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringRequest attribute that contains the original error code.static booleanFlag to enable / disable backlink checks.static final StringHTTP Accept Header for the cms:device-tag.static final StringHTTP Accept-Charset Header for internal requests used during static export.static final StringHTTP Accept-Language Header for internal requests used during static export.static final StringHTTP Header "Cache-Control".static final StringHTTP Header "Connection".static final StringThe "Content-Disposition" http header.static final StringThe "Content-Type" http header.static final StringHTTP Header "Expires".static final StringHTTP Header "If-Modified-Since".static final StringThe Header that stores the session id (used by OpenCms upload applet).static final StringHTTP Header "Last-Modified".static final StringHTTP Header "Location".static final StringHTTP Header for internal requests used during static export.static final StringHTTP Header "Pragma".static final StringHTTP Header "Server".static final StringHTTP Header "user-agent".static final StringHTTP Header value "max-age=" (for "Cache-Control").static final StringHTTP Header value "must-revalidate" (for "Cache-Control").static final StringHTTP Header value "no-cache" (for "Cache-Control").static final StringHTTP Header value "no-store" (for "Cache-Control").static final StringHTTP Header "WWW-Authenticate".static final StringIdentifier for x-forwarded-for (i.e.static final StringAssignment char between parameter name and values.static final StringDelimiter char between parameters.static final StringDelimiter char between url and query. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringappendParameter(String url, String paramName, String paramValue) Appends a request parameter to the given URL.static StringappendParameters(String url, Map<String, String[]> params, boolean encode) Appends a map of request parameters to the given URL.static booleancheckBacklink(String backlink, javax.servlet.http.HttpServletRequest optionalRequest) Checks that the given link is a valid backlink for editors.createParameterMap(String query) Parses the parameters of the given request query part and creates a parameter map out of them.createParameterMap(String query, boolean decodeParameters, String encoding) Parses the parameters of the given request query part, optionally decodes them, and creates a parameter map out of them.createParameterMap(Map<String, ?> params) Creates a valid request parameter map from the given map, most notably changing the values formStringtoString[]if required.static voiddisableCrossSiteFrameEmbedding(javax.servlet.http.HttpServletResponse response) Sets HTTP response headers to disable embedding in iframes on different domains.static StringencodeParams(javax.servlet.http.HttpServletRequest req) Returns all parameters of the given request as a request parameter URL String, that is in the formkey1=value1invalid input: '&key2'=value2etc.static StringencodeParamsWithUri(String uri, javax.servlet.http.HttpServletRequest req) Encodes the given URI, with all parameters from the given request appended.static voidforwardRequest(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 voidforwardRequest(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.getAtrributeMap(javax.servlet.ServletRequest req) Exactly like getAttributeMap, but incorrectly spelled.getAttributeMap(javax.servlet.ServletRequest req) Returns a map with all request attributes.static StringgetCookieValue(javax.servlet.http.Cookie[] cookies, String name) Gets the value of a specific cookie from an array of cookies.static StringgetCookieValue(CmsJspActionElement jsp, String name) Returns the value of the cookie with the given name.static JSONObjectgetJsonParameterMap(Map<String, String[]> params) Converts the given parameter map into an JSON object.static StringgetNotEmptyDecodedParameter(javax.servlet.http.HttpServletRequest request, String paramName) Reads value from the request parameters, will returnnullif the value is not available or only white space.static StringgetNotEmptyParameter(javax.servlet.http.HttpServletRequest request, String paramName) Reads value from the request parameters, will returnnullif the value is not available or only white space.getParameterMapFromJSON(JSONObject params) Converts the given JSON object into a valid parameter map.getParameters(URI uri) Parses parameter map from the given URI.getParametersFromRawQuery(String rawQuery) Parses the parameter map from a raw query string.static StringgetRequestLink(String url) Returns the link without parameters from a String that is formatted for a GET request.static ObjectgetSessionValue(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 formmultipart/form-data.static List<org.apache.commons.fileupload.FileItem> readMultipartFileItems(javax.servlet.http.HttpServletRequest request, String tempFolderPath) Parses a request of the formmultipart/form-data.readParameterMapFromMultiPart(String encoding, List<org.apache.commons.fileupload.FileItem> multiPartFileItems) Creates a "standard" request parameter map from the values of amultipart/form-datarequest.static voidredirectPermanently(CmsJspActionElement jsp, String target) Redirects the response to the target link using a "301 - Moved Permanently" header.static voidredirectRequestSecure(CmsJspActionElement jsp, String target) Redirects the response to the target link.static voidremoveSessionValue(javax.servlet.http.HttpServletRequest request, String key) Removes an object from the session of the given http request.static voidsetCookieValue(CmsJspActionElement jsp, String name, String value) Sets the value of a specific cookie.static voidsetNoCacheHeaders(javax.servlet.http.HttpServletResponse res) Sets headers to the given response to prevent client side caching.static voidsetSessionValue(javax.servlet.http.HttpServletRequest request, String key, Object value) Adds an object to the session of the given HTTP request.
-
Field Details
-
ATTRIBUTE_ERRORCODE
Request attribute that contains the original error code.- See Also:
-
HEADER_ACCEPT
HTTP Accept Header for the cms:device-tag.- See Also:
-
HEADER_ACCEPT_CHARSET
HTTP Accept-Charset Header for internal requests used during static export.- See Also:
-
HEADER_ACCEPT_LANGUAGE
HTTP Accept-Language Header for internal requests used during static export.- See Also:
-
HEADER_CACHE_CONTROL
HTTP Header "Cache-Control".- See Also:
-
HEADER_CONNECTION
HTTP Header "Connection".- See Also:
-
HEADER_CONTENT_DISPOSITION
The "Content-Disposition" http header.- See Also:
-
HEADER_CONTENT_TYPE
The "Content-Type" http header.- See Also:
-
HEADER_EXPIRES
HTTP Header "Expires".- See Also:
-
HEADER_IF_MODIFIED_SINCE
HTTP Header "If-Modified-Since".- See Also:
-
HEADER_JSESSIONID
The Header that stores the session id (used by OpenCms upload applet).- See Also:
-
HEADER_LAST_MODIFIED
HTTP Header "Last-Modified".- See Also:
-
HEADER_LOCATION
HTTP Header "Location".- See Also:
-
HEADER_OPENCMS_EXPORT
HTTP Header for internal requests used during static export.- See Also:
-
HEADER_PRAGMA
HTTP Header "Pragma".- See Also:
-
HEADER_SERVER
HTTP Header "Server".- See Also:
-
HEADER_USER_AGENT
HTTP Header "user-agent".- See Also:
-
HEADER_VALUE_MAX_AGE
HTTP Header value "max-age=" (for "Cache-Control").- See Also:
-
HEADER_VALUE_MUST_REVALIDATE
HTTP Header value "must-revalidate" (for "Cache-Control").- See Also:
-
HEADER_VALUE_NO_CACHE
HTTP Header value "no-cache" (for "Cache-Control").- See Also:
-
HEADER_VALUE_NO_STORE
HTTP Header value "no-store" (for "Cache-Control").- See Also:
-
HEADER_WWW_AUTHENTICATE
HTTP Header "WWW-Authenticate".- See Also:
-
HEADER_X_FORWARDED_FOR
Identifier for x-forwarded-for (i.e. proxied) request headers.- See Also:
-
PARAMETER_ASSIGNMENT
Assignment char between parameter name and values.- See Also:
-
PARAMETER_DELIMITER
Delimiter char between parameters.- See Also:
-
URL_DELIMITER
Delimiter char between url and query.- See Also:
-
backlinkCheckEnabled
Flag to enable / disable backlink checks.
-
-
Method Details
-
appendParameter
Appends a request parameter to the given URL.This method takes care about the adding the parameter as an additional parameter (appending
invalid input: '¶m'=value) or as the first parameter (appending?param=value).- Parameters:
url- the URL where to append the parameter toparamName- the paramter name to appendparamValue- the parameter value to append- Returns:
- the URL with the given parameter appended
-
appendParameters
Appends a map of request parameters to the given URL.The map can contains values of
String[]or simpleStringvalues.This method takes care about the adding the parameter as an additional parameter (appending
invalid input: '¶m'=value) or as the first parameter (appending?param=value).- Parameters:
url- the URL where to append the parameter toparams- the parameters to appendencode- iftrue, the parameter values are encoded before they are appended- Returns:
- the URL with the given parameter appended
-
checkBacklink
public static boolean checkBacklink(String backlink, javax.servlet.http.HttpServletRequest optionalRequest) Checks that the given link is a valid backlink for editors.This means that the link is either just a path, or starts with a scheme/domain/port that is either registered in the site configuration, or is the same as that of the current request.
- Parameters:
backlink- the link to checkoptionalRequest- the current request - may be null if no check against the current request is desired- Returns:
- true if the link is a valid backlink
-
createParameterMap
Creates a valid request parameter map from the given map, most notably changing the values formStringtoString[]if required.If the given parameter map is
null, thennullis returned.- Parameters:
params- the map of parameters to create a parameter map from- Returns:
- the created parameter map, all values will be instances of
String[]
-
createParameterMap
Parses the parameters of the given request query part and creates a parameter map out of them.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 isa=b&c=d.If the given String is empty, an empty map is returned.
- Parameters:
query- the query to parse- Returns:
- the parameter map created from the query
-
createParameterMap
public static Map<String,String[]> createParameterMap(String query, boolean decodeParameters, String encoding) Parses the parameters of the given request query part, optionally decodes them, and creates a parameter map out of them.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 isa=b&c=d.If the given String is empty, an empty map is returned.
- Parameters:
query- the query to parsedecodeParameters- a flag, indicating if the parameters should be decoded.encoding- the character encoding used while decoding. Ifnull, the default character encoding is used.- Returns:
- the parameter map created from the query
-
disableCrossSiteFrameEmbedding
Sets HTTP response headers to disable embedding in iframes on different domains.- Parameters:
response- the response on which to set the HTTP headers
-
encodeParams
Returns all parameters of the given request as a request parameter URL String, that is in the formkey1=value1invalid input: '&key2'=value2etc. The result will be encoded using thefunction.CmsEncoder.encode(String)- Parameters:
req- the request to read the parameters from- Returns:
- all initialized parameters of the given request as request parameter URL String
-
encodeParamsWithUri
Encodes the given URI, with all parameters from the given request appended.The result will be encoded using the
function.CmsEncoder.encode(String)- Parameters:
uri- the URI to encodereq- the request where to read the parameters from- Returns:
- the encoded URI, with all parameters from the given request appended
-
forwardRequest
public static void forwardRequest(String target, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws IOException, javax.servlet.ServletException Forwards the response to the given target, which may contain parameters appended like for example?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.
- Parameters:
target- the target to forward to (may contain parameters like?a=b&c=d)req- the request to forwardres- the response to forward- Throws:
IOException- in case the forwarding failsjavax.servlet.ServletException- in case the forwarding fails
-
forwardRequest
public static void forwardRequest(String target, Map<String, String[]> params, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws IOException, javax.servlet.ServletExceptionForwards the response to the given target, with the provided parameter map.The target URI must NOT have parameters appended like for example
?a=b&c=d. The values in the provided map must be of typeString[]. If required, usebefore calling this method to make sure all values are actually of the required array type.createParameterMap(Map)- Parameters:
target- the target to forward to (may NOT contain parameters like?a=b&c=d)params- the parameter map (the values must be of typeString[]req- the request to forwardres- the response to forward- Throws:
IOException- in case the forwarding failsjavax.servlet.ServletException- in case the forwarding fails
-
getAtrributeMap
Exactly like getAttributeMap, but incorrectly spelled.Kept for backward compatibility.
- Parameters:
req- the request- Returns:
- the attribute map
-
getAttributeMap
Returns a map with all request attributes.- Parameters:
req- the request- Returns:
- the attribute map
-
getCookieValue
Returns the value of the cookie with the given name.- Parameters:
jsp- the CmsJspActionElement to usename- the name of the cookie- Returns:
- the value of the cookie with the given name or null, if no cookie exists with the name
-
getCookieValue
Gets the value of a specific cookie from an array of cookies.- Parameters:
cookies- the cookie arrayname- the name of the cookie we want- Returns:
- the cookie value, or null if cookie with the given name wasn't found
-
getJsonParameterMap
Converts the given parameter map into an JSON object.- Parameters:
params- the parameters map to convert- Returns:
- the JSON representation of the given parameter map
-
getNotEmptyDecodedParameter
public static String getNotEmptyDecodedParameter(javax.servlet.http.HttpServletRequest request, String paramName) Reads value from the request parameters, will returnnullif the value is not available or only white space.The value of the request will also be decoded using
and also trimmed usingCmsEncoder.decode(String).String.trim()- Parameters:
request- the request to read the parameter fromparamName- the parameter name to read- Returns:
- the request parameter value for the given parameter
-
getNotEmptyParameter
public static String getNotEmptyParameter(javax.servlet.http.HttpServletRequest request, String paramName) Reads value from the request parameters, will returnnullif the value is not available or only white space.- Parameters:
request- the request to read the parameter fromparamName- the parameter name to read- Returns:
- the request parameter value for the given parameter
-
getParameterMapFromJSON
Converts the given JSON object into a valid parameter map.- Parameters:
params- the JSON object to convert- Returns:
- the parameter map from the given JSON object
-
getParameters
Parses parameter map from the given URI.- Parameters:
uri- the URI- Returns:
- the parameter map
-
getParametersFromRawQuery
public static com.google.common.collect.Multimap<String,String> getParametersFromRawQuery(String rawQuery) Parses the parameter map from a raw query string.- Parameters:
rawQuery- the raw query string- Returns:
- the parameter map
-
getRequestLink
Returns the link without parameters from a String that is formatted for a GET request.- Parameters:
url- the URL to remove the parameters from- Returns:
- the URL without any parameters
-
getSessionValue
Reads an object from the session of the given HTTP request.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
nullif no corresponding object is found in the session.- Parameters:
request- the request to get the session fromkey- the key of the object to read from the session- Returns:
- the object received form the session, or
null
-
readMultipartFileItems
public static List<org.apache.commons.fileupload.FileItem> readMultipartFileItems(javax.servlet.http.HttpServletRequest request) Parses a request of the formmultipart/form-data. The result list will contain items of type. If the request is not of typeFileItemmultipart/form-data, thennullis returned.- Parameters:
request- the HTTP servlet request to parse- Returns:
- the list of
extracted from the multipart request, orFileItemnullif the request was not of typemultipart/form-data
-
readMultipartFileItems
public static List<org.apache.commons.fileupload.FileItem> readMultipartFileItems(javax.servlet.http.HttpServletRequest request, String tempFolderPath) Parses a request of the formmultipart/form-data. The result list will contain items of type. If the request is not of typeFileItemmultipart/form-data, thennullis returned.- Parameters:
request- the HTTP servlet request to parsetempFolderPath- the real file system path to the temp file folder- Returns:
- the list of
extracted from the multipart request, orFileItemnullif the request was not of typemultipart/form-data
-
readParameterMapFromMultiPart
public static Map<String,String[]> readParameterMapFromMultiPart(String encoding, List<org.apache.commons.fileupload.FileItem> multiPartFileItems) Creates a "standard" request parameter map from the values of amultipart/form-datarequest.- Parameters:
encoding- the encoding to use when creating the valuesmultiPartFileItems- the list of parsed multi part file items- Returns:
- a map containing all non-file request parameters
- See Also:
-
redirectPermanently
Redirects the response to the target link using a "301 - Moved Permanently" header.This implementation will work only on JSP pages in OpenCms that use the default JSP loader implementation.
- Parameters:
jsp- the OpenCms JSP contexttarget- the target link
-
redirectRequestSecure
Redirects the response to the target link.Use this method instead of
HttpServletResponse.sendRedirect(java.lang.String)to avoid relative links with secure sites (and issues with apache).- Parameters:
jsp- the OpenCms JSP contexttarget- the target link- Throws:
IOException- if something goes wrong during redirection
-
removeSessionValue
Removes an object from the session of the given http request.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.
- Parameters:
request- the request to get the session fromkey- the key of the object to be removed from the session
-
setCookieValue
Sets the value of a specific cookie.If no cookie exists with the value, a new cookie will be created.
- Parameters:
jsp- the CmsJspActionElement to usename- the name of the cookievalue- the value of the cookie
-
setNoCacheHeaders
Sets headers to the given response to prevent client side caching.The following headers are set:
Cache-Control: max-age=0
Cache-Control: must-revalidate
Pragma: no-cache- Parameters:
res- the request where to set the no-cache headers
-
setSessionValue
public static void setSessionValue(javax.servlet.http.HttpServletRequest request, String key, Object value) Adds an object to the session of the given HTTP request.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.
- Parameters:
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
-