Search in sources :

Example 1 with FeedableBodyGenerator

use of org.asynchttpclient.request.body.generator.FeedableBodyGenerator in project async-http-client by AsyncHttpClient.

the class NettyBodyBody method write.

@Override
public void write(final Channel channel, NettyResponseFuture<?> future) throws IOException {
    Object msg;
    if (body instanceof RandomAccessBody && !ChannelManager.isSslHandlerConfigured(channel.pipeline()) && !config.isDisableZeroCopy()) {
        msg = new BodyFileRegion((RandomAccessBody) body);
    } else {
        msg = new BodyChunkedInput(body);
        BodyGenerator bg = future.getTargetRequest().getBodyGenerator();
        if (bg instanceof FeedableBodyGenerator && !(bg instanceof ReactiveStreamsBodyGenerator)) {
            final ChunkedWriteHandler chunkedWriteHandler = channel.pipeline().get(ChunkedWriteHandler.class);
            FeedableBodyGenerator.class.cast(bg).setListener(new FeedListener() {

                @Override
                public void onContentAdded() {
                    chunkedWriteHandler.resumeTransfer();
                }

                @Override
                public void onError(Throwable t) {
                }
            });
        }
    }
    //
    channel.write(msg, channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, getContentLength()) {

        public void operationComplete(ChannelProgressiveFuture cf) {
            closeSilently(body);
            super.operationComplete(cf);
        }
    });
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) FeedListener(org.asynchttpclient.request.body.generator.FeedListener) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) BodyGenerator(org.asynchttpclient.request.body.generator.BodyGenerator) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) RandomAccessBody(org.asynchttpclient.request.body.RandomAccessBody)

Example 2 with FeedableBodyGenerator

use of org.asynchttpclient.request.body.generator.FeedableBodyGenerator in project async-http-client by AsyncHttpClient.

the class ChunkingTest method doTestWithFeedableBodyGenerator.

public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
        final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
        Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
        ListenableFuture<Response> responseFuture = c.executeRequest(r);
        feed(feedableBodyGenerator, is);
        waitForAndAssertResponse(responseFuture);
    }
}
Also used : UnboundedQueueFeedableBodyGenerator(org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator) Response(org.asynchttpclient.Response) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) UnboundedQueueFeedableBodyGenerator(org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator) Request(org.asynchttpclient.Request) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Aggregations

FeedableBodyGenerator (org.asynchttpclient.request.body.generator.FeedableBodyGenerator)2 ChannelProgressiveFuture (io.netty.channel.ChannelProgressiveFuture)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)1 Request (org.asynchttpclient.Request)1 Response (org.asynchttpclient.Response)1 WriteProgressListener (org.asynchttpclient.netty.request.WriteProgressListener)1 RandomAccessBody (org.asynchttpclient.request.body.RandomAccessBody)1 BodyGenerator (org.asynchttpclient.request.body.generator.BodyGenerator)1 FeedListener (org.asynchttpclient.request.body.generator.FeedListener)1 ReactiveStreamsBodyGenerator (org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator)1 UnboundedQueueFeedableBodyGenerator (org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator)1