Package org.craftercms.commons.http
Class HttpUtils
java.lang.Object
org.craftercms.commons.http.HttpUtils
Utility methods for HTTP related stuff.
- Author:
- avasquez
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Adds a param value to a params map.static void
addValues
(String key, String[] values, org.springframework.util.MultiValueMap<String, String> params) Adds an array of param values to a params map.static void
addValues
(String key, Collection<String> values, org.springframework.util.MultiValueMap<String, String> params) Adds a collection of param values to a params map.createCookiesMap
(javax.servlet.http.HttpServletRequest request) Creates a map from the cookies in the specified request.createHeadersMap
(javax.servlet.http.HttpServletRequest request) Creates a map from the headers in the specified request.createRequestAttributesMap
(javax.servlet.http.HttpServletRequest request) Creates a map from the request attributes in the specified request.createRequestParamsMap
(javax.servlet.http.HttpServletRequest request) Creates a map from the parameters in the specified request.createSessionMap
(javax.servlet.http.HttpServletRequest request) Creates a map from the session attributes in the specified request.static void
disableCaching
(javax.servlet.http.HttpServletResponse response) Disable caching in the client.static StringBuilder
getBaseRequestUrl
(javax.servlet.http.HttpServletRequest request, boolean forceHttps) Returns the portion from the URL that includes the scheme, server name and port number, without the server path.static javax.servlet.http.Cookie
Returns the cookie with the given name for the given requeststatic String
getCookieValue
(String name, javax.servlet.http.HttpServletRequest request) Returns the cookie value with the given name for the given requeststatic final String
getFullRequestUri
(javax.servlet.http.HttpServletRequest request, boolean includeQueryString) Returns the full request URI, including scheme, server name, port number and server path.static String
getFullUrl
(javax.servlet.http.HttpServletRequest request, String relativeUrl) Returns the full URL for the relative URL based in the specified request.getParamsFromQueryString
(String queryString) Returns a map with the extracted parameters from the specified query string.static String
getQueryStringFromParams
(org.springframework.util.MultiValueMap<String, String> queryParams) Builds a query string from the specified params.static String
getQueryStringFromParams
(org.springframework.util.MultiValueMap<String, String> queryParams, boolean encodeValues) Builds a query string from the specified params.static String
getQueryStringFromParams
(org.springframework.util.MultiValueMap<String, String> queryParams, String charset, boolean encodeValues) Builds a query string from the specified params.static final String
getRequestUriWithoutContextPath
(javax.servlet.http.HttpServletRequest request) Returns the request URI without the context path.
-
Field Details
-
HTTP_SCHEME
- See Also:
-
HTTPS_SCHEME
- See Also:
-
DEFAULT_HTTP_PORT
public static final int DEFAULT_HTTP_PORT- See Also:
-
DEFAULT_HTTPS_PORT
public static final int DEFAULT_HTTPS_PORT- See Also:
-
PRAGMA_HEADER_NAME
- See Also:
-
CACHE_CONTROL_HEADER_NAME
- See Also:
-
EXPIRES_HEADER_NAME
- See Also:
-
-
Constructor Details
-
HttpUtils
public HttpUtils()
-
-
Method Details
-
getBaseRequestUrl
public static StringBuilder getBaseRequestUrl(javax.servlet.http.HttpServletRequest request, boolean forceHttps) Returns the portion from the URL that includes the scheme, server name and port number, without the server path.- Parameters:
request
- the request object used to build the base URLforceHttps
- if HTTPS should be enforced- Returns:
- the base request URL
-
getFullUrl
Returns the full URL for the relative URL based in the specified request. The full URL includes the scheme, server name, port number, context path and server path.- Parameters:
request
- the request object used to build the base URLrelativeUrl
- the relative URL- Returns:
- the full URL
-
getRequestUriWithoutContextPath
public static final String getRequestUriWithoutContextPath(javax.servlet.http.HttpServletRequest request) Returns the request URI without the context path.- Parameters:
request
- the request where to get the URI from
-
getFullRequestUri
public static final String getFullRequestUri(javax.servlet.http.HttpServletRequest request, boolean includeQueryString) Returns the full request URI, including scheme, server name, port number and server path. The query string is optional.- Parameters:
request
- the request object used to build the URIincludeQueryString
- if the query string should be appended to the URI- Returns:
- the full request URI
-
getCookie
public static javax.servlet.http.Cookie getCookie(String name, javax.servlet.http.HttpServletRequest request) Returns the cookie with the given name for the given request- Parameters:
name
- the name of the cookierequest
- the request where to extract the request from- Returns:
- the cookie object, or null if not found
-
getCookieValue
Returns the cookie value with the given name for the given request- Parameters:
name
- the name of the cookierequest
- the request where to extract the request from- Returns:
- the cookie value, or null if no cookie found
-
addValue
public static void addValue(String key, Object value, org.springframework.util.MultiValueMap<String, String> params) Adds a param value to a params map. If value is null, nothing is done.- Parameters:
key
- the param keyvalue
- the param valueparams
- the params map
-
addValues
public static void addValues(String key, Collection<String> values, org.springframework.util.MultiValueMap<String, String> params) Adds a collection of param values to a params map. If the collection is null, nothing is done.- Parameters:
key
- the param keyvalues
- the collection of param valuesparams
- the params map
-
addValues
public static void addValues(String key, String[] values, org.springframework.util.MultiValueMap<String, String> params) Adds an array of param values to a params map. If the array is null, nothing is done.- Parameters:
key
- the param keyvalues
- the array of param valuesparams
- the params map
-
getParamsFromQueryString
public static org.springframework.util.MultiValueMap<String,String> getParamsFromQueryString(String queryString) Returns a map with the extracted parameters from the specified query string. A multi value map is used since there can be several values for the same param.- Parameters:
queryString
- the query string to extract the params from- Returns:
- the param map
-
getQueryStringFromParams
public static String getQueryStringFromParams(org.springframework.util.MultiValueMap<String, String> queryParams) Builds a query string from the specified params. The param names and the values are always encoded. UTF-8 is used as the encoding charset.- Parameters:
queryParams
- the params to build the query string with- Returns:
- the query string
-
getQueryStringFromParams
public static String getQueryStringFromParams(org.springframework.util.MultiValueMap<String, String> queryParams, boolean encodeValues) Builds a query string from the specified params. The param names are always encoded, but the values are only encoded ifencodeValues
is true. UTF-8 is used as the encoding charset.- Parameters:
queryParams
- the params to build the query string withencodeValues
- if the param values should be encoded- Returns:
- the query string
-
getQueryStringFromParams
public static String getQueryStringFromParams(org.springframework.util.MultiValueMap<String, String> queryParams, String charset, boolean encodeValues) throws UnsupportedEncodingExceptionBuilds a query string from the specified params. The param names are always encoded, but the values are only encoded ifencodeValues
is true.- Parameters:
queryParams
- the params to build the query string withcharset
- the charset to use for URL encodingencodeValues
- if the param values should be encoded- Returns:
- the query string
- Throws:
UnsupportedEncodingException
-
createRequestParamsMap
public static Map<String,Object> createRequestParamsMap(javax.servlet.http.HttpServletRequest request) Creates a map from the parameters in the specified request. Multi value parameters will be in an array.- Parameters:
request
- the request
-
createRequestAttributesMap
public static Map<String,Object> createRequestAttributesMap(javax.servlet.http.HttpServletRequest request) Creates a map from the request attributes in the specified request.- Parameters:
request
- the request
-
createHeadersMap
Creates a map from the headers in the specified request. Multi value headers will be in an array.- Parameters:
request
- the request
-
createCookiesMap
Creates a map from the cookies in the specified request.- Parameters:
request
- the request
-
createSessionMap
Creates a map from the session attributes in the specified request.- Parameters:
request
- the request
-
disableCaching
public static void disableCaching(javax.servlet.http.HttpServletResponse response) Disable caching in the client.- Parameters:
response
- the response to add the headers for disabling caching.
-