@@ -415,9 +415,6 @@ public HttpHeaders getHeaders() {
415
415
416
416
private static class DefaultResponseSpec implements ResponseSpec {
417
417
418
- private static final StatusHandler DEFAULT_STATUS_HANDLER =
419
- new StatusHandler (HttpStatus ::isError , ClientResponse ::createException );
420
-
421
418
private final Mono <ClientResponse > responseMono ;
422
419
423
420
private final Supplier <HttpRequest > requestSupplier ;
@@ -427,72 +424,95 @@ private static class DefaultResponseSpec implements ResponseSpec {
427
424
DefaultResponseSpec (Mono <ClientResponse > responseMono , Supplier <HttpRequest > requestSupplier ) {
428
425
this .responseMono = responseMono ;
429
426
this .requestSupplier = requestSupplier ;
430
- this .statusHandlers .add (DEFAULT_STATUS_HANDLER );
427
+ this .statusHandlers .add (new StatusHandler ( HttpStatus :: isError , ClientResponse :: createException ) );
431
428
}
432
429
433
430
@ Override
434
431
public ResponseSpec onStatus (Predicate <HttpStatus > statusPredicate ,
435
432
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
436
433
437
- if (this .statusHandlers .size () == 1 && this .statusHandlers .get (0 ) == DEFAULT_STATUS_HANDLER ) {
438
- this .statusHandlers .clear ();
439
- }
440
- this .statusHandlers .add (new StatusHandler (statusPredicate , exceptionFunction ));
434
+ Assert .notNull (statusPredicate , "StatusPredicate must not be null" );
435
+ Assert .notNull (exceptionFunction , "Function must not be null" );
441
436
437
+ this .statusHandlers .add (0 , new StatusHandler (statusPredicate , exceptionFunction ));
442
438
return this ;
443
439
}
444
440
445
441
@ Override
446
442
public <T > Mono <T > bodyToMono (Class <T > elementClass ) {
447
- return this . responseMono . flatMap ( response -> handleBody ( response ,
448
- response . bodyToMono ( elementClass ), mono -> mono . flatMap ( Mono :: error )));
443
+ Assert . notNull ( elementClass , "ElementClass must not be null" );
444
+ return this . responseMono . flatMap ( response -> handleBodyMono ( response , response . bodyToMono ( elementClass )));
449
445
}
450
446
451
447
@ Override
452
448
public <T > Mono <T > bodyToMono (ParameterizedTypeReference <T > elementTypeRef ) {
453
- return this .responseMono .flatMap (response ->
454
- handleBody (response , response .bodyToMono (elementTypeRef ), mono -> mono .flatMap (Mono ::error )));
449
+ Assert .notNull (elementTypeRef , "ElementTypeRef must not be null" );
450
+ return this .responseMono .flatMap (response -> handleBodyMono (response , response .bodyToMono (elementTypeRef )));
451
+ }
452
+
453
+ private <T > Mono <T > handleBodyMono (ClientResponse response , Mono <T > bodyPublisher ) {
454
+ if (HttpStatus .resolve (response .rawStatusCode ()) != null ) {
455
+ Mono <T > result = statusHandlers (response );
456
+ if (result != null ) {
457
+ return result .switchIfEmpty (bodyPublisher );
458
+ }
459
+ else {
460
+ return bodyPublisher ;
461
+ }
462
+ }
463
+ else {
464
+ return response .createException ().flatMap (Mono ::error );
465
+ }
455
466
}
456
467
457
468
@ Override
458
469
public <T > Flux <T > bodyToFlux (Class <T > elementClass ) {
459
- return this . responseMono . flatMapMany ( response ->
460
- handleBody ( response , response . bodyToFlux ( elementClass ), mono -> mono . handle (( t , sink ) -> sink . error ( t ) )));
470
+ Assert . notNull ( elementClass , "ElementClass must not be null" );
471
+ return this . responseMono . flatMapMany ( response -> handleBodyFlux ( response , response . bodyToFlux ( elementClass )));
461
472
}
462
473
463
474
@ Override
464
475
public <T > Flux <T > bodyToFlux (ParameterizedTypeReference <T > elementTypeRef ) {
465
- return this . responseMono . flatMapMany ( response ->
466
- handleBody ( response , response . bodyToFlux ( elementTypeRef ), mono -> mono . handle (( t , sink ) -> sink . error ( t ) )));
476
+ Assert . notNull ( elementTypeRef , "ElementTypeRef must not be null" );
477
+ return this . responseMono . flatMapMany ( response -> handleBodyFlux ( response , response . bodyToFlux ( elementTypeRef )));
467
478
}
468
479
469
- private <T extends Publisher <?>> T handleBody (ClientResponse response ,
470
- T bodyPublisher , Function <Mono <? extends Throwable >, T > errorFunction ) {
471
-
480
+ private <T > Publisher <T > handleBodyFlux (ClientResponse response , Flux <T > bodyPublisher ) {
472
481
if (HttpStatus .resolve (response .rawStatusCode ()) != null ) {
473
- for (StatusHandler handler : this .statusHandlers ) {
474
- if (handler .test (response .statusCode ())) {
475
- Mono <? extends Throwable > exMono ;
476
- try {
477
- exMono = handler .apply (response );
478
- exMono = exMono .flatMap (ex -> drainBody (response , ex ));
479
- exMono = exMono .onErrorResume (ex -> drainBody (response , ex ));
480
- }
481
- catch (Throwable ex2 ) {
482
- exMono = drainBody (response , ex2 );
483
- }
484
- T result = errorFunction .apply (exMono );
485
- HttpRequest request = this .requestSupplier .get ();
486
- return insertCheckpoint (result , response .statusCode (), request );
487
- }
482
+ Mono <T > result = statusHandlers (response );
483
+ if (result != null ) {
484
+ return result .flux ().switchIfEmpty (bodyPublisher );
485
+ }
486
+ else {
487
+ return bodyPublisher ;
488
488
}
489
- return bodyPublisher ;
490
489
}
491
490
else {
492
- return errorFunction . apply ( response .createException ());
491
+ return response .createException (). flatMap ( Mono :: error );
493
492
}
494
493
}
495
494
495
+ @ Nullable
496
+ private <T > Mono <T > statusHandlers (ClientResponse response ) {
497
+ for (StatusHandler handler : this .statusHandlers ) {
498
+ if (handler .test (response .statusCode ())) {
499
+ Mono <? extends Throwable > exMono ;
500
+ try {
501
+ exMono = handler .apply (response );
502
+ exMono = exMono .flatMap (ex -> drainBody (response , ex ));
503
+ exMono = exMono .onErrorResume (ex -> drainBody (response , ex ));
504
+ }
505
+ catch (Throwable ex2 ) {
506
+ exMono = drainBody (response , ex2 );
507
+ }
508
+ Mono <T > result = exMono .flatMap (Mono ::error );
509
+ HttpRequest request = this .requestSupplier .get ();
510
+ return insertCheckpoint (result , response .statusCode (), request );
511
+ }
512
+ }
513
+ return null ;
514
+ }
515
+
496
516
@ SuppressWarnings ("unchecked" )
497
517
private <T > Mono <T > drainBody (ClientResponse response , Throwable ex ) {
498
518
// Ensure the body is drained, even if the StatusHandler didn't consume it,
@@ -501,20 +521,11 @@ private <T> Mono<T> drainBody(ClientResponse response, Throwable ex) {
501
521
.onErrorResume (ex2 -> Mono .empty ()).thenReturn (ex );
502
522
}
503
523
504
- @ SuppressWarnings ("unchecked" )
505
- private <T extends Publisher <?>> T insertCheckpoint (T result , HttpStatus status , HttpRequest request ) {
524
+ private <T > Mono <T > insertCheckpoint (Mono <T > result , HttpStatus status , HttpRequest request ) {
506
525
String httpMethod = request .getMethodValue ();
507
526
URI uri = request .getURI ();
508
527
String description = status + " from " + httpMethod + " " + uri + " [DefaultWebClient]" ;
509
- if (result instanceof Mono ) {
510
- return (T ) ((Mono <?>) result ).checkpoint (description );
511
- }
512
- else if (result instanceof Flux ) {
513
- return (T ) ((Flux <?>) result ).checkpoint (description );
514
- }
515
- else {
516
- return result ;
517
- }
528
+ return result .checkpoint (description );
518
529
}
519
530
520
531
@@ -527,8 +538,6 @@ private static class StatusHandler {
527
538
public StatusHandler (Predicate <HttpStatus > predicate ,
528
539
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
529
540
530
- Assert .notNull (predicate , "Predicate must not be null" );
531
- Assert .notNull (exceptionFunction , "Function must not be null" );
532
541
this .predicate = predicate ;
533
542
this .exceptionFunction = exceptionFunction ;
534
543
}
0 commit comments