Class FailoverHttpClient


  • public class FailoverHttpClient
    extends java.lang.Object
    Thread-safe HTTP client that can automatically failover from secure HTTPS to insecure HTTPS or HTTP. Intended to be created once and shared to be called at multiple places. Callers should close the returned Response.

    The failover (if enabled) in the following way:

    • When a port is provided (for example my-registry:5000/my-repo):
      1. Attempts secure HTTPS on the specified port.
      2. If (1) fails due to SSLException, re-attempts secure HTTPS on the specified port but disabling certificate validation.
      3. If (2) fails again due to SSLException, attempts plain-HTTP on the specified port.
    • When a port is not provided (for example my-registry/my-repo):
      1. Attempts secure HTTPS on port 443 (default HTTPS port).
      2. If (1) fails due to SSLException, re-attempts secure HTTPS on port 443 but disabling certificate validation.
      3. If (2) fails again due to SSLException, attempts plain-HTTP on port 80 (default HTTP port).
      4. Or, if (1) fails due to non-timeout ConnectException, attempts plain-HTTP on port 80.

    This failover behavior is similar to how the Docker client works: https://docs.docker.com/registry/insecure/#deploy-a-plain-http-registry

    • Constructor Summary

      Constructors 
      Constructor Description
      FailoverHttpClient​(boolean enableHttpAndInsecureFailover, boolean sendAuthorizationOverHttp, java.util.function.Consumer<LogEvent> logger)
      Create a new FailoverHttpclient.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Response call​(java.lang.String httpMethod, java.net.URL url, Request request)
      Sends the request.
      Response get​(java.net.URL url, Request request)
      Sends the request with method GET.
      java.util.Deque<com.google.api.client.http.HttpTransport> getTransportsCreated()  
      Response post​(java.net.URL url, Request request)
      Sends the request with method POST.
      Response put​(java.net.URL url, Request request)
      Sends the request with method PUT.
      void shutDown()
      Closes all connections and allocated resources, whether they are currently used or not.
      • Methods inherited from class java.lang.Object

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

      • FailoverHttpClient

        public FailoverHttpClient​(boolean enableHttpAndInsecureFailover,
                                  boolean sendAuthorizationOverHttp,
                                  java.util.function.Consumer<LogEvent> logger)
        Create a new FailoverHttpclient.
        Parameters:
        enableHttpAndInsecureFailover - to enable automatic failover to insecure connection types
        sendAuthorizationOverHttp - allow sending auth over http connections
        logger - to receive log events
    • Method Detail

      • shutDown

        public void shutDown()
                      throws java.io.IOException
        Closes all connections and allocated resources, whether they are currently used or not.

        If an I/O error occurs, shutdown attempts stop immediately, resulting in partial resource release up to that point. The method can be called again later to re-attempt releasing all resources.

        Throws:
        java.io.IOException - when I/O error shutting down resources
      • get

        public Response get​(java.net.URL url,
                            Request request)
                     throws java.io.IOException
        Sends the request with method GET.
        Parameters:
        url - endpoint URL
        request - the request to send
        Returns:
        the response to the sent request
        Throws:
        java.io.IOException - if sending the request fails
      • post

        public Response post​(java.net.URL url,
                             Request request)
                      throws java.io.IOException
        Sends the request with method POST.
        Parameters:
        url - endpoint URL
        request - the request to send
        Returns:
        the response to the sent request
        Throws:
        java.io.IOException - if sending the request fails
      • put

        public Response put​(java.net.URL url,
                            Request request)
                     throws java.io.IOException
        Sends the request with method PUT.
        Parameters:
        url - endpoint URL
        request - the request to send
        Returns:
        the response to the sent request
        Throws:
        java.io.IOException - if sending the request fails
      • call

        public Response call​(java.lang.String httpMethod,
                             java.net.URL url,
                             Request request)
                      throws java.io.IOException
        Sends the request.
        Parameters:
        httpMethod - the HTTP request method
        url - endpoint URL
        request - the request to send
        Returns:
        the response to the sent request
        Throws:
        java.io.IOException - if building the HTTP request fails.
      • getTransportsCreated

        public java.util.Deque<com.google.api.client.http.HttpTransport> getTransportsCreated()