wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else if (ioe != null && shouldContinue(command, ioe)) { continue; cleanup(nativeRequest);
boolean shouldContinue(HttpCommand command, IOException response) { // Even though Java does not want to handle it this way, // treat a Protocol Exception on PUT with 100-Continue as a case of Unauthorized (and attempt to retry) if (command.getCurrentRequest().getMethod().equals("PUT") && command.getCurrentRequest().getHeaders().containsEntry("Expect", "100-continue") && response instanceof ProtocolException && response.getMessage().equals("Server rejected operation") ) { logger.debug("Caught a protocol exception on a 100-continue PUT request. Attempting to retry."); return isIdempotent(command) && retryHandler.shouldRetryRequest(command, HttpResponse.builder().statusCode(401).message("Unauthorized").build()); } return isIdempotent(command) && ioRetryHandler.shouldRetryRequest(command, response); }
public void testStreamIsClosedWhenRetrying() throws IOException { MockInputStream in = new MockInputStream(2); // Input stream that produces 2 bytes HttpResponse response = HttpResponse.builder().payload(newInputStreamPayload(in)).build(); response.getPayload().getContentMetadata().setContentLength(1L); HttpCommand command = mockHttpCommand(); DelegatingRetryHandler retryHandler = EasyMock.createMock(DelegatingRetryHandler.class); DelegatingErrorHandler errorHandler = EasyMock.createMock(DelegatingErrorHandler.class); expect(retryHandler.shouldRetryRequest(command, response)).andReturn(true); replay(retryHandler, errorHandler); // Verify the stream is open. This consumes one byte. assertEquals(response.getPayload().openStream().available(), 2); assertEquals(response.getPayload().openStream().read(), 1); BaseHttpCommandExecutorService<?> service = mockHttpCommandExecutorService(retryHandler, errorHandler); assertTrue(service.shouldContinue(command, response)); verify(retryHandler, errorHandler); // Verify that the response stream is closed and consumed assertFalse(in.isOpen); assertTrue(response.getPayload().openStream() == in); // The service shouldn't have changed it assertEquals(response.getPayload().openStream().available(), 0); assertEquals(response.getPayload().openStream().read(), -1); }
private void helperRetryOnlyIdempotent(String method) throws IOException { final IOException error = new IOException("test exception"); HttpRequestFilter throwingFilter = new HttpRequestFilter() { @Override public HttpRequest filter(HttpRequest request) throws HttpException { throw new HttpException(error); } }; HttpCommand command = new HttpCommand(HttpRequest.builder().endpoint("http://localhost").method(method).filter(throwingFilter).build()); IOExceptionRetryHandler ioRetryHandler = EasyMock.createMock(IOExceptionRetryHandler.class); if ("GET".equals(method)) { expect(ioRetryHandler.shouldRetryRequest(command, error)).andReturn(true); expect(ioRetryHandler.shouldRetryRequest(command, error)).andReturn(false); } replay(ioRetryHandler); BaseHttpCommandExecutorService<?> service = mockHttpCommandExecutorService(ioRetryHandler); try { service.invoke(command); fail("Expected to fail due to throwing filter"); } catch (Exception e) {} verify(ioRetryHandler); }
public void testStreamIsClosedWhenNotRetrying() throws IOException { MockInputStream in = new MockInputStream(2); // Input stream that produces 2 bytes HttpResponse response = HttpResponse.builder().payload(newInputStreamPayload(in)).build(); response.getPayload().getContentMetadata().setContentLength(1L); HttpCommand command = mockHttpCommand(); DelegatingRetryHandler retryHandler = EasyMock.createMock(DelegatingRetryHandler.class); DelegatingErrorHandler errorHandler = EasyMock.createMock(DelegatingErrorHandler.class); errorHandler.handleError(command, response); expectLastCall(); expect(retryHandler.shouldRetryRequest(command, response)).andReturn(false); replay(retryHandler, errorHandler); // Verify the stream is open. This consumes one byte. assertEquals(response.getPayload().openStream().available(), 2); assertEquals(response.getPayload().openStream().read(), 1); BaseHttpCommandExecutorService<?> service = mockHttpCommandExecutorService(retryHandler, errorHandler); assertFalse(service.shouldContinue(command, response)); verify(retryHandler, errorHandler); // Verify that the response stream is closed assertFalse(in.isOpen); assertTrue(response.getPayload().openStream() == in); assertEquals(response.getPayload().openStream().available(), 0); assertEquals(response.getPayload().openStream().read(), -1); }
wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else if (ioe != null && shouldContinue(command, ioe)) { continue; cleanup(nativeRequest);
assertFalse(service.shouldContinue(command, response));
boolean shouldContinue(HttpCommand command, IOException response) { // Even though Java does not want to handle it this way, // treat a Protocol Exception on PUT with 100-Continue as a case of Unauthorized (and attempt to retry) if (command.getCurrentRequest().getMethod().equals("PUT") && command.getCurrentRequest().getHeaders().containsEntry("Expect", "100-continue") && response instanceof ProtocolException && response.getMessage().equals("Server rejected operation") ) { logger.debug("Caught a protocol exception on a 100-continue PUT request. Attempting to retry."); return isIdempotent(command) && retryHandler.shouldRetryRequest(command, HttpResponse.builder().statusCode(401).message("Unauthorized").build()); } return isIdempotent(command) && ioRetryHandler.shouldRetryRequest(command, response); }
wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else if (ioe != null && shouldContinue(command, ioe)) { continue; cleanup(nativeRequest);
assertFalse(service.shouldContinue(command, response));
boolean shouldContinue(HttpCommand command, IOException response) { // Even though Java does not want to handle it this way, // treat a Protocol Exception on PUT with 100-Continue as a case of Unauthorized (and attempt to retry) if (command.getCurrentRequest().getMethod().equals("PUT") && command.getCurrentRequest().getHeaders().containsEntry("Expect", "100-continue") && response instanceof ProtocolException && response.getMessage().equals("Server rejected operation") ) { logger.debug("Caught a protocol exception on a 100-continue PUT request. Attempting to retry."); return isIdempotent(command) && retryHandler.shouldRetryRequest(command, HttpResponse.builder().statusCode(401).message("Unauthorized").build()); } return isIdempotent(command) && ioRetryHandler.shouldRetryRequest(command, response); }
wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else cleanup(nativeRequest);
wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else cleanup(nativeRequest);
wirePayloadIfEnabled(wire, request); utils.logRequest(headerLog, request, ">>"); nativeRequest = convert(request); response = invoke(nativeRequest); if (shouldContinue(command, response)) continue; else cleanup(nativeRequest);