Search in sources :

Example 1 with ReactiveStreamsBodyGenerator

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

the class NettyRequestFactory method body.

private NettyBody body(Request request, boolean connect) {
    NettyBody nettyBody = null;
    if (!connect) {
        Charset bodyCharset = withDefault(request.getCharset(), DEFAULT_CHARSET);
        if (request.getByteData() != null) {
            nettyBody = new NettyByteArrayBody(request.getByteData());
        } else if (request.getCompositeByteData() != null) {
            nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
        } else if (request.getStringData() != null) {
            nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
        } else if (request.getByteBufferData() != null) {
            nettyBody = new NettyByteBufferBody(request.getByteBufferData());
        } else if (request.getStreamData() != null) {
            nettyBody = new NettyInputStreamBody(request.getStreamData());
        } else if (isNonEmpty(request.getFormParams())) {
            CharSequence contentType = null;
            if (!request.getHeaders().contains(CONTENT_TYPE)) {
                contentType = HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
            }
            nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
        } else if (isNonEmpty(request.getBodyParts())) {
            nettyBody = new NettyMultipartBody(request.getBodyParts(), request.getHeaders(), config);
        } else if (request.getFile() != null) {
            nettyBody = new NettyFileBody(request.getFile(), config);
        } else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
            FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator();
            nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), config);
        } else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {
            InputStreamBodyGenerator inStreamGenerator = InputStreamBodyGenerator.class.cast(request.getBodyGenerator());
            nettyBody = new NettyInputStreamBody(inStreamGenerator.getInputStream(), inStreamGenerator.getContentLength());
        } else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {
            ReactiveStreamsBodyGenerator reactiveStreamsBodyGenerator = (ReactiveStreamsBodyGenerator) request.getBodyGenerator();
            nettyBody = new NettyReactiveStreamsBody(reactiveStreamsBodyGenerator.getPublisher(), reactiveStreamsBodyGenerator.getContentLength());
        } else if (request.getBodyGenerator() != null) {
            nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), config);
        }
    }
    return nettyBody;
}
Also used : InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) NettyMultipartBody(org.asynchttpclient.netty.request.body.NettyMultipartBody) NettyInputStreamBody(org.asynchttpclient.netty.request.body.NettyInputStreamBody) Charset(java.nio.charset.Charset) NettyCompositeByteArrayBody(org.asynchttpclient.netty.request.body.NettyCompositeByteArrayBody) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) NettyByteBufferBody(org.asynchttpclient.netty.request.body.NettyByteBufferBody) NettyByteArrayBody(org.asynchttpclient.netty.request.body.NettyByteArrayBody) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) NettyBodyBody(org.asynchttpclient.netty.request.body.NettyBodyBody) NettyBody(org.asynchttpclient.netty.request.body.NettyBody) NettyFileBody(org.asynchttpclient.netty.request.body.NettyFileBody) NettyReactiveStreamsBody(org.asynchttpclient.netty.request.body.NettyReactiveStreamsBody)

Example 2 with ReactiveStreamsBodyGenerator

use of org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator 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)

Aggregations

ReactiveStreamsBodyGenerator (org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator)2 ChannelProgressiveFuture (io.netty.channel.ChannelProgressiveFuture)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 Charset (java.nio.charset.Charset)1 WriteProgressListener (org.asynchttpclient.netty.request.WriteProgressListener)1 NettyBody (org.asynchttpclient.netty.request.body.NettyBody)1 NettyBodyBody (org.asynchttpclient.netty.request.body.NettyBodyBody)1 NettyByteArrayBody (org.asynchttpclient.netty.request.body.NettyByteArrayBody)1 NettyByteBufferBody (org.asynchttpclient.netty.request.body.NettyByteBufferBody)1 NettyCompositeByteArrayBody (org.asynchttpclient.netty.request.body.NettyCompositeByteArrayBody)1 NettyFileBody (org.asynchttpclient.netty.request.body.NettyFileBody)1 NettyInputStreamBody (org.asynchttpclient.netty.request.body.NettyInputStreamBody)1 NettyMultipartBody (org.asynchttpclient.netty.request.body.NettyMultipartBody)1 NettyReactiveStreamsBody (org.asynchttpclient.netty.request.body.NettyReactiveStreamsBody)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 FileBodyGenerator (org.asynchttpclient.request.body.generator.FileBodyGenerator)1 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)1