From edfe56160855162d8d70ddb398c79428fac10743 Mon Sep 17 00:00:00 2001 From: Mauro Mugnaini Date: Wed, 6 Mar 2024 12:09:09 +0100 Subject: [PATCH] Added hooks methods for connection and read timeouts override --- ...stractHTTPWithJWTTokenAuthEventSender.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java b/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java index b1a8f7e..121ecc1 100644 --- a/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java +++ b/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java @@ -24,6 +24,8 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe protected String clientId; protected String clientSecret; protected URL tokenURL; + protected int connectionTimeout; + protected int readTimeout; public AbstractHTTPWithJWTTokenAuthEventSender(URL baseEndpointURL, String clientId, String clientSecret, URL tokenURL) { @@ -33,6 +35,16 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe this.clientId = clientId; this.clientSecret = clientSecret; this.tokenURL = tokenURL; + connectionTimeout = getConnectionTimeout(); + readTimeout = getReadTimeout(); + } + + protected int getReadTimeout() { + return HTTPVerb.DEFAULT_READ_TIMEOUT; + } + + protected int getConnectionTimeout() { + return HTTPVerb.DEFAULT_CONNECTION_TIMEOUT; } @Override @@ -69,8 +81,8 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe public abstract class HTTPVerb { - protected static final int CONNECTION_TIMEOUT = 10000; - protected static final int READ_TIMEOUT = 5000; + protected static final int DEFAULT_CONNECTION_TIMEOUT = 10000; + protected static final int DEFAULT_READ_TIMEOUT = 5000; protected URL baseEndpoint; @@ -118,9 +130,9 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe log.debug("Performing HTTP POST to: {}", baseEndpoint); HttpURLConnection connection = (HttpURLConnection) eventEndpoint.openConnection(); connection.setRequestMethod("POST"); - connection.setConnectTimeout(CONNECTION_TIMEOUT); + connection.setConnectTimeout(connectionTimeout); log.trace("HTTP connection timeout set to: {}", connection.getConnectTimeout()); - connection.setReadTimeout(READ_TIMEOUT); + connection.setReadTimeout(readTimeout); log.trace("HTTP connection Read timeout set to: {}", connection.getReadTimeout()); connection.setRequestProperty("Content-Type", "application/json"); // Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376 @@ -233,9 +245,9 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe log.debug("Performing HTTP GET to: {}", endpoint); HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection(); connection.setRequestMethod("GET"); - connection.setConnectTimeout(CONNECTION_TIMEOUT); + connection.setConnectTimeout(connectionTimeout); log.trace("HTTP connection timeout set to: {}", connection.getConnectTimeout()); - connection.setReadTimeout(READ_TIMEOUT); + connection.setReadTimeout(readTimeout); log.trace("HTTP connection Read timeout set to: {}", connection.getReadTimeout()); if (token != null) { log.debug("Setting authorization header as: {}", token.getAccessTokenAsBearer());