From ff340b3aec5193b066b3cf6ee6cbc9c542de48df Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Wed, 3 Apr 2024 18:40:59 +0200 Subject: fix(http): include retry on HTTP POST requests Reason: that feature looks be removed during refactoring from Apache HTTP Client v4.x to 5.x --- .../impl/http/EaafHttpRequestRetryHandler.java | 37 ++++++++++++++++++++++ .../core/impl/http/HttpClientConfiguration.java | 3 ++ .../eaaf/core/impl/http/HttpClientFactory.java | 7 +++- 3 files changed, 46 insertions(+), 1 deletion(-) (limited to 'eaaf_core_utils/src/main/java/at') diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/EaafHttpRequestRetryHandler.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/EaafHttpRequestRetryHandler.java index 026b76c4..6ea13905 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/EaafHttpRequestRetryHandler.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/EaafHttpRequestRetryHandler.java @@ -7,11 +7,15 @@ import javax.net.ssl.SSLException; import org.apache.hc.client5.http.HttpRequestRetryStrategy; import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.Method; import org.apache.hc.core5.util.TimeValue; public class EaafHttpRequestRetryHandler extends DefaultHttpRequestRetryStrategy implements HttpRequestRetryStrategy { + private boolean allowHttpPostRetry; + /** * Create the request retry handler using the following list of non-retriable. * IOException classes:
@@ -25,15 +29,48 @@ public class EaafHttpRequestRetryHandler extends DefaultHttpRequestRetryStrategy *
  • 502
  • * * After two seconds if no {@code Retry-After} header was set. + *

    + * Do not allow retry of HTTP POST + *

    * * @param retryCount how many times to retry; 0 means no retries */ public EaafHttpRequestRetryHandler(final int retryCount) { + this(retryCount, false); + + } + + /** + * Create the request retry handler using the following list of non-retriable. + * IOException classes:
    + * + * HTTP StatusCodes: + * + * After two seconds if no {@code Retry-After} header was set. + * + * @param retryCount how many times to retry; 0 means no retries + * @param retryHttpPost In case of true allow retry of HTTP POST + * too + */ + public EaafHttpRequestRetryHandler(final int retryCount, final boolean retryHttpPost) { super(retryCount, TimeValue.ofSeconds(2), Arrays.asList( UnknownHostException.class, SSLException.class), Arrays.asList(429, 502)); + allowHttpPostRetry = retryHttpPost; + + } + + protected boolean handleAsIdempotent(final HttpRequest request) { + return allowHttpPostRetry && Method.POST.isSame(request.getMethod()) + || Method.isIdempotent(request.getMethod()); } diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientConfiguration.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientConfiguration.java index 2081bd24..f978fa4c 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientConfiguration.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientConfiguration.java @@ -66,6 +66,9 @@ public class HttpClientConfiguration { @Setter private boolean followHttpRedirects = true; + @Setter + private boolean httpPostRetryAllowed = false; + @Setter private int httpErrorRetryCount = 3; diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientFactory.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientFactory.java index 62e781b9..7a71bfab 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientFactory.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpClientFactory.java @@ -81,6 +81,8 @@ public class HttpClientFactory implements IHttpClientFactory { "client.http.connection.timeout.request"; public static final String PROP_CONFIG_CLIENT_HTTP_CONNECTION_RETRY_COUNT = "client.http.connection.retry.count"; + public static final String PROP_CONFIG_CLIENT_HTTP_CONNECTION_RETRY_POST = + "client.http.connection.retry.post"; public static final String PROP_CONFIG_CLIENT_HTTP_SSL_HOSTNAMEVERIFIER_TRUSTALL = "client.http.ssl.hostnameverifier.trustall"; @@ -210,7 +212,8 @@ public class HttpClientFactory implements IHttpClientFactory { log.info("Set HTTP error-retry to {} for http-client: {}", config.getHttpErrorRetryCount(), config.getFriendlyName()); builder.setRetryStrategy(new EaafHttpRequestRetryHandler( - config.getHttpErrorRetryCount())); + config.getHttpErrorRetryCount(), + config.isHttpPostRetryAllowed())); } else { log.info("Disable HTTP error-retry for http-client: {}", config.getFriendlyName()); @@ -309,6 +312,8 @@ public class HttpClientFactory implements IHttpClientFactory { config.setHttpErrorRetryCount(Integer.parseInt(basicConfig.getBasicConfiguration( PROP_CONFIG_CLIENT_HTTP_CONNECTION_RETRY_COUNT, DEFAULT_CONFIG_CLIENT_HTTP_CONNECTION_RETRY_COUNT))); + config.setHttpPostRetryAllowed(basicConfig.getBasicConfigurationBoolean( + PROP_CONFIG_CLIENT_HTTP_CONNECTION_RETRY_POST, false)); config.setEnableHttpProxyMode(basicConfig.getBasicConfigurationBoolean( PROP_CONFIG_CLIENT_HTTP_PROXY_ENABLED, false)); -- cgit v1.2.3