26
26
import java .util .Map ;
27
27
import java .util .function .Consumer ;
28
28
import java .util .function .Function ;
29
+ import java .util .function .IntPredicate ;
29
30
import java .util .function .Predicate ;
30
31
import java .util .function .Supplier ;
31
32
@@ -416,6 +417,8 @@ public HttpHeaders getHeaders() {
416
417
417
418
private static class DefaultResponseSpec implements ResponseSpec {
418
419
420
+ private static final IntPredicate STATUS_CODE_ERROR = value -> value >= 400 ;
421
+
419
422
private final Mono <ClientResponse > responseMono ;
420
423
421
424
private final Supplier <HttpRequest > requestSupplier ;
@@ -425,17 +428,30 @@ private static class DefaultResponseSpec implements ResponseSpec {
425
428
DefaultResponseSpec (Mono <ClientResponse > responseMono , Supplier <HttpRequest > requestSupplier ) {
426
429
this .responseMono = responseMono ;
427
430
this .requestSupplier = requestSupplier ;
428
- this .statusHandlers .add (new StatusHandler (HttpStatus :: isError , ClientResponse ::createException ));
431
+ this .statusHandlers .add (new StatusHandler (STATUS_CODE_ERROR , ClientResponse ::createException ));
429
432
}
430
433
431
434
@ Override
432
435
public ResponseSpec onStatus (Predicate <HttpStatus > statusPredicate ,
433
436
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
437
+ return onRawStatus (toIntPredicate (statusPredicate ), exceptionFunction );
438
+ }
434
439
435
- Assert .notNull (statusPredicate , "StatusPredicate must not be null" );
440
+ private static IntPredicate toIntPredicate (Predicate <HttpStatus > predicate ) {
441
+ return value -> {
442
+ HttpStatus status = HttpStatus .resolve (value );
443
+ return (status != null ) && predicate .test (status );
444
+ };
445
+ }
446
+
447
+ @ Override
448
+ public ResponseSpec onRawStatus (IntPredicate statusCodePredicate ,
449
+ Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
450
+
451
+ Assert .notNull (statusCodePredicate , "StatusCodePredicate must not be null" );
436
452
Assert .notNull (exceptionFunction , "Function must not be null" );
437
453
438
- this .statusHandlers .add (0 , new StatusHandler (statusPredicate , exceptionFunction ));
454
+ this .statusHandlers .add (0 , new StatusHandler (statusCodePredicate , exceptionFunction ));
439
455
return this ;
440
456
}
441
457
@@ -452,17 +468,12 @@ public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> elementTypeRef) {
452
468
}
453
469
454
470
private <T > Mono <T > handleBodyMono (ClientResponse response , Mono <T > bodyPublisher ) {
455
- if (HttpStatus .resolve (response .rawStatusCode ()) != null ) {
456
- Mono <T > result = statusHandlers (response );
457
- if (result != null ) {
458
- return result .switchIfEmpty (bodyPublisher );
459
- }
460
- else {
461
- return bodyPublisher ;
462
- }
471
+ Mono <T > result = statusHandlers (response );
472
+ if (result != null ) {
473
+ return result .switchIfEmpty (bodyPublisher );
463
474
}
464
475
else {
465
- return response . createException (). flatMap ( Mono :: error ) ;
476
+ return bodyPublisher ;
466
477
}
467
478
}
468
479
@@ -479,24 +490,20 @@ public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> elementTypeRef) {
479
490
}
480
491
481
492
private <T > Publisher <T > handleBodyFlux (ClientResponse response , Flux <T > bodyPublisher ) {
482
- if (HttpStatus .resolve (response .rawStatusCode ()) != null ) {
483
- Mono <T > result = statusHandlers (response );
484
- if (result != null ) {
485
- return result .flux ().switchIfEmpty (bodyPublisher );
486
- }
487
- else {
488
- return bodyPublisher ;
489
- }
493
+ Mono <T > result = statusHandlers (response );
494
+ if (result != null ) {
495
+ return result .flux ().switchIfEmpty (bodyPublisher );
490
496
}
491
497
else {
492
- return response . createException (). flatMap ( Mono :: error ) ;
498
+ return bodyPublisher ;
493
499
}
494
500
}
495
501
496
502
@ Nullable
497
503
private <T > Mono <T > statusHandlers (ClientResponse response ) {
504
+ int statusCode = response .rawStatusCode ();
498
505
for (StatusHandler handler : this .statusHandlers ) {
499
- if (handler .test (response . statusCode () )) {
506
+ if (handler .test (statusCode )) {
500
507
Mono <? extends Throwable > exMono ;
501
508
try {
502
509
exMono = handler .apply (response );
@@ -508,7 +515,7 @@ private <T> Mono<T> statusHandlers(ClientResponse response) {
508
515
}
509
516
Mono <T > result = exMono .flatMap (Mono ::error );
510
517
HttpRequest request = this .requestSupplier .get ();
511
- return insertCheckpoint (result , response . statusCode () , request );
518
+ return insertCheckpoint (result , statusCode , request );
512
519
}
513
520
}
514
521
return null ;
@@ -522,10 +529,10 @@ private <T> Mono<T> drainBody(ClientResponse response, Throwable ex) {
522
529
.onErrorResume (ex2 -> Mono .empty ()).thenReturn (ex );
523
530
}
524
531
525
- private <T > Mono <T > insertCheckpoint (Mono <T > result , HttpStatus status , HttpRequest request ) {
532
+ private <T > Mono <T > insertCheckpoint (Mono <T > result , int statusCode , HttpRequest request ) {
526
533
String httpMethod = request .getMethodValue ();
527
534
URI uri = request .getURI ();
528
- String description = status + " from " + httpMethod + " " + uri + " [DefaultWebClient]" ;
535
+ String description = statusCode + " from " + httpMethod + " " + uri + " [DefaultWebClient]" ;
529
536
return result .checkpoint (description );
530
537
}
531
538
@@ -558,24 +565,25 @@ public <T> Mono<ResponseEntity<List<T>>> toEntityList(ParameterizedTypeReference
558
565
559
566
private static class StatusHandler {
560
567
561
- private final Predicate < HttpStatus > predicate ;
568
+ private final IntPredicate predicate ;
562
569
563
570
private final Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ;
564
571
565
- public StatusHandler (Predicate < HttpStatus > predicate ,
572
+ public StatusHandler (IntPredicate predicate ,
566
573
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
567
574
568
575
this .predicate = predicate ;
569
576
this .exceptionFunction = exceptionFunction ;
570
577
}
571
578
572
- public boolean test (HttpStatus status ) {
579
+ public boolean test (int status ) {
573
580
return this .predicate .test (status );
574
581
}
575
582
576
583
public Mono <? extends Throwable > apply (ClientResponse response ) {
577
584
return this .exceptionFunction .apply (response );
578
585
}
586
+
579
587
}
580
588
}
581
589
0 commit comments