[java-idp-integration-tests] branch main updated: Updates for HttpClient 5.x
Tom Zeller
tzeller at dragonacea.biz
Tue Mar 14 23:43:39 UTC 2023
This is an automated email from the git hooks/post-receive script.
tzeller pushed a commit to branch main
in repository java-idp-integration-tests.
View the commit online:
http://git.shibboleth.net/view/?p=java-idp-integration-tests.git;a=commit;h=a150a22a5a8d82e963b9a2bb5dda3cd8efdf0c33
The following commit(s) were added to refs/heads/main by this push:
new a150a22 Updates for HttpClient 5.x
a150a22 is described below
commit a150a22a5a8d82e963b9a2bb5dda3cd8efdf0c33
Author: Tom Zeller <tzeller at dragonacea.biz>
AuthorDate: Tue Mar 14 18:43:29 2023 -0500
Updates for HttpClient 5.x
Combine old retry handlers into single less-configurable retry strategy
with 1 second interval.
https://shibboleth.atlassian.net/browse/JSSH-16
---
.../integration/tests/AbstractServerProcess.java | 113 ++++++++++-----------
.../idp/integration/tests/BaseIntegrationTest.java | 3 +-
2 files changed, 52 insertions(+), 64 deletions(-)
diff --git a/src/test/java/net/shibboleth/idp/integration/tests/AbstractServerProcess.java b/src/test/java/net/shibboleth/idp/integration/tests/AbstractServerProcess.java
index 9491794..5069d36 100644
--- a/src/test/java/net/shibboleth/idp/integration/tests/AbstractServerProcess.java
+++ b/src/test/java/net/shibboleth/idp/integration/tests/AbstractServerProcess.java
@@ -26,17 +26,24 @@ import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.util.TimeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.Lifecycle;
@@ -353,8 +360,7 @@ public class AbstractServerProcess extends AbstractInitializableComponent implem
final HttpClientBuilder builder = new HttpClientBuilder();
if (retries > 1) {
- builder.setHttpRequestRetryHandler(new FiniteWaitHttpRequestRetryHandler(retries, millis));
- builder.setServiceUnavailableRetryHandler(new FiniteWaitServiceUnavailableRetryStrategy(retries, millis));
+ builder.setHttpRequestRetryStrategy(new FiniteWaitRetryStrategy(retries));
}
builder.setSocketTimeout(Duration.ofMillis(millis));
@@ -366,7 +372,7 @@ public class AbstractServerProcess extends AbstractInitializableComponent implem
final HttpClient httpClient = builder.buildClient();
final HttpGet httpget = new HttpGet(statusPageURL);
- final ClassicHttpResponse response = httpClient.executeOpen(httpget);
+ final ClassicHttpResponse response = httpClient.executeOpen(null, httpget, null);
log.trace("Status page response '{}'", response);
try {
@@ -389,85 +395,68 @@ public class AbstractServerProcess extends AbstractInitializableComponent implem
}
/**
- * A {@link HttpRequestRetryHandler} which retries requests until a maximum number of attempts has been made and
- * which sleeps between retry attempts.
+ * Retry requests until a maximum number of attempts has been made.
*/
- public class FiniteWaitHttpRequestRetryHandler implements HttpRequestRetryHandler {
+ public class FiniteWaitRetryStrategy implements HttpRequestRetryStrategy {
+
+ /** Class logger. */
+ @Nonnull
+ private final Logger log = LoggerFactory.getLogger(FiniteWaitRetryStrategy.class);
/** Maximum number of retry attempts. */
private final int maxRetries;
- /** Length of time to sleep in milliseconds between retry attempts. */
- private final int sleepMillis;
+ /** HTTP status codes to be retried, 404 and 503. */
+ private final Set<Integer> retriableCodes = Set.of(HttpStatus.SC_NOT_FOUND, HttpStatus.SC_SERVICE_UNAVAILABLE);
/**
* Constructor.
*
- * @param retries maximum number of times to retry
- * @param millis length of time to sleep in milliseconds between retry attempts
+ * @param maxRetries
+ * maximum number of retry attempts
*/
- public FiniteWaitHttpRequestRetryHandler(@Nullable final int retries, @Nonnull final int millis) {
- maxRetries = retries;
- sleepMillis = millis;
+ public FiniteWaitRetryStrategy(@Nonnull final int maxRetries) {
+ this.maxRetries = maxRetries;
}
- /** {@inheritDoc} */
- public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
- log.trace("Request retry handler exception msg '{}'", exception.getMessage());
- log.trace("Request retry handler execution count '{}'", executionCount);
-
- if (sleepMillis > 0) {
- try {
- Thread.sleep(sleepMillis);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (executionCount <= maxRetries) {
- return true;
- }
-
- return false;
+ /**
+ * Retry upon exception until the maximum number of attempts has been made.
+ *
+ * {@inheritDoc}
+ */
+ public boolean retryRequest(final HttpRequest request, final IOException exception, final int execCount,
+ final HttpContext context) {
+ log.trace("Request retry handler execution count '{}' msg '{}'", execCount, exception.getMessage());
+ return execCount <= maxRetries;
}
- }
-
- /**
- * A {@link ServiceUnavailableRetryStrategy} which retries requests until a maximum number of attempts has been made
- * and which sleeps between retry attempts. This strategy retries response status codes of
- * {@link HttpStatus#SC_SERVICE_UNAVAILABLE} and {@link HttpStatus#SC_NOT_FOUND}.
- */
- public class FiniteWaitServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
-
- /** Maximum number of retry attempts. */
- private final int maxRetries;
-
- /** Length of time to sleep in milliseconds between retry attempts. */
- private final int sleepMillis;
/**
- * Constructor.
- *
- * @param retries maximum number of times to retry
- * @param retryInterval length of time to sleep in milliseconds between retry attempts
+ * Retry 404 or 503 response codes until the maximum number of attempts has been
+ * made.
+ *
+ * {@inheritDoc}
*/
- public FiniteWaitServiceUnavailableRetryStrategy(int retries, int retryInterval) {
- maxRetries = retries;
- sleepMillis = retryInterval;
+ public boolean retryRequest(final HttpResponse response, final int execCount, final HttpContext context) {
+ return execCount <= this.maxRetries && retriableCodes.contains(response.getCode());
}
- @Override
- public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) {
- log.trace("Service unavailable retry strategy response '{}'", response);
- log.trace("Service unavailable retry strategy execution count '{}'", executionCount);
- return executionCount <= maxRetries
- && (response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE
- || response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);
+ /**
+ * 1 second retry interval for 404 or 503 response codes.
+ *
+ * {@inheritDoc}
+ */
+ public TimeValue getRetryInterval(final HttpResponse response, final int execCount, final HttpContext context) {
+ return TimeValue.ofSeconds(1L);
}
- /** {@inheritDoc} */
- public long getRetryInterval() {
- return sleepMillis;
+ /**
+ * 1 second retry interval upon exception.
+ *
+ * {@inheritDoc}
+ */
+ public TimeValue getRetryInterval(final HttpRequest request, final IOException exception, final int execCount,
+ HttpContext context) {
+ return TimeValue.ofSeconds(1L);
}
}
diff --git a/src/test/java/net/shibboleth/idp/integration/tests/BaseIntegrationTest.java b/src/test/java/net/shibboleth/idp/integration/tests/BaseIntegrationTest.java
index 1d6fd8f..959b492 100644
--- a/src/test/java/net/shibboleth/idp/integration/tests/BaseIntegrationTest.java
+++ b/src/test/java/net/shibboleth/idp/integration/tests/BaseIntegrationTest.java
@@ -2150,8 +2150,7 @@ public abstract class BaseIntegrationTest {
final HttpClientBuilder builder = new HttpClientBuilder();
try {
final HttpClient httpClient = builder.buildClient();
- // TODO fix up open call...
- final ClassicHttpResponse response = httpClient.executeOpen(httpget);
+ final ClassicHttpResponse response = httpClient.executeOpen(null, httpget, null);
log.trace("EC2 metadata response '{}'", response);
try {
final HttpEntity entity = response.getEntity();
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list