Search in sources :

Example 1 with WriteProgressListener

use of org.asynchttpclient.netty.request.WriteProgressListener 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 WriteProgressListener

use of org.asynchttpclient.netty.request.WriteProgressListener in project async-http-client by AsyncHttpClient.

the class NettyInputStreamBody method write.

@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    final InputStream is = inputStream;
    if (future.isStreamConsumed()) {
        if (is.markSupported())
            is.reset();
        else {
            LOGGER.warn("Stream has already been consumed and cannot be reset");
            return;
        }
    } else {
        future.setStreamConsumed(true);
    }
    channel.write(new ChunkedStream(is), channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, getContentLength()) {

        public void operationComplete(ChannelProgressiveFuture cf) {
            closeSilently(is);
            super.operationComplete(cf);
        }
    });
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) InputStream(java.io.InputStream) ChunkedStream(io.netty.handler.stream.ChunkedStream) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture)

Example 3 with WriteProgressListener

use of org.asynchttpclient.netty.request.WriteProgressListener in project async-http-client by AsyncHttpClient.

the class NettyFileBody method write.

@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    @SuppressWarnings("resource") final FileChannel // Netty will close the ChunkedNioFile or the DefaultFileRegion
    fileChannel = new RandomAccessFile(file, "r").getChannel();
    Object message = //
    (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) ? new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize()) : new DefaultFileRegion(fileChannel, offset, length);
    //
    channel.write(message, channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, getContentLength()));
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) DefaultFileRegion(io.netty.channel.DefaultFileRegion)

Aggregations

WriteProgressListener (org.asynchttpclient.netty.request.WriteProgressListener)3 ChannelProgressiveFuture (io.netty.channel.ChannelProgressiveFuture)2 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 ChunkedNioFile (io.netty.handler.stream.ChunkedNioFile)1 ChunkedStream (io.netty.handler.stream.ChunkedStream)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 FileChannel (java.nio.channels.FileChannel)1 RandomAccessBody (org.asynchttpclient.request.body.RandomAccessBody)1 BodyGenerator (org.asynchttpclient.request.body.generator.BodyGenerator)1 FeedListener (org.asynchttpclient.request.body.generator.FeedListener)1 FeedableBodyGenerator (org.asynchttpclient.request.body.generator.FeedableBodyGenerator)1 ReactiveStreamsBodyGenerator (org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator)1