@Override public long getContentLength() { return delegate.getContentLength(); }
private void callBodyProducerHandleError(BodyProducer bodyProducer, @Nullable Throwable failureCause) { try { bodyProducer.handleError(failureCause); } catch (Throwable t) { LOG.warn("Exception raised from BodyProducer.handleError() for {}", bodyProducer, t); } }
@Override public ByteBuf nextChunk() throws Exception { return delegate.nextChunk(); }
@Override public void sendContent(HttpResponseStatus status, BodyProducer bodyProducer, HttpHeaders headers) { // Buffer all contents produced by the body producer ByteBuf contentChunks = Unpooled.EMPTY_BUFFER; try { ByteBuf chunk = bodyProducer.nextChunk(); while (chunk.isReadable()) { contentChunks = Unpooled.wrappedBuffer(contentChunks, chunk); chunk = bodyProducer.nextChunk(); } bodyProducer.finished(); final ByteBuf finalContentChunks = contentChunks; response = new AbstractInternalResponse(status.code()) { @Override public InputStream openInputStream() throws IOException { return new ByteBufInputStream(finalContentChunks.duplicate()); } }; } catch (Throwable t) { try { bodyProducer.handleError(t); } catch (Throwable et) { LOG.warn("Exception raised from BodyProducer.handleError() for {}", bodyProducer, et); } } }
final long contentLength; try { contentLength = bodyProducer.getContentLength(); } catch (Throwable t) { bodyProducer.handleError(t);
@Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { callBodyProducerHandleError(bodyProducer, future.cause()); channel.close(); return; } try { bodyProducer.finished(); } catch (Throwable t) { callBodyProducerHandleError(bodyProducer, t); channel.close(); } } };
@Override public void finished() throws Exception { try { delegate.finished(); } finally { taskExecutor.releaseCallResources(); } }
@Override public void handleError(@Nullable Throwable cause) { try { delegate.handleError(cause); } finally { taskExecutor.releaseCallResources(); } } }
@Override public boolean isEndOfInput() throws Exception { if (completed) { return true; } if (nextChunk == null) { nextChunk = bodyProducer.nextChunk(); } completed = !nextChunk.isReadable(); if (completed && length >= 0 && bytesProduced != length) { throw new IllegalStateException("Body size doesn't match with content length. " + "Content-Length: " + length + ", bytes produced: " + bytesProduced); } return completed; }