Class HttpServletSupport

java.lang.Object
net.shibboleth.utilities.java.support.net.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.
  • Method Summary

    Modifier and Type Method Description
    static void addNoCacheHeaders​(javax.servlet.http.HttpServletResponse response)
    Adds Cache-Control and Pragma headers meant to disable caching.
    static URI getFullRequestURI​(javax.servlet.http.HttpServletRequest request)
    Gets the URL that was requested to generate this request.
    static List<Locale.LanguageRange> getLanguageRange​(javax.servlet.http.HttpServletRequest request)
    Return the Locale.LanguageRange associated with this request.
    static String getRemoteAddr​(javax.servlet.ServletRequest request)
    Gets the sanitized form of the result of ServletRequest.getRemoteAddr().
    static String getRequestPathWithoutContext​(javax.servlet.http.HttpServletRequest request)
    Gets the request URI as returned by HttpServletRequest.getRequestURI() but without the servlet context path.
    static void setContentType​(javax.servlet.http.HttpServletResponse response, String contentType)
    Sets the MIME content type of the response.
    static void setUTF8Encoding​(javax.servlet.http.HttpServletResponse response)
    Sets the character encoding of the transport to UTF-8.
    static boolean validateContentType​(javax.servlet.http.HttpServletRequest request, Set<MediaType> validTypes, boolean noContentTypeIsValid, boolean isOneOfStrategy)
    Validate the Content-Type of the specified request.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOG

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

    • HttpServletSupport

      private HttpServletSupport()
      Constructor.
  • Method Details

    • 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
    • getLanguageRange

      @Nonnull @NonnullElements @Unmodifiable public static List<Locale.LanguageRange> getLanguageRange​(@Nonnull javax.servlet.http.HttpServletRequest request)
      Return the Locale.LanguageRange associated with this request. This is more up to date than ServletRequest.getLocales().
      Parameters:
      request - the request to process
      Returns:
      The range.
    • 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