14
14
package org .asynchttpclient .netty .request ;
15
15
16
16
import static io .netty .handler .codec .http .HttpHeaderNames .EXPECT ;
17
+ import static org .asynchttpclient .handler .AsyncHandlerExtensionsUtils .toAsyncHandlerExtensions ;
17
18
import static org .asynchttpclient .util .Assertions .assertNotNull ;
18
19
import static org .asynchttpclient .util .AuthenticatorUtils .*;
19
20
import static org .asynchttpclient .util .HttpConstants .Methods .*;
@@ -209,17 +210,25 @@ private <T> NettyResponseFuture<T> newNettyRequestAndResponseFuture(final Reques
209
210
}
210
211
211
212
private Channel getOpenChannel (NettyResponseFuture <?> future , Request request , ProxyServer proxyServer , AsyncHandler <?> asyncHandler ) {
212
-
213
- if (future != null && future .isReuseChannel () && Channels .isChannelValid (future .channel ()))
213
+ if (future != null && future .isReuseChannel () && Channels .isChannelValid (future .channel ())) {
214
214
return future .channel ();
215
- else
215
+ } else {
216
216
return pollPooledChannel (request , proxyServer , asyncHandler );
217
+ }
217
218
}
218
219
219
220
private <T > ListenableFuture <T > sendRequestWithOpenChannel (Request request , ProxyServer proxy , NettyResponseFuture <T > future , AsyncHandler <T > asyncHandler , Channel channel ) {
220
221
221
- if (asyncHandler instanceof AsyncHandlerExtensions )
222
- AsyncHandlerExtensions .class .cast (asyncHandler ).onConnectionPooled (channel );
222
+ final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions (asyncHandler );
223
+ if (asyncHandlerExtensions != null ) {
224
+ try {
225
+ asyncHandlerExtensions .onConnectionPooled (channel );
226
+ } catch (Exception e ) {
227
+ LOGGER .error ("onConnectionPooled crashed" , e );
228
+ abort (channel , future , e );
229
+ return future ;
230
+ }
231
+ }
223
232
224
233
TimeoutsHolder timeoutsHolder = scheduleRequestTimeout (future );
225
234
timeoutsHolder .initRemoteAddress ((InetSocketAddress ) channel .remoteAddress ());
@@ -291,8 +300,7 @@ private <T> ListenableFuture<T> sendRequestWithNewChannel(//
291
300
292
301
@ Override
293
302
protected void onSuccess (List <InetSocketAddress > addresses ) {
294
- NettyConnectListener <T > connectListener = new NettyConnectListener <>(
295
- future , NettyRequestSender .this , channelManager , connectionSemaphore , partitionKey );
303
+ NettyConnectListener <T > connectListener = new NettyConnectListener <>(future , NettyRequestSender .this , channelManager , connectionSemaphore , partitionKey );
296
304
NettyChannelConnector connector = new NettyChannelConnector (request .getLocalAddress (), addresses , asyncHandler , clientState , config );
297
305
if (!future .isDone ()) {
298
306
connector .connect (bootstrap , connectListener );
@@ -338,14 +346,22 @@ public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
338
346
return ;
339
347
340
348
try {
341
- if (handler instanceof TransferCompletionHandler )
349
+ if (handler instanceof TransferCompletionHandler ) {
342
350
configureTransferAdapter (handler , httpRequest );
351
+ }
343
352
344
353
boolean writeBody = !future .isDontWriteBodyBecauseExpectContinue () && httpRequest .method () != HttpMethod .CONNECT && nettyRequest .getBody () != null ;
345
354
346
355
if (!future .isHeadersAlreadyWrittenOnContinue ()) {
347
- if (handler instanceof AsyncHandlerExtensions ) {
348
- AsyncHandlerExtensions .class .cast (handler ).onRequestSend (nettyRequest );
356
+ final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions (handler );
357
+ if (asyncHandlerExtensions != null ) {
358
+ try {
359
+ asyncHandlerExtensions .onRequestSend (nettyRequest );
360
+ } catch (Exception e ) {
361
+ LOGGER .error ("onRequestSend crashed" , e );
362
+ abort (channel , future , e );
363
+ return ;
364
+ }
349
365
}
350
366
351
367
// if the request has a body, we want to track progress
@@ -365,8 +381,9 @@ public <T> void writeRequest(NettyResponseFuture<T> future, Channel channel) {
365
381
nettyRequest .getBody ().write (channel , future );
366
382
367
383
// don't bother scheduling read timeout if channel became invalid
368
- if (Channels .isChannelValid (channel ))
384
+ if (Channels .isChannelValid (channel )) {
369
385
scheduleReadTimeout (future );
386
+ }
370
387
371
388
} catch (Exception e ) {
372
389
LOGGER .error ("Can't write request" , e );
@@ -398,8 +415,9 @@ private void scheduleReadTimeout(NettyResponseFuture<?> nettyResponseFuture) {
398
415
399
416
public void abort (Channel channel , NettyResponseFuture <?> future , Throwable t ) {
400
417
401
- if (channel != null )
418
+ if (channel != null ) {
402
419
channelManager .closeChannel (channel );
420
+ }
403
421
404
422
if (!future .isDone ()) {
405
423
future .setChannelState (ChannelState .CLOSED );
@@ -423,15 +441,23 @@ public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture<?
423
441
424
442
public boolean retry (NettyResponseFuture <?> future ) {
425
443
426
- if (isClosed ())
444
+ if (isClosed ()) {
427
445
return false ;
446
+ }
428
447
429
448
if (future .isReplayPossible ()) {
430
449
future .setChannelState (ChannelState .RECONNECTED );
431
450
432
451
LOGGER .debug ("Trying to recover request {}\n " , future .getNettyRequest ().getHttpRequest ());
433
- if (future .getAsyncHandler () instanceof AsyncHandlerExtensions ) {
434
- AsyncHandlerExtensions .class .cast (future .getAsyncHandler ()).onRetry ();
452
+ final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions (future .getAsyncHandler ());
453
+ if (asyncHandlerExtensions != null ) {
454
+ try {
455
+ asyncHandlerExtensions .onRetry ();
456
+ } catch (Exception e ) {
457
+ LOGGER .error ("onRetry crashed" , e );
458
+ abort (future .channel (), future , e );
459
+ return false ;
460
+ }
435
461
}
436
462
437
463
try {
@@ -478,19 +504,26 @@ private void validateWebSocketRequest(Request request, AsyncHandler<?> asyncHand
478
504
Uri uri = request .getUri ();
479
505
boolean isWs = uri .isWebSocket ();
480
506
if (asyncHandler instanceof WebSocketUpgradeHandler ) {
481
- if (!isWs )
507
+ if (!isWs ) {
482
508
throw new IllegalArgumentException ("WebSocketUpgradeHandler but scheme isn't ws or wss: " + uri .getScheme ());
483
- else if (!request .getMethod ().equals (GET ) && !request .getMethod ().equals (CONNECT ))
509
+ } else if (!request .getMethod ().equals (GET ) && !request .getMethod ().equals (CONNECT )) {
484
510
throw new IllegalArgumentException ("WebSocketUpgradeHandler but method isn't GET or CONNECT: " + request .getMethod ());
511
+ }
485
512
} else if (isWs ) {
486
513
throw new IllegalArgumentException ("No WebSocketUpgradeHandler but scheme is " + uri .getScheme ());
487
514
}
488
515
}
489
516
490
517
private Channel pollPooledChannel (Request request , ProxyServer proxy , AsyncHandler <?> asyncHandler ) {
491
518
492
- if (asyncHandler instanceof AsyncHandlerExtensions )
493
- AsyncHandlerExtensions .class .cast (asyncHandler ).onConnectionPoolAttempt ();
519
+ final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions (asyncHandler );
520
+ if (asyncHandlerExtensions != null ) {
521
+ try {
522
+ asyncHandlerExtensions .onConnectionPoolAttempt ();
523
+ } catch (Exception e ) {
524
+ LOGGER .error ("onConnectionPoolAttempt crashed" , e );
525
+ }
526
+ }
494
527
495
528
Uri uri = request .getUri ();
496
529
String virtualHost = request .getVirtualHost ();
@@ -511,8 +544,16 @@ public void replayRequest(final NettyResponseFuture<?> future, FilterContext fc,
511
544
future .touch ();
512
545
513
546
LOGGER .debug ("\n \n Replaying Request {}\n for Future {}\n " , newRequest , future );
514
- if (future .getAsyncHandler () instanceof AsyncHandlerExtensions )
515
- AsyncHandlerExtensions .class .cast (future .getAsyncHandler ()).onRetry ();
547
+ final AsyncHandlerExtensions asyncHandlerExtensions = toAsyncHandlerExtensions (future .getAsyncHandler ());
548
+ if (asyncHandlerExtensions != null ) {
549
+ try {
550
+ asyncHandlerExtensions .onRetry ();
551
+ } catch (Exception e ) {
552
+ LOGGER .error ("onRetry crashed" , e );
553
+ abort (channel , future , e );
554
+ return ;
555
+ }
556
+ }
516
557
517
558
channelManager .drainChannelAndOffer (channel , future );
518
559
sendNextRequest (newRequest , future );
0 commit comments