Search in sources :

Example 1 with NettyConnectListener

use of org.asynchttpclient.netty.channel.NettyConnectListener in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method sendRequestWithNewChannel.

private <//
T> //
ListenableFuture<T> sendRequestWithNewChannel(//
Request request, //
ProxyServer proxy, //
NettyResponseFuture<T> future, //
AsyncHandler<T> asyncHandler, boolean performingNextRequest) {
    // some headers are only set when performing the first request
    HttpHeaders headers = future.getNettyRequest().getHttpRequest().headers();
    Realm realm = future.getRealm();
    Realm proxyRealm = future.getProxyRealm();
    requestFactory.addAuthorizationHeader(headers, perConnectionAuthorizationHeader(request, proxy, realm));
    requestFactory.setProxyAuthorizationHeader(headers, perConnectionProxyAuthorizationHeader(request, proxyRealm));
    future.setInAuth(realm != null && realm.isUsePreemptiveAuth() && realm.getScheme() != AuthScheme.NTLM);
    future.setInProxyAuth(proxyRealm != null && proxyRealm.isUsePreemptiveAuth() && proxyRealm.getScheme() != AuthScheme.NTLM);
    // Do not throw an exception when we need an extra connection for a redirect
    // FIXME why? This violate the max connection per host handling, right?
    Bootstrap bootstrap = channelManager.getBootstrap(request.getUri(), proxy);
    Object partitionKey = future.getPartitionKey();
    // we disable channelPreemption when performing next requests
    final boolean acquireChannelLock = !performingNextRequest;
    try {
        // redirect.
        if (acquireChannelLock) {
            // if there's an exception here, channel wasn't preempted and resolve won't happen
            channelManager.acquireChannelLock(partitionKey);
        }
    } catch (Throwable t) {
        abort(null, future, getCause(t));
        // exit and don't try to resolve address
        return future;
    }
    scheduleRequestTimeout(future);
    //
    RequestHostnameResolver.INSTANCE.resolve(request, proxy, asyncHandler).addListener(new SimpleFutureListener<List<InetSocketAddress>>() {

        @Override
        protected void onSuccess(List<InetSocketAddress> addresses) {
            NettyConnectListener<T> connectListener = new NettyConnectListener<>(future, NettyRequestSender.this, channelManager, acquireChannelLock, partitionKey);
            NettyChannelConnector connector = new NettyChannelConnector(request.getLocalAddress(), addresses, asyncHandler, clientState, config);
            if (!future.isDone()) {
                connector.connect(bootstrap, connectListener);
            } else if (acquireChannelLock) {
                channelManager.releaseChannelLock(partitionKey);
            }
        }

        @Override
        protected void onFailure(Throwable cause) {
            if (acquireChannelLock) {
                channelManager.releaseChannelLock(partitionKey);
            }
            abort(null, future, getCause(cause));
        }
    });
    return future;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NettyConnectListener(org.asynchttpclient.netty.channel.NettyConnectListener) Realm(org.asynchttpclient.Realm)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Realm (org.asynchttpclient.Realm)1 NettyConnectListener (org.asynchttpclient.netty.channel.NettyConnectListener)1