Class Downloader


  • public final class Downloader
    extends java.lang.Object
    A Utility class to centralize download logic like HTTP(S) proxy configuration and proxy- and server credential handling.
    Author:
    Jeremy Long, Hans Aikema
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void configure​(Settings settings)
      Initialize the Downloader from the settings.
      <T> T fetchAndHandle​(@NotNull java.net.URL url, @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler)
      Download a resource from the given URL and have its content handled by the given ResponseHandler.
      <T> T fetchAndHandle​(@NotNull java.net.URL url, @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler, @NotNull java.util.List<org.apache.hc.core5.http.Header> hdr)
      Download a resource from the given URL and have its content handled by the given ResponseHandler.
      <T> T fetchAndHandle​(@NotNull java.net.URL url, @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler, @NotNull java.util.List<org.apache.hc.core5.http.Header> hdr, boolean useProxy)
      Download a resource from the given URL and have its content handled by the given ResponseHandler.
      java.lang.String fetchContent​(java.net.URL url, boolean useProxy, java.nio.charset.Charset charset)
      Retrieves a file from a given URL and returns the contents.
      java.lang.String fetchContent​(java.net.URL url, java.nio.charset.Charset charset)
      Retrieves a file from a given URL and returns the contents.
      void fetchFile​(java.net.URL url, java.io.File outputPath)
      Retrieves a file from a given URL and saves it to the outputPath.
      void fetchFile​(java.net.URL url, java.io.File outputPath, boolean useProxy)
      Retrieves a file from a given URL and saves it to the outputPath.
      void fetchFile​(java.net.URL url, java.io.File outputPath, boolean useProxy, java.lang.String userKey, java.lang.String passwordKey)
      Retrieves a file from a given URL using an ad-hoc created CredentialsProvider if needed and saves it to the outputPath.
      static Downloader getInstance()
      The singleton instance for downloading file resources.
      java.lang.String postBasedFetchContent​(java.net.URI url, java.lang.String payload, org.apache.hc.core5.http.ContentType payloadType, java.util.List<org.apache.hc.core5.http.Header> hdr)
      Posts a payload to the URL and returns the response as a string.
      • Methods inherited from class java.lang.Object

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

      • getInstance

        public static Downloader getInstance()
        The singleton instance for downloading file resources.
        Returns:
        The singleton instance managing download credentials and proxy configuration
      • configure

        public void configure​(Settings settings)
                       throws InvalidSettingException
        Initialize the Downloader from the settings. Extracts the configured proxy- and credential information from the settings and system properties and caches those for future use by the Downloader.
        Parameters:
        settings - The settings to configure from
        Throws:
        InvalidSettingException - When improper configurations are found.
      • fetchFile

        public void fetchFile​(java.net.URL url,
                              java.io.File outputPath,
                              boolean useProxy,
                              java.lang.String userKey,
                              java.lang.String passwordKey)
                       throws DownloadFailedException,
                              TooManyRequestsException,
                              ResourceNotFoundException,
                              URLConnectionFailureException
        Retrieves a file from a given URL using an ad-hoc created CredentialsProvider if needed and saves it to the outputPath.
        Parameters:
        url - the URL of the file to download
        outputPath - the path to the save the file to
        useProxy - whether to use the configured proxy when downloading files
        userKey - the settings key for the username to be used
        passwordKey - the settings key for the password to be used
        Throws:
        DownloadFailedException - is thrown if there is an error downloading the file
        URLConnectionFailureException - is thrown when certificate-chain trust errors occur downloading the file
        TooManyRequestsException - thrown when a 429 is received
        ResourceNotFoundException - thrown when a 404 is received
        Implementation Note:
        This method should only be used in cases where the target host cannot be determined beforehand from settings, so that ad-hoc Credentials needs to be constructed for the target URL when the user/password keys point to configured credentials. The method delegates to fetchFile(URL, File, boolean) when credentials are not configured for the given keys or the resource points to a file.
      • fetchAndHandle

        public <T> T fetchAndHandle​(@NotNull
                                    @NotNull java.net.URL url,
                                    @NotNull
                                    @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler)
                             throws java.io.IOException,
                                    TooManyRequestsException,
                                    ResourceNotFoundException
        Download a resource from the given URL and have its content handled by the given ResponseHandler.
        Type Parameters:
        T - The return-type for the responseHandler
        Parameters:
        url - The url of the resource
        handler - The responsehandler to handle the response
        Returns:
        The response handler result
        Throws:
        java.io.IOException - on I/O Exceptions
        TooManyRequestsException - When HTTP status 429 is encountered
        ResourceNotFoundException - When HTTP status 404 is encountered
      • fetchAndHandle

        public <T> T fetchAndHandle​(@NotNull
                                    @NotNull java.net.URL url,
                                    @NotNull
                                    @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler,
                                    @NotNull
                                    @NotNull java.util.List<org.apache.hc.core5.http.Header> hdr)
                             throws java.io.IOException,
                                    TooManyRequestsException,
                                    ResourceNotFoundException
        Download a resource from the given URL and have its content handled by the given ResponseHandler.
        Type Parameters:
        T - The return-type for the responseHandler
        Parameters:
        url - The url of the resource
        handler - The responsehandler to handle the response
        hdr - Additional headers to add to the HTTP request
        Returns:
        The response handler result
        Throws:
        java.io.IOException - on I/O Exceptions
        TooManyRequestsException - When HTTP status 429 is encountered
        ResourceNotFoundException - When HTTP status 404 is encountered
      • fetchAndHandle

        public <T> T fetchAndHandle​(@NotNull
                                    @NotNull java.net.URL url,
                                    @NotNull
                                    @NotNull org.apache.hc.core5.http.io.HttpClientResponseHandler<T> handler,
                                    @NotNull
                                    @NotNull java.util.List<org.apache.hc.core5.http.Header> hdr,
                                    boolean useProxy)
                             throws java.io.IOException,
                                    TooManyRequestsException,
                                    ResourceNotFoundException
        Download a resource from the given URL and have its content handled by the given ResponseHandler.
        Type Parameters:
        T - The return-type for the responseHandler
        Parameters:
        url - The url of the resource
        handler - The responsehandler to handle the response
        hdr - Additional headers to add to the HTTP request
        useProxy - Whether to use the configured proxy for the connection
        Returns:
        The response handler result
        Throws:
        java.io.IOException - on I/O Exceptions
        TooManyRequestsException - When HTTP status 429 is encountered
        ResourceNotFoundException - When HTTP status 404 is encountered