Skip to content

Commit 8e55709

Browse files
committed
Add additional handler extension callbacks
Implements AsyncHttpClient#732 for 1.9.x.
1 parent e86d1f4 commit 8e55709

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

src/main/java/com/ning/http/client/AsyncHandlerExtensions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ public interface AsyncHandlerExtensions {
5959
* Notify the callback every time a request is being retried.
6060
*/
6161
void onRetry();
62+
63+
/**
64+
* Notify the callback after DNS resolution has completed.
65+
*/
66+
void onDnsResolved();
67+
68+
/**
69+
* Notify the callback when the SSL handshake performed to establish an HTTPS connection has been completed.
70+
*/
71+
void onSslHandshakeCompleted();
6272
}

src/main/java/com/ning/http/client/providers/netty/request/NettyConnectListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424

25+
import com.ning.http.client.AsyncHandler;
2526
import com.ning.http.client.AsyncHandlerExtensions;
2627
import com.ning.http.client.AsyncHttpClientConfig;
2728
import com.ning.http.client.providers.netty.channel.ChannelManager;
@@ -71,7 +72,7 @@ private void abortChannelPreemption(String poolKey) {
7172
if (channelPreempted)
7273
channelManager.abortChannelPreemption(poolKey);
7374
}
74-
75+
7576
private void writeRequest(Channel channel, String poolKey) {
7677

7778
LOGGER.debug("Request using non cached Channel '{}':\n{}\n", channel, future.getNettyRequest().getHttpRequest());
@@ -108,6 +109,10 @@ public void operationComplete(ChannelFuture handshakeFuture) throws Exception {
108109
LOGGER.debug("onFutureSuccess: session = {}, id = {}, isValid = {}, host = {}", session.toString(),
109110
Base64.encode(session.getId()), session.isValid(), host);
110111
if (hostnameVerifier.verify(host, session)) {
112+
final AsyncHandler<T> asyncHandler = future.getAsyncHandler();
113+
if (asyncHandler instanceof AsyncHandlerExtensions)
114+
AsyncHandlerExtensions.class.cast(asyncHandler).onSslHandshakeCompleted();
115+
111116
writeRequest(channel, poolKey);
112117
} else {
113118
abortChannelPreemption(poolKey);

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestSender.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private <T> ListenableFuture<T> sendRequestWithNewChannel(//
250250
HttpMethod method = future.getNettyRequest().getHttpRequest().getMethod();
251251
requestFactory.addAuthorizationHeader(headers, requestFactory.firstRequestOnlyAuthorizationHeader(request, uri, proxy, realm));
252252
requestFactory.setProxyAuthorizationHeader(headers, requestFactory.firstRequestOnlyProxyAuthorizationHeader(request, proxy, method));
253-
253+
254254
// Do not throw an exception when we need an extra connection for a
255255
// redirect
256256
// FIXME why? This violate the max connection per host handling, right?
@@ -276,7 +276,7 @@ private <T> ListenableFuture<T> sendRequestWithNewChannel(//
276276
if (asyncHandler instanceof AsyncHandlerExtensions)
277277
AsyncHandlerExtensions.class.cast(asyncHandler).onOpenConnection();
278278

279-
ChannelFuture channelFuture = connect(request, uri, proxy, useProxy, bootstrap);
279+
ChannelFuture channelFuture = connect(request, uri, proxy, useProxy, bootstrap, asyncHandler);
280280
channelFuture.addListener(new NettyConnectListener<T>(config, future, this, channelManager, channelPreempted, poolKey));
281281

282282
} catch (Throwable t) {
@@ -308,17 +308,17 @@ private <T> NettyResponseFuture<T> newNettyResponseFuture(Uri uri, Request reque
308308
}
309309

310310
public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
311-
311+
312312
NettyRequest nettyRequest = future.getNettyRequest();
313313
HttpRequest httpRequest = nettyRequest.getHttpRequest();
314314
AsyncHandler<T> handler = future.getAsyncHandler();
315-
315+
316316
// if the channel is dead because it was pooled and the remote
317317
// server decided to close it,
318318
// we just let it go and the channelInactive do its work
319319
if (!Channels.isChannelValid(channel))
320320
return;
321-
321+
322322
try {
323323
if (handler instanceof TransferCompletionHandler)
324324
configureTransferAdapter(handler, httpRequest);
@@ -362,9 +362,12 @@ else if (!useProxy || avoidProxy(proxy, uri.getHost()))
362362
return new InetSocketAddress(proxy.getHost(), proxy.getPort());
363363
}
364364

365-
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap) {
365+
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) {
366366
InetSocketAddress remoteAddress = remoteAddress(request, uri, proxy, useProxy);
367367

368+
if (asyncHandler instanceof AsyncHandlerExtensions)
369+
AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved();
370+
368371
if (request.getLocalAddress() != null)
369372
return bootstrap.connect(remoteAddress, new InetSocketAddress(request.getLocalAddress(), 0));
370373
else
@@ -484,7 +487,7 @@ private boolean validateWebSocketRequest(Request request, AsyncHandler<?> asyncH
484487
}
485488

486489
public Channel pollAndVerifyCachedChannel(Uri uri, ProxyServer proxy, ConnectionPoolPartitioning connectionPoolPartitioning, AsyncHandler<?> asyncHandler) {
487-
490+
488491
if (asyncHandler instanceof AsyncHandlerExtensions)
489492
AsyncHandlerExtensions.class.cast(asyncHandler).onPoolConnection();
490493

src/test/java/com/ning/http/client/async/EventCollectingHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ public void onSendRequest(Object request) {
8686
public void onRetry() {
8787
firedEvents.add("Retry");
8888
}
89+
90+
@Override
91+
public void onDnsResolved() {
92+
firedEvents.add("DnsResolved");
93+
}
94+
95+
@Override
96+
public void onSslHandshakeCompleted() {
97+
firedEvents.add("SslHandshakeCompleted");
98+
}
8999
}

src/test/java/com/ning/http/client/async/netty/NettyAsyncProviderBasicTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void testNewConnectionEventsFired() throws InterruptedException, TimeoutE
6363
List<String> expectedEvents = Arrays.asList(
6464
"PoolConnection",
6565
"OpenConnection",
66+
"DnsResolved",
6667
"ConnectionOpen",
6768
"SendRequest",
6869
"HeaderWriteCompleted",

src/test/java/com/ning/http/client/async/netty/NettyBasicHttpsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public void testNormalEventsFired() throws InterruptedException, TimeoutExceptio
4242
List<String> expectedEvents = Arrays.asList(
4343
"PoolConnection",
4444
"OpenConnection",
45+
"DnsResolved",
46+
"SslHandshakeCompleted",
4547
"ConnectionOpen",
4648
"SendRequest",
4749
"HeaderWriteCompleted",

0 commit comments

Comments
 (0)