Package com.google.cloud.tools.jib.http
Class FailoverHttpClient
- java.lang.Object
-
- com.google.cloud.tools.jib.http.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 returnedResponse
.The failover (if enabled) in the following way:
- When a port is provided (for example
my-registry:5000/my-repo
):- Attempts secure HTTPS on the specified port.
- If (1) fails due to
SSLException
, re-attempts secure HTTPS on the specified port but disabling certificate validation. - 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
):- Attempts secure HTTPS on port 443 (default HTTPS port).
- If (1) fails due to
SSLException
, re-attempts secure HTTPS on port 443 but disabling certificate validation. - If (2) fails again due to
SSLException
, attempts plain-HTTP on port 80 (default HTTP port). - 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
- When a port is provided (for example
-
-
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.
-
-
-
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 typessendAuthorizationOverHttp
- allow sending auth over http connectionslogger
- 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 URLrequest
- 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 URLrequest
- 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 URLrequest
- 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 methodurl
- endpoint URLrequest
- 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()
-
-