Class HttpConnectionUtils


  • public final class HttpConnectionUtils
    extends java.lang.Object
    Utility collection for http connections.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean downloadBinaryFile​(java.net.URL fileURL, java.util.Map<java.lang.String,​java.lang.String> headers, java.util.Set<java.lang.String> expectedMime, java.io.File saveFilePath)
      Downloads a binary file from a URL.
      static void downloadZipFileAndExtract​(java.net.URL url, java.io.File dir, java.util.List<java.lang.String> expectedFiles)
      Download Zip file from remote site and extract it to specified directory.
      static java.lang.String get​(java.lang.String address, java.util.Map<java.lang.String,​java.lang.String> params, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
      Get data from the remote URL.
      static java.lang.String get​(java.lang.String address, java.util.Map<java.lang.String,​java.lang.String> params, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.lang.String defaultOutputCharset)
      Get data from the remote URL.
      static java.lang.String getURL​(java.net.URL url)
      Get resource from URL with default timeout.
      static java.lang.String getURL​(java.net.URL url, int timeout)
      Download a file to memory.
      static byte[] getURLasByteArray​(java.lang.String target)
      Obtain byte array context from remote URL.
      static java.lang.String post​(java.lang.String address, java.util.Map<java.lang.String,​java.lang.String> params)
      Method call without additional headers for possible calls from plugins.
      static java.lang.String post​(java.lang.String address, java.util.Map<java.lang.String,​java.lang.String> params, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
      Post data to the remote URL.
      static java.lang.String postJSON​(java.lang.String address, java.lang.String json, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
      Post JSON data to the remote URL.
      • Methods inherited from class java.lang.Object

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

      • getURL

        public static java.lang.String getURL​(java.net.URL url)
                                       throws java.io.IOException
        Get resource from URL with default timeout.
        Parameters:
        url - resource URL.
        Returns:
        string returned from server.
        Throws:
        java.io.IOException - raises when connection is failed.
      • getURL

        public static java.lang.String getURL​(java.net.URL url,
                                              int timeout)
                                       throws java.io.IOException
        Download a file to memory.
        Parameters:
        url - resource URL to download
        timeout - timeout to connect and read.
        Returns:
        returned string
        Throws:
        java.io.IOException - when connection and read method error.
      • downloadZipFileAndExtract

        public static void downloadZipFileAndExtract​(java.net.URL url,
                                                     java.io.File dir,
                                                     java.util.List<java.lang.String> expectedFiles)
                                              throws java.io.IOException
        Download Zip file from remote site and extract it to specified directory.
        Parameters:
        url - URL of zip file resource to download.
        dir - target directory to extract
        expectedFiles - filter extract file names
        Throws:
        java.io.IOException - raises when extraction is failed, maybe flaky download happened.
      • downloadBinaryFile

        public static boolean downloadBinaryFile​(java.net.URL fileURL,
                                                 java.util.Map<java.lang.String,​java.lang.String> headers,
                                                 java.util.Set<java.lang.String> expectedMime,
                                                 java.io.File saveFilePath)
                                          throws java.io.IOException,
                                                 HttpConnectionUtils.FlakyDownloadException
        Downloads a binary file from a URL.
        Parameters:
        fileURL - HTTP URL of the file to be downloaded
        headers - Additional HTTP headers
        expectedMime - Mime type expected and check against such as ["application/octet-stream", "application/jar-archive"]. If getting type is differed, return false.
        saveFilePath - path of the file
        Returns:
        true when succeeded, otherwise false.
        Throws:
        java.io.IOException - raise when connection and file write failed.
        HttpConnectionUtils.FlakyDownloadException - raise when downloaded file length differs from expected content length.
      • getURLasByteArray

        public static byte[] getURLasByteArray​(java.lang.String target)
                                        throws java.io.IOException
        Obtain byte array context from remote URL.
        Parameters:
        target - String representation of well-formed URL.
        Returns:
        byte array or null if status is not 200 OK
        Throws:
        java.io.IOException - raise when connection failed.
      • post

        public static java.lang.String post​(java.lang.String address,
                                            java.util.Map<java.lang.String,​java.lang.String> params)
                                     throws java.io.IOException
        Method call without additional headers for possible calls from plugins.
        Parameters:
        address - URL to post
        params - post parameters in Map
        Returns:
        result string returned from server.
        Throws:
        java.io.IOException - raises when connection failed.
      • get

        public static java.lang.String get​(java.lang.String address,
                                           java.util.Map<java.lang.String,​java.lang.String> params,
                                           java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
                                    throws java.io.IOException
        Get data from the remote URL.
        Parameters:
        address - address to post
        params - parameters
        additionalHeaders - additional headers for request, can be null
        Returns:
        server output
        Throws:
        java.io.IOException
      • get

        public static java.lang.String get​(java.lang.String address,
                                           java.util.Map<java.lang.String,​java.lang.String> params,
                                           java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                                           java.lang.String defaultOutputCharset)
                                    throws java.io.IOException
        Get data from the remote URL.
        Parameters:
        address - address to post
        params - parameters
        additionalHeaders - additional headers for request, can be null
        defaultOutputCharset - default charset used to interpret the response
        Returns:
        server output
        Throws:
        java.io.IOException
      • post

        public static java.lang.String post​(java.lang.String address,
                                            java.util.Map<java.lang.String,​java.lang.String> params,
                                            java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
                                     throws java.io.IOException
        Post data to the remote URL.
        Parameters:
        address - address to post
        params - parameters
        additionalHeaders - additional headers for request, can be null
        Returns:
        Server output
        Throws:
        java.io.IOException
      • postJSON

        public static java.lang.String postJSON​(java.lang.String address,
                                                java.lang.String json,
                                                java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
                                         throws java.io.IOException
        Post JSON data to the remote URL.
        Parameters:
        address - address to post
        json - JSON-encoded data
        additionalHeaders - additional headers for request, can be null
        Returns:
        Server output
        Throws:
        java.io.IOException