RedirectionRetryHandler implements HttpRetryHandler { public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { closeClientButKeepContentStream(response); if (!command.isReplayable()) { logger.error("Cannot retry after redirect, command is not replayable: %s", command); return false; } if (command.incrementRedirectCount() > retryCountLimit) { logger.error("Cannot retry after redirect, command exceeded retry limit %d: %s", retryCountLimit, command); return false; } String location = response.getFirstHeaderOrNull(LOCATION); if (location == null) { logger.error("Cannot retry after redirect, no host header: %s", command); return false; } HttpRequest current = command.getCurrentRequest(); URI redirect = URI.create(location); if (!redirect.isAbsolute()) { if (redirect.getPath() == null) { logger.error("Cannot retry after redirect, no path in location header %s", command); return false; } redirect = uriBuilder(current.getEndpoint()).path(redirect.getPath()).query(redirect.getQuery()).build(); } if (redirect.equals(current.getEndpoint())) { backoffHandler.imposeBackoffExponentialDelay(command.getRedirectCount(), "redirect: " + command.toString()); } else if (current.getFirstHeaderOrNull(HOST) != null && redirect.getHost() != null) { String host = redirect.getPort() > 0 ? redirect.getHost() + ":" + redirect.getPort() : redirect.getHost(); command.setCurrentRequest(current.toBuilder().replaceHeader(HOST, host).endpoint(redirect).build()); } else { command.setCurrentRequest(current.toBuilder().endpoint(redirect).build()); } return true; } @Inject protected  RedirectionRetryHandler(BackoffLimitedRetryHandler backoffHandler); boolean shouldRetryRequest(HttpCommand command, HttpResponse response);  }
@Test public void test302DoesNotRetry() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(0); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }
@Test public void test302DoesNotRetryAfterLimit() { HttpCommand command = createMock(HttpCommand.class); HttpResponse response = HttpResponse.builder() .statusCode(302) .message("HTTP/1.1 302 Found") .addHeader(LOCATION, "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645").build(); expect(command.isReplayable()).andReturn(true); expect(command.incrementRedirectCount()).andReturn(6); replay(command); RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class); assert !retry.shouldRetryRequest(command, response); verify(command); }