Class HTTPTool

  • Direct Known Subclasses:
    ServletTool

    public class HTTPTool
    extends java.lang.Object

    HTTP toolbox

    • Constructor Summary

      Constructors 
      Constructor Description
      HTTPTool()
      Constructor
      Don't instantiate this class, it only provides static variables and methods
    • Method Summary

      Modifier and Type Method Description
      static java.lang.String append​(java.lang.String url, java.lang.String params)
      Append GET parameters to URL
      static java.lang.String append​(java.lang.String url, java.lang.String name, java.lang.Object value)
      Append a GET parameter to URL
      static java.lang.String append​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.Object> params)
      Append GET parameters to URL
      static java.lang.String appendURLParameter​(java.lang.String url, java.lang.String params)
      Deprecated.
      Use append
      static java.lang.String appendURLParameter​(java.lang.String url, java.lang.String name, java.lang.Object value)
      Deprecated.
      Use append
      static java.lang.String appendURLParameter​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.Object> params)
      Deprecated.
      Use append
      static java.lang.String baseURL​(java.lang.String url)
      Get base URL without parameters
      static java.lang.String[] call​(java.net.HttpURLConnection conn, java.lang.Object request)
      Call the HTTP request and disconnect
      static java.lang.String[] call​(java.net.HttpURLConnection conn, java.lang.Object request, java.lang.String encoding)
      Call the HTTP request and disconnect
      static int callMultipart​(java.net.HttpURLConnection conn, java.lang.String[] textFields, java.lang.String[] values, java.lang.String[] fileFields, java.lang.String[] filePaths, java.lang.String[] dataFields, byte[][] data)
      Call the HTTP request with multi-part data
      static java.lang.String[] callMultipartWithReturn​(java.net.HttpURLConnection conn, java.lang.String[] textFields, java.lang.String[] values, java.lang.String[] fileFields, java.lang.String[] filePaths, java.lang.String[] dataFields, byte[][] data)
      Call the HTTP request and disconnect with multi-part data
      static java.lang.String cleanURI​(java.lang.String uri)
      Get clean URI
      static java.lang.String decode​(java.lang.Object data)
      URL decode given string using platform encoding
      static java.lang.String decode​(java.lang.Object data, java.lang.String encoding)
      URL decode given string using specified encoding
      static java.lang.String encode​(java.lang.Object data)
      URL encode given string using platform encoding
      static java.lang.String encode​(java.lang.Object data, java.lang.String encoding)
      URL encode given string using specified encoding
      static java.util.Map<java.lang.String,​java.lang.Object> getBasicAuthLoginPassword​(java.lang.String auth)
      Return user and password from a HTTP basic authentication string (base64 encoded).
      static java.lang.String getBasicAuthString​(java.lang.String user, java.lang.String pwd)
      Return HTTP basic authentication string (base64 encoded) for use in Authorization or Proxy-Authrorization headers
      static int getCacheMaxAge​(Grant g)
      Returns configured maximum cache age
      static java.lang.String getCookie​(java.net.HttpURLConnection conn)
      Deprecated.
      Use getCookies
      static java.lang.String getCookies​(java.net.HttpURLConnection conn)
      Get a connection's cookies
      static java.lang.String getExtensionFromMedia​(java.lang.String media)  
      static java.lang.String getHeaderField​(java.net.HttpURLConnection conn, java.lang.String name)
      Get a connection header field
      static int getIEVersion​(java.lang.String userAgent)
      Get Internet Explorer major version number (returns -1 if other browser)
      static java.lang.String getMediaFromMimeType​(java.lang.String mimeType)  
      static java.lang.String getMimeTypeFromMedia​(java.lang.String media)  
      static java.lang.String getMimeTypeWithEncoding​(java.lang.String mime)  
      static java.lang.String getMimeTypeWithEncoding​(java.lang.String mime, java.lang.String encoding)  
      static java.net.HttpURLConnection initConnection​(java.lang.String url, java.lang.String method, java.lang.String contentType, java.lang.String cookie, java.lang.String user, java.lang.String pwd, boolean proxy, int timeout)
      Get one HTTP connection
      static javax.net.ssl.HttpsURLConnection initConnectionSSL​(java.lang.String url, java.lang.String method, java.lang.String contentType, java.lang.String cookie, java.io.InputStream jks, java.lang.String pwd, int timeout)
      Get one HTTPS connection
      static javax.net.ssl.HttpsURLConnection initConnectionSSL​(java.lang.String url, java.lang.String method, java.lang.String contentType, java.lang.String cookie, java.lang.String user, java.lang.String pwd, boolean proxy, int timeout)
      Get one HTTPS connection
      static boolean isBrowser​(java.lang.String userAgent, java.lang.String browser)
      Checks if user agent is specified browser.
      static boolean isChrome​(java.lang.String userAgent)
      Checks if user agent is Chrome.
      static boolean isEdge​(java.lang.String userAgent)
      Checks if user agent is Edge.
      static boolean isEmpty​(java.lang.String value)
      Check is HTTP value is empty or equals HTTP.NULL
      static boolean isFirefox​(java.lang.String userAgent)
      Checks if user agent is Firefox.
      static boolean isIE​(java.lang.String userAgent)
      Checks if user agent is Internet Explorer < 11.
      static boolean isIE11​(java.lang.String userAgent)
      Checks if user agent is Internet Explorer 11.
      static boolean isIEMaxVersion​(java.lang.String userAgent, int maxVersion)
      Check if browser is Internet Explorer with specified major version number or less
      static boolean isMobileBrowser​(java.lang.String userAgent)
      Checks if user agent is a mobile browser.
      static boolean isMobileWebkit​(java.lang.String userAgent)
      Checks if user agent is a iPhone/iPad/iPod/Android webkit browser.
      static boolean isOpera​(java.lang.String userAgent)
      Checks if user agent is Opera.
      static boolean isSafari​(java.lang.String userAgent)
      Checks if user agent is Safari.
      static boolean isURLAbsolute​(java.lang.String url)
      Checks if URL is absolute
      static java.lang.String paramsToQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params)
      Get a query string from a parameters map
      static java.lang.String paramsToQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params, java.lang.String encoding)
      Get a query string from a parameters map
      static java.util.Map<java.lang.String,​java.lang.Object> queryStringToParams​(java.lang.String queryString)
      Get a parameters map from a query string
      static java.util.Map<java.lang.String,​java.lang.Object> queryStringToParams​(java.lang.String queryString, java.lang.String encoding)
      Get a parameters map from a query string
      static java.lang.String[] split​(java.lang.String uri)
      Split a cleaned URI into parts (removes the context path)
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HTTPTool

        public HTTPTool()
        Constructor
        Don't instantiate this class, it only provides static variables and methods
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(java.lang.String value)
        Check is HTTP value is empty or equals HTTP.NULL
        Parameters:
        value - Value to check
      • getCacheMaxAge

        public static int getCacheMaxAge​(Grant g)

        Returns configured maximum cache age

        Parameters:
        g - Grant
        Returns:
        Max cache age in seconds
      • getMimeTypeWithEncoding

        public static java.lang.String getMimeTypeWithEncoding​(java.lang.String mime)
      • getMimeTypeWithEncoding

        public static java.lang.String getMimeTypeWithEncoding​(java.lang.String mime,
                                                               java.lang.String encoding)
      • getMimeTypeFromMedia

        public static java.lang.String getMimeTypeFromMedia​(java.lang.String media)
      • getMediaFromMimeType

        public static java.lang.String getMediaFromMimeType​(java.lang.String mimeType)
      • getExtensionFromMedia

        public static java.lang.String getExtensionFromMedia​(java.lang.String media)
      • isBrowser

        public static boolean isBrowser​(java.lang.String userAgent,
                                        java.lang.String browser)
        Checks if user agent is specified browser.
        Parameters:
        userAgent - User agent string
        browser - Browser's pattern in user agent (user HTTPTool.BROWSER_* constants)
      • isIE

        public static boolean isIE​(java.lang.String userAgent)
        Checks if user agent is Internet Explorer < 11.
        Parameters:
        userAgent - User agent string
      • isIE11

        public static boolean isIE11​(java.lang.String userAgent)
        Checks if user agent is Internet Explorer 11.
        Parameters:
        userAgent - User agent string
      • isEdge

        public static boolean isEdge​(java.lang.String userAgent)
        Checks if user agent is Edge.
        Parameters:
        userAgent - User agent string
      • isFirefox

        public static boolean isFirefox​(java.lang.String userAgent)
        Checks if user agent is Firefox.
        Parameters:
        userAgent - User agent string
      • isChrome

        public static boolean isChrome​(java.lang.String userAgent)
        Checks if user agent is Chrome.
        Parameters:
        userAgent - User agent string
      • isOpera

        public static boolean isOpera​(java.lang.String userAgent)
        Checks if user agent is Opera.
        Parameters:
        userAgent - User agent string
      • isSafari

        public static boolean isSafari​(java.lang.String userAgent)
        Checks if user agent is Safari.
        Parameters:
        userAgent - User agent string
      • getIEVersion

        public static int getIEVersion​(java.lang.String userAgent)
        Get Internet Explorer major version number (returns -1 if other browser)
        Parameters:
        userAgent - User agent string
      • isIEMaxVersion

        public static boolean isIEMaxVersion​(java.lang.String userAgent,
                                             int maxVersion)
        Check if browser is Internet Explorer with specified major version number or less
        Parameters:
        userAgent - User agent string
        maxVersion - Maximum version to check if browser version is less or equals to it
      • isMobileWebkit

        public static boolean isMobileWebkit​(java.lang.String userAgent)
        Checks if user agent is a iPhone/iPad/iPod/Android webkit browser.
        Parameters:
        userAgent - User agent string
      • isMobileBrowser

        public static boolean isMobileBrowser​(java.lang.String userAgent)
        Checks if user agent is a mobile browser.
        Parameters:
        userAgent - User agent string
      • baseURL

        public static java.lang.String baseURL​(java.lang.String url)
        Get base URL without parameters
      • isURLAbsolute

        public static boolean isURLAbsolute​(java.lang.String url)
        Checks if URL is absolute
      • encode

        public static java.lang.String encode​(java.lang.Object data)
        URL encode given string using platform encoding
        Parameters:
        data - Data to URL encode (if not a String then toString() is applied to Object)
        Returns:
        Encoded string
      • encode

        public static java.lang.String encode​(java.lang.Object data,
                                              java.lang.String encoding)
        URL encode given string using specified encoding
        Parameters:
        data - Data to URL encode (if not a String then toString() is applied to Object)
        encoding - encoding
        Returns:
        Encoded string
      • decode

        public static java.lang.String decode​(java.lang.Object data)
        URL decode given string using platform encoding
        Parameters:
        data - Data to URL decode (if not a String then toString() is applied to Object)
        Returns:
        Decoded string
      • decode

        public static java.lang.String decode​(java.lang.Object data,
                                              java.lang.String encoding)
        URL decode given string using specified encoding
        Parameters:
        data - Data to URL decode (if not a String then toString() is applied to Object)
        encoding - encoding
        Returns:
        Decoded string
      • appendURLParameter

        @Deprecated
        public static java.lang.String appendURLParameter​(java.lang.String url,
                                                          java.lang.String params)
        Deprecated.
        Use append
      • append

        public static java.lang.String append​(java.lang.String url,
                                              java.lang.String params)
        Append GET parameters to URL
        Parameters:
        url - URL
        params - URL-encoded parameters (in the form name=value[&name=value[...]]
      • appendURLParameter

        @Deprecated
        public static java.lang.String appendURLParameter​(java.lang.String url,
                                                          java.lang.String name,
                                                          java.lang.Object value)
        Deprecated.
        Use append
      • append

        public static java.lang.String append​(java.lang.String url,
                                              java.lang.String name,
                                              java.lang.Object value)
        Append a GET parameter to URL
        Parameters:
        url - URL
        name - Name
        value - Non URL encoded value
      • appendURLParameter

        @Deprecated
        public static java.lang.String appendURLParameter​(java.lang.String url,
                                                          java.util.Map<java.lang.String,​java.lang.Object> params)
        Deprecated.
        Use append
      • append

        public static java.lang.String append​(java.lang.String url,
                                              java.util.Map<java.lang.String,​java.lang.Object> params)
        Append GET parameters to URL
        Parameters:
        url - URL
        params - Map of name and non URL encoded values
      • queryStringToParams

        public static java.util.Map<java.lang.String,​java.lang.Object> queryStringToParams​(java.lang.String queryString)
        Get a parameters map from a query string
        Parameters:
        queryString - Query string
        Returns:
        Parameters map
      • queryStringToParams

        public static java.util.Map<java.lang.String,​java.lang.Object> queryStringToParams​(java.lang.String queryString,
                                                                                                 java.lang.String encoding)
        Get a parameters map from a query string
        Parameters:
        queryString - Query string
        encoding - Encoding
        Returns:
        Parameters map
      • paramsToQueryString

        public static java.lang.String paramsToQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params)
        Get a query string from a parameters map
        Parameters:
        params - Parameters map
        Returns:
        Query string
      • paramsToQueryString

        public static java.lang.String paramsToQueryString​(java.util.Map<java.lang.String,​java.lang.Object> params,
                                                           java.lang.String encoding)
        Get a query string from a parameters map
        Parameters:
        encoding - Encoding
        params - Parameters map
        Returns:
        Query string
      • getBasicAuthString

        public static java.lang.String getBasicAuthString​(java.lang.String user,
                                                          java.lang.String pwd)
        Return HTTP basic authentication string (base64 encoded) for use in Authorization or Proxy-Authrorization headers
        Parameters:
        user - User
        pwd - Password
      • getBasicAuthLoginPassword

        public static java.util.Map<java.lang.String,​java.lang.Object> getBasicAuthLoginPassword​(java.lang.String auth)
                                                                                                throws java.lang.Exception
        Return user and password from a HTTP basic authentication string (base64 encoded).
        Parameters:
        auth - Auth string with keys "login" and "password"
        Throws:
        java.lang.Exception
      • initConnection

        public static java.net.HttpURLConnection initConnection​(java.lang.String url,
                                                                java.lang.String method,
                                                                java.lang.String contentType,
                                                                java.lang.String cookie,
                                                                java.lang.String user,
                                                                java.lang.String pwd,
                                                                boolean proxy,
                                                                int timeout)
                                                         throws java.lang.Exception
        Get one HTTP connection
        Parameters:
        url - Full URL
        method - GET or POST
        contentType - Content-Type header
        cookie - Cookies
        user - Optional user for HTTP basic authentication
        pwd - Optional password for HTTP basic authentication
        proxy - true to use user and pwd in the Proxy-Authorization header instead of the Authorization header
        timeout - timeout value in milliseconds, 0 is interpreted as an infinite timeout
        Returns:
        HTTP connection
        Throws:
        java.lang.Exception
      • initConnectionSSL

        public static javax.net.ssl.HttpsURLConnection initConnectionSSL​(java.lang.String url,
                                                                         java.lang.String method,
                                                                         java.lang.String contentType,
                                                                         java.lang.String cookie,
                                                                         java.lang.String user,
                                                                         java.lang.String pwd,
                                                                         boolean proxy,
                                                                         int timeout)
                                                                  throws java.lang.Exception
        Get one HTTPS connection
        Parameters:
        url - Full URL
        method - GET or POST
        contentType - Content-Type header
        cookie - Cookies
        user - Optional user for HTTP basic authentication
        pwd - Optional password for HTTP basic authentication
        proxy - true to use user and pwd in the Proxy-Authorization header instead of the Authorization header
        timeout - timeout value in milliseconds, 0 is interpreted as an infinite timeout
        Returns:
        HTTP connection
        Throws:
        java.lang.Exception
      • initConnectionSSL

        public static javax.net.ssl.HttpsURLConnection initConnectionSSL​(java.lang.String url,
                                                                         java.lang.String method,
                                                                         java.lang.String contentType,
                                                                         java.lang.String cookie,
                                                                         java.io.InputStream jks,
                                                                         java.lang.String pwd,
                                                                         int timeout)
                                                                  throws java.lang.Exception
        Get one HTTPS connection
        Parameters:
        url - Full URL
        method - GET or POST
        contentType - Content-Type header
        cookie - Cookies
        jks - Optional JKS key store (as stream) for certificate based authentication
        pwd - Optional password for certificate based authentication
        timeout - timeout value in milliseconds, 0 is interpreted as an infinite timeout
        Returns:
        HTTP connection
        Throws:
        java.lang.Exception
      • call

        public static java.lang.String[] call​(java.net.HttpURLConnection conn,
                                              java.lang.Object request)
                                       throws java.lang.Exception
        Call the HTTP request and disconnect
        Parameters:
        conn - HTTP connection
        request - Message to send
        Returns:
        Array with the response and the full cookie
        Throws:
        java.lang.Exception - when status not equals to HTTP 200, 201 or 202
      • call

        public static java.lang.String[] call​(java.net.HttpURLConnection conn,
                                              java.lang.Object request,
                                              java.lang.String encoding)
                                       throws java.lang.Exception
        Call the HTTP request and disconnect
        Parameters:
        conn - HTTP connection
        request - Message to send
        encoding - Encoding (used to encode request to and to decode response from)
        Returns:
        Array with the string response and the full cookie
        Throws:
        java.lang.Exception - when status not equals to HTTP 200, 201 or 202
      • callMultipart

        public static int callMultipart​(java.net.HttpURLConnection conn,
                                        java.lang.String[] textFields,
                                        java.lang.String[] values,
                                        java.lang.String[] fileFields,
                                        java.lang.String[] filePaths,
                                        java.lang.String[] dataFields,
                                        byte[][] data)
                                 throws java.lang.Exception
        Call the HTTP request with multi-part data
        Parameters:
        conn - HTTP connection
        textFields - field names to send
        values - field values
        fileFields - file names to send
        filePaths - file local paths
        dataFields - data names to send (format: name[;file name]
        data - bytes to send
        Returns:
        HTTP response code
        Throws:
        java.lang.Exception
      • callMultipartWithReturn

        public static java.lang.String[] callMultipartWithReturn​(java.net.HttpURLConnection conn,
                                                                 java.lang.String[] textFields,
                                                                 java.lang.String[] values,
                                                                 java.lang.String[] fileFields,
                                                                 java.lang.String[] filePaths,
                                                                 java.lang.String[] dataFields,
                                                                 byte[][] data)
                                                          throws java.lang.Exception
        Call the HTTP request and disconnect with multi-part data
        Parameters:
        conn - HTTP connection
        textFields - field names to send
        values - field values
        fileFields - file names to send
        filePaths - file local paths
        dataFields - data names to send
        data - bytes to send
        Returns:
        Array with the response and the full cookie
        Throws:
        java.lang.Exception - when status not equals to HTTP 200
      • getHeaderField

        public static java.lang.String getHeaderField​(java.net.HttpURLConnection conn,
                                                      java.lang.String name)
        Get a connection header field
        Parameters:
        conn - HTTP connection
        name - Header field name
        Returns:
        field value or null if not found
      • getCookie

        @Deprecated
        public static java.lang.String getCookie​(java.net.HttpURLConnection conn)
        Deprecated.
        Use getCookies
      • getCookies

        public static java.lang.String getCookies​(java.net.HttpURLConnection conn)
        Get a connection's cookies
        Parameters:
        conn - HTTP connection
        Returns:
        cookie value or null
      • cleanURI

        public static java.lang.String cleanURI​(java.lang.String uri)
        Get clean URI
        Parameters:
        uri - URI to clean
      • split

        public static java.lang.String[] split​(java.lang.String uri)
        Split a cleaned URI into parts (removes the context path)
        Parameters:
        uri - URI to split