Class HttpServletSupport


  • public final class HttpServletSupport
    extends Object
    Utilities for working with HTTP Servlet requests and responses.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.slf4j.Logger LOG
      Log.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private HttpServletSupport()
      Constructor.
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
        Log.
    • Constructor Detail

      • HttpServletSupport

        private HttpServletSupport()
        Constructor.
    • Method Detail

      • addNoCacheHeaders

        public static void addNoCacheHeaders​(@Nonnull
                                             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​(@Nonnull
                                           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​(@Nonnull
                                          javax.servlet.http.HttpServletResponse response,
                                          @Nullable
                                          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​(@Nonnull
                                                          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​(@Nonnull
                                            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​(@Nonnull
                                                  javax.servlet.http.HttpServletRequest request,
                                                  @Nonnull @NonnullElements
                                                  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
      • getRemoteAddr

        @Nullable
        public static String getRemoteAddr​(@Nonnull
                                           javax.servlet.ServletRequest request)
        Gets the sanitized form of the result of ServletRequest.getRemoteAddr().

        This routine accounts for variability in the format of the returned address string, in particular the incorrect use of brackets around IPv6 addresses, a form intended to be used when expressing addresses as hostnames, not as bare addresses.

        Parameters:
        request - servlet request
        Returns:
        sanitized address string