@Override public final State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { return emitter.isDisposed() ? disposed() : delegate().onBodyPartReceived(content); }
private void loadContent() throws ExecutionException { if (future.isDone()) { try { future.get(); } catch (InterruptedException e) { throw new RuntimeException("unreachable", e); } } // No more retry CURRENT_RETRY_UPDATER.set(this, maxRetry); if (CONTENT_PROCESSED_FIELD.getAndSet(this, 1) == 0) { try { future.complete(asyncHandler.onCompleted()); } catch (Throwable ex) { if (ON_THROWABLE_CALLED_FIELD.getAndSet(this, 1) == 0) { try { try { asyncHandler.onThrowable(ex); } catch (Throwable t) { LOGGER.debug("asyncHandler.onThrowable", t); } } finally { cancelTimeouts(); } } future.completeExceptionally(ex); } } future.getNow(null); }
private boolean abortAfterHandlingHeaders(AsyncHandler<?> handler, HttpHeaders responseHeaders) throws Exception { return !responseHeaders.isEmpty() && handler.onHeadersReceived(responseHeaders) == State.ABORT; }
private void handleChunk(HttpContent chunk, final Channel channel, final NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception { boolean abort = false; boolean last = chunk instanceof LastHttpContent; // Netty 4: the last chunk is not empty if (last) { LastHttpContent lastChunk = (LastHttpContent) chunk; HttpHeaders trailingHeaders = lastChunk.trailingHeaders(); if (!trailingHeaders.isEmpty()) { abort = handler.onTrailingHeadersReceived(trailingHeaders) == State.ABORT; } } ByteBuf buf = chunk.content(); if (!abort && !(handler instanceof StreamedAsyncHandler) && (buf.isReadable() || last)) { HttpResponseBodyPart bodyPart = config.getResponseBodyPartFactory().newResponseBodyPart(buf, last); abort = handler.onBodyPartReceived(bodyPart) == State.ABORT; } if (abort || last) { boolean close = abort || !future.isKeepAlive(); finishUpdate(future, channel, close); } }
@Override public void onThrowable(Throwable t) { if (decoratedAsyncHandler != null) { decoratedAsyncHandler.onThrowable(t); } else { logger.debug("", t); } }
private boolean abortAfterHandlingStatus(// AsyncHandler<?> handler, NettyResponseStatus status) throws Exception { return handler.onStatusReceived(status) == State.ABORT; }
@Override public Response onCompleted() throws Exception { resumableProcessor.remove(url); resumableListener.onAllBytesReceived(); if (decoratedAsyncHandler != null) { decoratedAsyncHandler.onCompleted(); } // Not sure return responseBuilder.build(); }
@Override public State onTrailingHeadersReceived(HttpHeaders headers) throws Exception { return emitter.isDisposed() ? disposed() : delegate().onTrailingHeadersReceived(headers); }
public final void abort(final Throwable t) { if (terminateAndExit()) return; future.completeExceptionally(t); if (ON_THROWABLE_CALLED_FIELD.compareAndSet(this, 0, 1)) { try { asyncHandler.onThrowable(t); } catch (Throwable te) { LOGGER.debug("asyncHandler.onThrowable", te); } } }
@Override public final State onStatusReceived(HttpResponseStatus status) throws Exception { return emitter.isDisposed() ? disposed() : delegate().onStatusReceived(status); }
@Override public Void onCompleted() { if (delegateTerminated.getAndSet(true)) { return null; } final T result; try { result = delegate().onCompleted(); } catch (final Throwable t) { emitOnError(t); return null; } if (!subscriber.isUnsubscribed()) { subscriber.onSuccess(result); } return null; }
@Override public State onTrailingHeadersReceived(HttpHeaders headers) throws Exception { return subscriber.isUnsubscribed() ? abort() : delegate().onTrailingHeadersReceived(headers); }
protected AsyncHandler.State abort() { if (!delegateTerminated.getAndSet(true)) { // send a terminal event to the delegate // e.g. to trigger cleanup logic delegate().onThrowable(new UnsubscribedException()); } return State.ABORT; }
private V getContent() throws ExecutionException { if (isCancelled()) throw new CancellationException(); ExecutionException e = exEx.get(); if (e != null) throw e; V update = content.get(); // No more retry currentRetry.set(maxRetry); if (!contentProcessed.getAndSet(true)) { try { update = asyncHandler.onCompleted(); } catch (Throwable ex) { if (!onThrowableCalled.getAndSet(true)) { try { try { asyncHandler.onThrowable(ex); } catch (Throwable t) { LOGGER.debug("asyncHandler.onThrowable", t); } throw new RuntimeException(ex); } finally { cancelTimeouts(); } } } content.compareAndSet(null, update); } return update; }
@Override public State onHeadersReceived(HttpHeaders headers) throws Exception { responseBuilder.accumulate(headers); String contentLengthHeader = headers.get(CONTENT_LENGTH); if (contentLengthHeader != null) { if (Long.parseLong(contentLengthHeader) == -1L) { return AsyncHandler.State.ABORT; } } if (decoratedAsyncHandler != null) { return decoratedAsyncHandler.onHeadersReceived(headers); } return State.CONTINUE; }
@Override public State onStatusReceived(HttpResponseStatus status) throws Exception { return subscriber.isUnsubscribed() ? abort() : delegate().onStatusReceived(status); }
@Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { return subscriber.isUnsubscribed() ? abort() : delegate().onBodyPartReceived(content); }
@Test(expectedExceptions = ExecutionException.class) public void testGetThrowsExceptionThrownByAsyncHandler() throws Exception { AsyncHandler<?> asyncHandler = mock(AsyncHandler.class); when(asyncHandler.onCompleted()).thenThrow(new RuntimeException()); NettyResponseFuture<?> nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null, null); nettyResponseFuture.done(); nettyResponseFuture.get(); fail("An ExecutionException must have occurred by now as asyncHandler threw an exception in 'onCompleted'"); }
private <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> asyncHandler) { try { return requestSender.sendRequest(request, asyncHandler, null); } catch (Exception e) { asyncHandler.onThrowable(e); return new ListenableFuture.CompletedFailure<>(e); } }
private V getContent() throws ExecutionException { if (isCancelled()) throw new CancellationException(); ExecutionException e = exEx.get(); if (e != null) throw e; V update = content.get(); // No more retry currentRetry.set(maxRetry); if (!contentProcessed.getAndSet(true)) { try { update = asyncHandler.onCompleted(); } catch (Throwable ex) { if (!onThrowableCalled.getAndSet(true)) { try { try { asyncHandler.onThrowable(ex); } catch (Throwable t) { LOGGER.debug("asyncHandler.onThrowable", t); } throw new RuntimeException(ex); } finally { cancelTimeouts(); } } } content.compareAndSet(null, update); } return update; }