Class ServletUtils

java.lang.Object
com.avoka.core.util.ServletUtils

public class ServletUtils extends Object
Provides Servlet Utility methods.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The decoded user agent browser types.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default PDF open window URL parameters (show toolbar, show navpanes, show scrollbar).
    static final String
    The url parameter which specifies the Adobe Acrobat plugin version detected using JavaScript.
    static final String
    The using objectTag request parameter name.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    acceptsGzipEncoding(jakarta.servlet.http.HttpServletRequest request)
    Check whether the request accepts encoding gzip.
    static Double
    getAcrobatPluginVersion(jakarta.servlet.http.HttpServletRequest request)
    Return Adobe Acrobat (Reader) plug-in version number if defined in the request parameter 'AcrobatPluginVersion', or null otherwise.
    static String
    getAddressFromRequest(jakarta.servlet.http.HttpServletRequest request)
    Return the remote address for the given request, using the X-Forwarded-For if available.
    getBrowserType(jakarta.servlet.http.HttpServletRequest request)
    Return the browser type [IE, FF, OTHER].
    static jakarta.servlet.http.Cookie
    getCookie(jakarta.servlet.http.HttpServletRequest request, String name)
    Returns the specified Cookie object, or null if the cookie does not exist.
    static String
    getCookieValue(jakarta.servlet.http.HttpServletRequest request, String name)
    Returns the value of the specified cookie as a String.
    static String
    getReferer(jakarta.servlet.http.HttpServletRequest request)
    Return the referer HTTP Header value.
    static String
    getRequestErrorDebug(jakarta.servlet.http.HttpServletRequest request, Map<String,Object> parameters)
    Return a request error debug string for the given request and parameters map.
    static String
    getRequestInfoDebug(jakarta.servlet.http.HttpServletRequest request)
    Return a request info debug string for the given request
    static String
    getRequestInfoDebug(jakarta.servlet.http.HttpServletRequest request, Map<String,Object> parameters)
    Return a request info debug string for the given request and parameters map.
    static String
    getRequestParameterDebug(jakarta.servlet.http.HttpServletRequest request, boolean decode)
    Return a debug string of the request parameter names and values.
    static String
    getRequestURI(jakarta.servlet.http.HttpServletRequest request)
    Return the requestURI from the request.
    static String
    getResourcePath(jakarta.servlet.http.HttpServletRequest request)
    Return the page resource path from the request.
    static String
    getUserAgent(jakarta.servlet.http.HttpServletRequest request)
    Return the user agent header (browser string).
    static boolean
    isContentTypeRequest(jakarta.servlet.http.HttpServletRequest request)
    Return true if an IE Plug-in "contype" type request.
    static boolean
    isDoubleObjectTagRequest(jakarta.servlet.http.HttpServletRequest request)
    Return true if a HTML object tag double request for a PDF form which should be ignored.
    static boolean
    isIE6to8(jakarta.servlet.http.HttpServletRequest request)
    Return true if the User Agent header is [ "MSIE 6.0" | "MSIE 7.0" | "MSIE 8.0" ].
    static boolean
    isIE6to9(jakarta.servlet.http.HttpServletRequest request)
    Return true if the User Agent header is IE6 to IE9 [ "MSIE 6.0" | "MSIE 7.0" | "Trident/4.0" | "Trident/5.0" ].
    static boolean
    isIEandNotV8to10(jakarta.servlet.http.HttpServletRequest request)
    Return true if the browser is IE and not version IE8 to IE10.
    static void
    regenerateSession(jakarta.servlet.http.HttpServletRequest request)
    Generates a new session containing all attributes in the current session.
    static long
    renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray)
    Render a byte array to the browser.
    static long
    renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray, boolean doCompress)
    Render a byte array to the browser.
    static long
    renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray, byte[] byteArrayGzip, boolean doCompress)
    Render a byte array or zipped byte array to the browser
    static void
    sendRedirect(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
    Redirect a request to the new URL path.
    static void
    sendRedirect(jakarta.servlet.http.HttpServletResponse response, String location, Map<String,?> params)
    Redirect the request to the specified location with the given parameters.
    static void
    sendRedirectAferPdfSubmit(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
    Redirect a request to the new URL path.
    static void
    sendRedirectParentPage(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
    Redirect a request to the specified URL and redirect any parent page if the request was from an IFRAME.
    static void
    setCacheHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, int durationSecs)
    Set caching time in the header
    static void
    setContentDisposition(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String contentType, String filename, boolean saveAsAttachment)
    Set content disposition for the response.
    static void
    setContentDownloadHeader(jakarta.servlet.http.HttpServletResponse response, String filename)
    Sets the content download header.
    static jakarta.servlet.http.Cookie
    setCookie(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name, String value, int maxAge, String path)
    Sets the given cookie values in the servlet response.
    static void
    setNoCacheHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Disable the caching in the response header.
    static void
    setNoCacheHeadersAlways(jakarta.servlet.http.HttpServletResponse response)
    Disable the caching in the response header always.
    static String
    Decode the given URL encode value.
    static String
    URL encode the given value making it safe to pass in a URL.

    Methods inherited from class java.lang.Object

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

    • PARAM_OBJECT_TAG

      public static final String PARAM_OBJECT_TAG
      The using objectTag request parameter name.
      See Also:
    • PARAM_AcrobatPluginVersion

      public static final String PARAM_AcrobatPluginVersion
      The url parameter which specifies the Adobe Acrobat plugin version detected using JavaScript.
      See Also:
    • DEFAULT_PDF_OPEN_PARAMS

      public static final String DEFAULT_PDF_OPEN_PARAMS
      The default PDF open window URL parameters (show toolbar, show navpanes, show scrollbar).
      See Also:
  • Constructor Details

    • ServletUtils

      public ServletUtils()
  • Method Details

    • getAddressFromRequest

      public static String getAddressFromRequest(jakarta.servlet.http.HttpServletRequest request)
      Return the remote address for the given request, using the X-Forwarded-For if available.

      This code was derived from http://r.va.gg/2011/07/handling-x-forwarded-for-in-java-and-tomcat.html

      Parameters:
      request - the http request
      Returns:
      the address from the given request
      Since:
      4.0.0
    • getReferer

      public static String getReferer(jakarta.servlet.http.HttpServletRequest request)
      Return the referer HTTP Header value.
      Parameters:
      request - the servlet request (required)
      Returns:
      the referer HTTP Header value, or null if not found
      Since:
      4.0.0
    • urlEncode

      public static String urlEncode(String value)
      URL encode the given value making it safe to pass in a URL.
      Parameters:
      value - the value to URL encode
      Returns:
      the URL encoded value
    • urlDecode

      public static String urlDecode(String value)
      Decode the given URL encode value.
      Parameters:
      value - the URL encode value
      Returns:
      the URL decoded value
    • getUserAgent

      public static String getUserAgent(jakarta.servlet.http.HttpServletRequest request)
      Return the user agent header (browser string).
      Parameters:
      request - the servlet request
      Returns:
      the user agent header
    • getBrowserType

      public static ServletUtils.BrowserType getBrowserType(jakarta.servlet.http.HttpServletRequest request)
      Return the browser type [IE, FF, OTHER].
      Parameters:
      request - the servlet request
      Returns:
      the browser type [IE, FF, OTHER]
    • isIE6to8

      public static boolean isIE6to8(jakarta.servlet.http.HttpServletRequest request)
      Return true if the User Agent header is [ "MSIE 6.0" | "MSIE 7.0" | "MSIE 8.0" ].
      Parameters:
      request - the servlet request
      Returns:
      true if the User Agent header is [ "MSIE 6.0" | "MSIE 7.0" | "MSIE 8.0" ]
    • isIE6to9

      public static boolean isIE6to9(jakarta.servlet.http.HttpServletRequest request)
      Return true if the User Agent header is IE6 to IE9 [ "MSIE 6.0" | "MSIE 7.0" | "Trident/4.0" | "Trident/5.0" ].
      Parameters:
      request - the servlet request
      Returns:
      true if the User Agent header is IE6 to IE9 [ "MSIE 6.0" | "MSIE 7.0" | "Trident/4.0" | "Trident/5.0" ]
      Since:
      4.0.1
    • isIEandNotV8to10

      public static boolean isIEandNotV8to10(jakarta.servlet.http.HttpServletRequest request)
      Return true if the browser is IE and not version IE8 to IE10.
      Parameters:
      request - the servlet request
      Returns:
      true if the browser is IE and not version IE8 to IE10
      Since:
      4.0.0
    • isContentTypeRequest

      public static boolean isContentTypeRequest(jakarta.servlet.http.HttpServletRequest request)
      Return true if an IE Plug-in "contype" type request.
      Parameters:
      request - the servlet request
      Returns:
      true if an IE Plug-in "contype" type request
    • getAcrobatPluginVersion

      public static Double getAcrobatPluginVersion(jakarta.servlet.http.HttpServletRequest request)
      Return Adobe Acrobat (Reader) plug-in version number if defined in the request parameter 'AcrobatPluginVersion', or null otherwise.
      Parameters:
      request - the servlet request
      Returns:
      the Adobe Acrobat (Reader) plug-in version number if defined, or null otherwise
    • isDoubleObjectTagRequest

      public static boolean isDoubleObjectTagRequest(jakarta.servlet.http.HttpServletRequest request)
      Return true if a HTML object tag double request for a PDF form which should be ignored.
      Parameters:
      request - the servlet request
      Returns:
      true if a HTML object tag double request for a PDF form which should be ignored
    • acceptsGzipEncoding

      public static boolean acceptsGzipEncoding(jakarta.servlet.http.HttpServletRequest request)
      Check whether the request accepts encoding gzip.
      Parameters:
      request - the http request
      Returns:
      true if it allows encoding gzip
    • setContentDisposition

      public static void setContentDisposition(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String contentType, String filename, boolean saveAsAttachment)
      Set content disposition for the response.
      Parameters:
      request - http request
      response - http response
      contentType - the content type
      filename - file name
      saveAsAttachment - a flag checks whether a user wants to save as attachment
    • setContentDownloadHeader

      public static void setContentDownloadHeader(jakarta.servlet.http.HttpServletResponse response, String filename)
      Sets the content download header.
      Parameters:
      response - the response (required)
      filename - the filename (required)
      Since:
      3.6.5
    • setNoCacheHeaders

      public static void setNoCacheHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
      Disable the caching in the response header.
      Parameters:
      request - HTTP request
      response - HTTP response
    • setNoCacheHeadersAlways

      public static void setNoCacheHeadersAlways(jakarta.servlet.http.HttpServletResponse response)
      Disable the caching in the response header always.
      Parameters:
      response - HTTP response
      Since:
      3.6.0
    • setCacheHeaders

      public static void setCacheHeaders(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, int durationSecs)
      Set caching time in the header
      Parameters:
      request - HTTP request
      response - HTTP response
      durationSecs - time duration
    • renderByteArray

      public static long renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray) throws IOException
      Render a byte array to the browser.
      Parameters:
      request - HTTP request
      response - HTTP response
      byteArray - the byte array to render
      Returns:
      byte array length
      Throws:
      IOException - if If an I/O error has occurred.
    • renderByteArray

      public static long renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray, boolean doCompress) throws IOException
      Render a byte array to the browser.
      Parameters:
      request - HTTP request
      response - HTTP response
      byteArray - the byte array to render
      doCompress - whether to compress in the render
      Returns:
      byte array length
      Throws:
      IOException - If an I/O error has occurred.
    • renderByteArray

      public static long renderByteArray(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, byte[] byteArray, byte[] byteArrayGzip, boolean doCompress) throws IOException
      Render a byte array or zipped byte array to the browser
      Parameters:
      request - HTTP request
      response - HTTP response
      byteArray - the byte array to render
      byteArrayGzip - Gzipped byte array to render
      doCompress - whether allows gzip encoding
      Returns:
      byte array length
      Throws:
      IOException - If an I/O error has occurred.
    • sendRedirect

      public static void sendRedirect(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
      Redirect a request to the new URL path.
      Parameters:
      request - HTTP request
      response - HTTP response
      path - the new URL path
    • sendRedirect

      public static void sendRedirect(jakarta.servlet.http.HttpServletResponse response, String location, Map<String,?> params)
      Redirect the request to the specified location with the given parameters.
      Parameters:
      response - the response (required)
      location - the location to redirect to (required)
      params - the request parameter
      Since:
      17.10.0
    • sendRedirectParentPage

      public static void sendRedirectParentPage(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
      Redirect a request to the specified URL and redirect any parent page if the request was from an IFRAME.
      Parameters:
      request - HTTP request
      response - HTTP response
      path - the new URL path to redirect to
    • sendRedirectAferPdfSubmit

      public static void sendRedirectAferPdfSubmit(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
      Redirect a request to the new URL path.
      Parameters:
      request - HTTP request
      response - HTTP response
      path - the new URL path
    • getCookie

      public static jakarta.servlet.http.Cookie getCookie(jakarta.servlet.http.HttpServletRequest request, String name)
      Returns the specified Cookie object, or null if the cookie does not exist.

      Copied from Apache ClickUtils method of the same name.

      Parameters:
      request - the servlet request
      name - the name of the cookie
      Returns:
      the Cookie object if it exists, otherwise null
    • setCookie

      public static jakarta.servlet.http.Cookie setCookie(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String name, String value, int maxAge, String path)
      Sets the given cookie values in the servlet response.

      This will also put the cookie in a list of cookies to send with this request's response (so that in case of a redirect occurring down the chain, the first filter will always try to set this cookie again)

      The cookie secure flag is set if the request is secure.

      Parameters:
      request - the servlet request
      response - the servlet response
      name - the cookie name
      value - the cookie value
      maxAge - the maximum age of the cookie in seconds. A negative value will expire the cookie at the end of the session, while 0 will delete the cookie.
      path - the cookie path
      Returns:
      the Cookie object created and set in the response
    • getCookieValue

      public static String getCookieValue(jakarta.servlet.http.HttpServletRequest request, String name)
      Returns the value of the specified cookie as a String. If the cookie does not exist, the method returns null.
      Parameters:
      request - the servlet request
      name - the name of the cookie
      Returns:
      the value of the cookie, or null if the cookie does not exist.
    • getResourcePath

      public static String getResourcePath(jakarta.servlet.http.HttpServletRequest request)
      Return the page resource path from the request. For example:
       http://www.mycorp.com/banking/secure/login.htm  ->  /secure/login.htm 
      Parameters:
      request - the page servlet request (required)
      Returns:
      the page resource path from the request
      Since:
      5.1.0
    • getRequestParameterDebug

      public static String getRequestParameterDebug(jakarta.servlet.http.HttpServletRequest request, boolean decode)
      Return a debug string of the request parameter names and values.
      Parameters:
      request - the HTTP servlet request
      decode - a flag to decode any URL encoded value
      Returns:
      a debug string of the request values
    • regenerateSession

      public static void regenerateSession(jakarta.servlet.http.HttpServletRequest request)
      Generates a new session containing all attributes in the current session.
      Parameters:
      request - the HTTP servlet request
    • getRequestInfoDebug

      public static String getRequestInfoDebug(jakarta.servlet.http.HttpServletRequest request)
      Return a request info debug string for the given request
      Parameters:
      request - the request to debug (required)
      Returns:
      the request debug info string
      Since:
      4.3.2
    • getRequestInfoDebug

      public static String getRequestInfoDebug(jakarta.servlet.http.HttpServletRequest request, Map<String,Object> parameters)
      Return a request info debug string for the given request and parameters map.
      Parameters:
      request - the request to debug (required)
      parameters - the request parameters to debug (optional)
      Returns:
      the request debug info string
      Since:
      18.5.0
    • getRequestErrorDebug

      public static String getRequestErrorDebug(jakarta.servlet.http.HttpServletRequest request, Map<String,Object> parameters)
      Return a request error debug string for the given request and parameters map.
      Parameters:
      request - the request to debug (required)
      parameters - the request parameters to debug (optional)
      Returns:
      the request debug info string
      Since:
      18.5.0
    • getRequestURI

      public static String getRequestURI(jakarta.servlet.http.HttpServletRequest request)
      Return the requestURI from the request. For example:
       http://www.mycorp.com/banking/secure/login.htm  ->  /banking/secure/login.htm 

      Copied from Apache ClickUtils method of the same name.

      Parameters:
      request - the page servlet request
      Returns:
      the requestURI from the request
      Since:
      17.10.0