diff --git a/open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd/NvdApiRetryStrategy.java b/open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd/NvdApiRetryStrategy.java index 245ccbcd..5d49fb95 100644 --- a/open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd/NvdApiRetryStrategy.java +++ b/open-vulnerability-clients/src/main/java/io/github/jeremylong/openvulnerability/client/nvd/NvdApiRetryStrategy.java @@ -27,6 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; import java.io.IOException; import java.time.Instant; import java.util.ArrayList; @@ -53,33 +54,45 @@ public class NvdApiRetryStrategy extends DefaultHttpRequestRetryStrategy { */ private final long delay; + private static final String RETRY_MESSAGE_TEMPLATE = "NVD API request failures are occurring; retrying request for the {} time"; + public NvdApiRetryStrategy(int maxRetries, long delay) { - super(maxRetries, TimeValue.of(delay, TimeUnit.MILLISECONDS), new ArrayList>(), + super(maxRetries, TimeValue.of(delay, TimeUnit.MILLISECONDS), new ArrayList<>(), Arrays.asList(429, 502, 503, 504)); this.maxRetries = maxRetries; this.delay = delay; } @Override - public boolean retryRequest(HttpRequest request, IOException exception, int execCount, HttpContext context) { - if (execCount >= (maxRetries / 2)) { - LOG.warn("NVD API request failures are occurring; retrying request for the {} time", execCount); - } else if (execCount > 1) { - LOG.debug("Retrying request {} : {} time", request.getRequestUri(), execCount); - } + public boolean retryRequest(@Nonnull HttpRequest request, @Nonnull IOException exception, int execCount, HttpContext context) { + logRetryState(request, exception, execCount); return super.retryRequest(request, exception, execCount, context); } @Override public boolean retryRequest(HttpResponse response, int execCount, HttpContext context) { if (execCount >= (maxRetries / 2)) { - LOG.warn("NVD API request failures are occurring; retrying request for the {} time", execCount); + LOG.warn(RETRY_MESSAGE_TEMPLATE, execCount); } else if (execCount > 1) { LOG.debug("Retrying request {} time", execCount); } return super.retryRequest(response, execCount, context); } + private void logRetryState(@Nonnull HttpRequest request, @Nonnull IOException exception, int execCount) { + if (execCount >= (maxRetries / 2)) { + LOG.warn(RETRY_MESSAGE_TEMPLATE, execCount); + if (LOG.isDebugEnabled()) { + LOG.debug("NVD API request failures with exception : {}. Message: {}", exception.getClass().getName(), exception.getMessage()); + } + } else if (execCount > 1) { + LOG.warn("Retrying request {} : {} time", request.getRequestUri(), execCount); + if (LOG.isDebugEnabled()) { + LOG.debug("Retrying request with exception {}. Message: {}", exception.getClass().getName(), exception.getMessage()); + } + } + } + @Override public TimeValue getRetryInterval(final HttpResponse response, final int execCount, final HttpContext context) { TimeValue value;