Search in sources :

Example 1 with ChannelProgressivePromise

use of io.netty.channel.ChannelProgressivePromise in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method writeRequest.

public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
    NettyRequest nettyRequest = future.getNettyRequest();
    HttpRequest httpRequest = nettyRequest.getHttpRequest();
    AsyncHandler<T> handler = future.getAsyncHandler();
    // we just let it go and the channelInactive do its work
    if (!Channels.isChannelValid(channel))
        return;
    try {
        if (handler instanceof TransferCompletionHandler)
            configureTransferAdapter(handler, httpRequest);
        boolean writeBody = !future.isDontWriteBodyBecauseExpectContinue() && httpRequest.method() != HttpMethod.CONNECT && nettyRequest.getBody() != null;
        if (!future.isHeadersAlreadyWrittenOnContinue()) {
            if (handler instanceof AsyncHandlerExtensions) {
                AsyncHandlerExtensions.class.cast(handler).onRequestSend(nettyRequest);
            }
            // if the request has a body, we want to track progress
            if (writeBody) {
                ChannelProgressivePromise promise = channel.newProgressivePromise();
                ChannelFuture f = channel.write(httpRequest, promise);
                f.addListener(new WriteProgressListener(future, true, 0L));
            } else {
                // we can just track write completion
                ChannelPromise promise = channel.newPromise();
                ChannelFuture f = channel.writeAndFlush(httpRequest, promise);
                f.addListener(new WriteCompleteListener(future));
            }
        }
        if (writeBody)
            nettyRequest.getBody().write(channel, future);
        // don't bother scheduling read timeout if channel became invalid
        if (Channels.isChannelValid(channel))
            scheduleReadTimeout(future);
    } catch (Exception e) {
        LOGGER.error("Can't write request", e);
        abort(channel, future, e);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler) ChannelPromise(io.netty.channel.ChannelPromise) AsyncHandlerExtensions(org.asynchttpclient.handler.AsyncHandlerExtensions) FilterException(org.asynchttpclient.filter.FilterException) IOException(java.io.IOException) RemotelyClosedException(org.asynchttpclient.exception.RemotelyClosedException) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) ChannelProgressivePromise(io.netty.channel.ChannelProgressivePromise)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelProgressivePromise (io.netty.channel.ChannelProgressivePromise)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EXPECT (io.netty.handler.codec.http.HttpHeaderNames.EXPECT)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 IOException (java.io.IOException)1 RemotelyClosedException (org.asynchttpclient.exception.RemotelyClosedException)1 FilterException (org.asynchttpclient.filter.FilterException)1 AsyncHandlerExtensions (org.asynchttpclient.handler.AsyncHandlerExtensions)1 TransferCompletionHandler (org.asynchttpclient.handler.TransferCompletionHandler)1