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;
}
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());
}
Aggregations