Class HttpServletSupport


  • @Beta
    public final class HttpServletSupport
    extends Object
    Utilities for working with HTTP Servlet requests and responses.
    • Constructor Detail

      • HttpServletSupport

        private HttpServletSupport()
        Constructor.
    • Method Detail

      • addNoCacheHeaders

        public static void addNoCacheHeaders​(javax.servlet.http.HttpServletResponse response)
        Adds Cache-Control and Pragma headers meant to disable caching.
        Parameters:
        response - transport to add headers to
      • setUTF8Encoding

        public static void setUTF8Encoding​(javax.servlet.http.HttpServletResponse response)
        Sets the character encoding of the transport to UTF-8.
        Parameters:
        response - transport to set character encoding type
      • setContentType

        public static void setContentType​(javax.servlet.http.HttpServletResponse response,
                                          String contentType)
        Sets the MIME content type of the response.
        Parameters:
        response - the transport to set content type on
        contentType - the content type to set
      • getRequestPathWithoutContext

        public static String getRequestPathWithoutContext​(javax.servlet.http.HttpServletRequest request)
        Gets the request URI as returned by HttpServletRequest.getRequestURI() but without the servlet context path.
        Parameters:
        request - request to get the URI from
        Returns:
        constructed URI
      • getFullRequestURI

        public static URI getFullRequestURI​(javax.servlet.http.HttpServletRequest request)
        Gets the URL that was requested to generate this request. This includes the scheme, host, port, path, and query string.
        Parameters:
        request - current request
        Returns:
        URL that was requested to generate this request
      • validateContentType

        public static boolean validateContentType​(javax.servlet.http.HttpServletRequest request,
                                                  Set<MediaType> validTypes,
                                                  boolean noContentTypeIsValid,
                                                  boolean isOneOfStrategy)
        Validate the Content-Type of the specified request.

        Two strategies are supported for evaluating the request's parsed content type:

        1. If isOneOfStrategy is true, then the MediaType parsed from the request is compared to each of the specified valid types via MediaType.is(MediaType). If any pass, the type is considered valid. This allows use of MediaType's support for wildcard and parameter evaluation.
        2. If isOneOfStrategy is false, then the MediaType parsed from the request is stripped of its parameters, as is each of the valid types. Then a simple evaluation is done that the request type is equal to one of the passed types. In this case, only literal types and subtypes should be passed as valid types; wildcards should not be used.
        Parameters:
        request - the request to be validated
        validTypes - the set of valid media types
        noContentTypeIsValid - flag whether the case of a missing/empty Content-Type header is considered valid
        isOneOfStrategy - flag for the strategy used in the validation (see above for details)
        Returns:
        true if the content type is valid, false if not