Class HttpUtils

java.lang.Object
org.craftercms.commons.http.HttpUtils

public class HttpUtils extends Object
Utility methods for HTTP related stuff.
Author:
avasquez
  • Field Details

  • 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 URL
      forceHttps - if HTTPS should be enforced
      Returns:
      the base request URL
    • getFullUrl

      public static String getFullUrl(javax.servlet.http.HttpServletRequest request, String relativeUrl)
      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 URL
      relativeUrl - 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 URI
      includeQueryString - 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 cookie
      request - the request where to extract the request from
      Returns:
      the cookie object, or null if not found
    • getCookieValue

      public static String getCookieValue(String name, javax.servlet.http.HttpServletRequest request)
      Returns the cookie value with the given name for the given request
      Parameters:
      name - the name of the cookie
      request - 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 key
      value - the param value
      params - 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 key
      values - the collection of param values
      params - 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 key
      values - the array of param values
      params - 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 if encodeValues is true. UTF-8 is used as the encoding charset.
      Parameters:
      queryParams - the params to build the query string with
      encodeValues - 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 UnsupportedEncodingException
      Builds a query string from the specified params. The param names are always encoded, but the values are only encoded if encodeValues is true.
      Parameters:
      queryParams - the params to build the query string with
      charset - the charset to use for URL encoding
      encodeValues - 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

      public static Map<String,Object> createHeadersMap(javax.servlet.http.HttpServletRequest request)
      Creates a map from the headers in the specified request. Multi value headers will be in an array.
      Parameters:
      request - the request
    • createCookiesMap

      public static Map<String,String> createCookiesMap(javax.servlet.http.HttpServletRequest request)
      Creates a map from the cookies in the specified request.
      Parameters:
      request - the request
    • createSessionMap

      public static Map<String,Object> createSessionMap(javax.servlet.http.HttpServletRequest request)
      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.