Class ServletUtils


  • public class ServletUtils
    extends Object
    Provides Servlet Utility methods.
    • Field Detail

      • PARAM_OBJECT_TAG

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

        public static final String PARAM_AcrobatPluginVersion
        The url parameter which specifies the Adobe Acrobat plugin version detected using JavaScript.
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
    • Constructor Detail

      • ServletUtils

        public ServletUtils()
    • Method Detail

      • getAddressFromRequest

        public static String getAddressFromRequest​(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​(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​(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​(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​(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​(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​(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​(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​(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​(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​(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​(HttpServletRequest request,
                                                 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​(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​(HttpServletRequest request,
                                             HttpServletResponse response)
        Disable the caching in the response header.
        Parameters:
        request - HTTP request
        response - HTTP response
      • setNoCacheHeadersAlways

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

        public static void setCacheHeaders​(HttpServletRequest request,
                                           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​(HttpServletRequest request,
                                           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​(HttpServletRequest request,
                                           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​(HttpServletRequest request,
                                           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​(HttpServletRequest request,
                                        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​(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​(HttpServletRequest request,
                                                  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​(HttpServletRequest request,
                                                     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 Cookie getCookie​(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 Cookie setCookie​(HttpServletRequest request,
                                       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​(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​(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​(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​(HttpServletRequest request)
        Generates a new session containing all attributes in the current session.
        Parameters:
        request - the HTTP servlet request
      • getRequestInfoDebug

        public static String getRequestInfoDebug​(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​(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​(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​(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