@@ -454,55 +454,64 @@ public Response get(long timeout, TimeUnit unit) throws InterruptedException, Ex
454
454
}
455
455
}
456
456
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 {
458
458
459
459
URI uri = URI .create (url );
460
460
461
461
// 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 >() {
463
463
@ Override
464
464
public void operationComplete (ChannelFuture future ) throws Exception {
465
465
LOGGER .info ("Connect future complete" );
466
466
if (!future .isSuccess ()) {
467
467
LOGGER .error ("Connect failed" , future .cause ());
468
468
responseFuture .set (future .cause ());
469
469
} 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 >>() {
471
478
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
+ }
477
493
}
478
- }
479
- });
494
+ });
495
+ }
480
496
}
481
497
}
482
498
});
483
499
}
484
500
485
- private ResponseFuture post ( Bootstrap b , String url , final String string ) throws InterruptedException {
501
+ private static interface RequestPerformer {
486
502
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
+ }
491
505
492
- if (!responseFuture .isDone ()) {
493
- Channel ch = connectFuture .channel ();
506
+ private ResponseFuture post (Bootstrap b , String url , final String string ) throws InterruptedException {
494
507
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 ();
503
510
511
+ RequestPerformer requestPerformer = new RequestPerformer () {
512
+ @ Override
513
+ public void performRequest (Channel ch ) {
504
514
byte [] bodyBytes = string .getBytes (CharsetUtil .UTF_8 );
505
-
506
515
HttpRequest request = new DefaultFullHttpRequest (HttpVersion .HTTP_1_1 , HttpMethod .POST , uri .getPath (), Unpooled .wrappedBuffer (bodyBytes ));
507
516
request .headers ()/**/
508
517
.set (HttpHeaders .Names .CONTENT_TYPE , "text/html" )/**/
@@ -521,7 +530,9 @@ public void operationComplete(ChannelFuture future) throws Exception {
521
530
}
522
531
});
523
532
}
524
- }
533
+ };
534
+
535
+ connectThenPerformRequest (b , url , responseFuture , requestPerformer );
525
536
526
537
return responseFuture ;
527
538
}
@@ -549,23 +560,12 @@ public void multipleSSLRequestsTest() throws Throwable {
549
560
550
561
private ResponseFuture post (Bootstrap b , String url , final File file ) throws Exception {
551
562
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 ();
568
565
566
+ RequestPerformer requestPerformer = new RequestPerformer () {
567
+ @ Override
568
+ public void performRequest (Channel ch ) throws Exception {
569
569
HttpRequest request = new DefaultHttpRequest (HttpVersion .HTTP_1_1 , HttpMethod .POST , uri .getPath ());
570
570
request .headers ()/**/
571
571
.set (HttpHeaders .Names .CONTENT_TYPE , "text/html" )/**/
@@ -591,7 +591,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
591
591
592
592
ch .writeAndFlush (LastHttpContent .EMPTY_LAST_CONTENT );
593
593
}
594
- }
594
+ };
595
+
596
+ connectThenPerformRequest (b , url , responseFuture , requestPerformer );
597
+
595
598
return responseFuture ;
596
599
}
597
600
0 commit comments