Skip to content

Commit 40c2fb8

Browse files
author
Stephane Landelle
committed
Perform request in handshake listener
1 parent 4f53174 commit 40c2fb8

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

providers/netty4/src/test/java/org/asynchttpclient/providers/netty4/NettyBasicHttpsPureNettyTest.java

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -454,55 +454,64 @@ public Response get(long timeout, TimeUnit unit) throws InterruptedException, Ex
454454
}
455455
}
456456

457-
private ChannelFuture connect(Bootstrap b, String url, final ResponseFuture responseFuture) throws InterruptedException {
457+
private void connectThenPerformRequest(Bootstrap b, String url, final ResponseFuture responseFuture, final RequestPerformer requestPerformer) throws InterruptedException {
458458

459459
URI uri = URI.create(url);
460460

461461
// FIXME handle connect timeout exception
462-
return b.connect(uri.getHost(), uri.getPort()).sync().addListener(new GenericFutureListener<ChannelFuture>() {
462+
b.connect(uri.getHost(), uri.getPort()).sync().addListener(new GenericFutureListener<ChannelFuture>() {
463463
@Override
464464
public void operationComplete(ChannelFuture future) throws Exception {
465465
LOGGER.info("Connect future complete");
466466
if (!future.isSuccess()) {
467467
LOGGER.error("Connect failed", future.cause());
468468
responseFuture.set(future.cause());
469469
} else {
470-
future.channel().pipeline().get(SslHandler.class).handshakeFuture().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Channel>>() {
470+
final Channel ch = future.channel();
471+
if (!ch.isActive()) {
472+
responseFuture.set(new Exception("Channel is not active after connect"));
473+
} else if (!ch.isOpen()) {
474+
responseFuture.set(new Exception("Channel is not open after connect"));
475+
} else {
476+
LOGGER.info("Channel properly open and active, performing request");
477+
future.channel().pipeline().get(SslHandler.class).handshakeFuture().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Channel>>() {
471478

472-
@Override
473-
public void operationComplete(io.netty.util.concurrent.Future<Channel> f) throws Exception {
474-
if (!f.isSuccess()) {
475-
LOGGER.error("Handshake failed", f.cause());
476-
responseFuture.set(f.cause());
479+
@Override
480+
public void operationComplete(io.netty.util.concurrent.Future<Channel> f) throws Exception {
481+
if (!f.isSuccess()) {
482+
Throwable cause = f.cause();
483+
if (cause.getCause() != null) {
484+
cause = cause.getCause();
485+
}
486+
487+
LOGGER.error("Handshake failed", cause);
488+
responseFuture.set(f.cause());
489+
} else {
490+
MyChannelInboundHandlerAdapter.responseFutureAttr(ch.pipeline()).set(responseFuture);
491+
requestPerformer.performRequest(ch);
492+
}
477493
}
478-
}
479-
});
494+
});
495+
}
480496
}
481497
}
482498
});
483499
}
484500

485-
private ResponseFuture post(Bootstrap b, String url, final String string) throws InterruptedException {
501+
private static interface RequestPerformer {
486502

487-
URI uri = URI.create(url);
488-
final ResponseFuture responseFuture = new ResponseFuture();
489-
490-
ChannelFuture connectFuture = connect(b, url, responseFuture);
503+
void performRequest(Channel ch) throws Exception;
504+
}
491505

492-
if (!responseFuture.isDone()) {
493-
Channel ch = connectFuture.channel();
506+
private ResponseFuture post(Bootstrap b, String url, final String string) throws InterruptedException {
494507

495-
LOGGER.info("Performing request");
496-
if (!ch.isActive()) {
497-
responseFuture.set(new Exception("Channel is not active after connect"));
498-
} else if (!ch.isOpen()) {
499-
responseFuture.set(new Exception("Channel is not open after connect"));
500-
} else {
501-
LOGGER.info("Channel properly open and active");
502-
MyChannelInboundHandlerAdapter.responseFutureAttr(ch.pipeline()).set(responseFuture);
508+
final URI uri = URI.create(url);
509+
final ResponseFuture responseFuture = new ResponseFuture();
503510

511+
RequestPerformer requestPerformer = new RequestPerformer() {
512+
@Override
513+
public void performRequest(Channel ch) {
504514
byte[] bodyBytes = string.getBytes(CharsetUtil.UTF_8);
505-
506515
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getPath(), Unpooled.wrappedBuffer(bodyBytes));
507516
request.headers()/**/
508517
.set(HttpHeaders.Names.CONTENT_TYPE, "text/html")/**/
@@ -521,7 +530,9 @@ public void operationComplete(ChannelFuture future) throws Exception {
521530
}
522531
});
523532
}
524-
}
533+
};
534+
535+
connectThenPerformRequest(b, url, responseFuture, requestPerformer);
525536

526537
return responseFuture;
527538
}
@@ -549,23 +560,12 @@ public void multipleSSLRequestsTest() throws Throwable {
549560

550561
private ResponseFuture post(Bootstrap b, String url, final File file) throws Exception {
551562

552-
URI uri = URI.create(url);
553-
ResponseFuture responseFuture = new ResponseFuture();
554-
555-
ChannelFuture connectFuture = connect(b, url, responseFuture);
556-
557-
if (!responseFuture.isDone()) {
558-
Channel ch = connectFuture.channel();
559-
560-
LOGGER.info("Performing request");
561-
if (!ch.isActive()) {
562-
responseFuture.set(new Exception("Channel is not active after connect"));
563-
} else if (!ch.isOpen()) {
564-
responseFuture.set(new Exception("Channel is not open after connect"));
565-
} else {
566-
LOGGER.info("Channel properly open and active");
567-
MyChannelInboundHandlerAdapter.responseFutureAttr(ch.pipeline()).set(responseFuture);
563+
final URI uri = URI.create(url);
564+
final ResponseFuture responseFuture = new ResponseFuture();
568565

566+
RequestPerformer requestPerformer = new RequestPerformer() {
567+
@Override
568+
public void performRequest(Channel ch) throws Exception {
569569
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.getPath());
570570
request.headers()/**/
571571
.set(HttpHeaders.Names.CONTENT_TYPE, "text/html")/**/
@@ -591,7 +591,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
591591

592592
ch.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
593593
}
594-
}
594+
};
595+
596+
connectThenPerformRequest(b, url, responseFuture, requestPerformer);
597+
595598
return responseFuture;
596599
}
597600

0 commit comments

Comments
 (0)