28
28
import java .util .function .BiFunction ;
29
29
import java .util .function .Consumer ;
30
30
import java .util .function .Function ;
31
+ import java .util .function .IntPredicate ;
31
32
import java .util .function .Predicate ;
32
33
import java .util .function .Supplier ;
33
34
@@ -396,8 +397,10 @@ public HttpHeaders getHeaders() {
396
397
397
398
private static class DefaultResponseSpec implements ResponseSpec {
398
399
400
+ private static final IntPredicate STATUS_CODE_ERROR = value -> value >= 400 ;
401
+
399
402
private static final StatusHandler DEFAULT_STATUS_HANDLER =
400
- new StatusHandler (HttpStatus :: isError , DefaultResponseSpec ::createResponseException );
403
+ new StatusHandler (STATUS_CODE_ERROR , DefaultResponseSpec ::createResponseException );
401
404
402
405
private final Mono <ClientResponse > responseMono ;
403
406
@@ -414,11 +417,24 @@ private static class DefaultResponseSpec implements ResponseSpec {
414
417
@ Override
415
418
public ResponseSpec onStatus (Predicate <HttpStatus > statusPredicate ,
416
419
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
420
+ return onRawStatus (toIntPredicate (statusPredicate ), exceptionFunction );
421
+ }
422
+
423
+ private static IntPredicate toIntPredicate (Predicate <HttpStatus > predicate ) {
424
+ return value -> {
425
+ HttpStatus status = HttpStatus .resolve (value );
426
+ return (status != null ) && predicate .test (status );
427
+ };
428
+ }
429
+
430
+ @ Override
431
+ public ResponseSpec onRawStatus (IntPredicate statusCodePredicate ,
432
+ Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction ) {
417
433
418
434
if (this .statusHandlers .size () == 1 && this .statusHandlers .get (0 ) == DEFAULT_STATUS_HANDLER ) {
419
435
this .statusHandlers .clear ();
420
436
}
421
- this .statusHandlers .add (new StatusHandler (statusPredicate ,
437
+ this .statusHandlers .add (new StatusHandler (statusCodePredicate ,
422
438
(clientResponse , request ) -> exceptionFunction .apply (clientResponse )));
423
439
424
440
return this ;
@@ -451,27 +467,23 @@ public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> elementType) {
451
467
private <T extends Publisher <?>> T handleBody (ClientResponse response ,
452
468
T bodyPublisher , Function <Mono <? extends Throwable >, T > errorFunction ) {
453
469
454
- if (HttpStatus .resolve (response .rawStatusCode ()) != null ) {
455
- for (StatusHandler handler : this .statusHandlers ) {
456
- if (handler .test (response .statusCode ())) {
457
- HttpRequest request = this .requestSupplier .get ();
458
- Mono <? extends Throwable > exMono ;
459
- try {
460
- exMono = handler .apply (response , request );
461
- exMono = exMono .flatMap (ex -> drainBody (response , ex ));
462
- exMono = exMono .onErrorResume (ex -> drainBody (response , ex ));
463
- }
464
- catch (Throwable ex2 ) {
465
- exMono = drainBody (response , ex2 );
466
- }
467
- return errorFunction .apply (exMono );
470
+ int statusCode = response .rawStatusCode ();
471
+ for (StatusHandler handler : this .statusHandlers ) {
472
+ if (handler .test (statusCode )) {
473
+ HttpRequest request = this .requestSupplier .get ();
474
+ Mono <? extends Throwable > exMono ;
475
+ try {
476
+ exMono = handler .apply (response , request );
477
+ exMono = exMono .flatMap (ex -> drainBody (response , ex ));
478
+ exMono = exMono .onErrorResume (ex -> drainBody (response , ex ));
468
479
}
480
+ catch (Throwable ex2 ) {
481
+ exMono = drainBody (response , ex2 );
482
+ }
483
+ return errorFunction .apply (exMono );
469
484
}
470
- return bodyPublisher ;
471
- }
472
- else {
473
- return errorFunction .apply (createResponseException (response , this .requestSupplier .get ()));
474
485
}
486
+ return bodyPublisher ;
475
487
}
476
488
477
489
@ SuppressWarnings ("unchecked" )
@@ -520,11 +532,11 @@ private static Mono<WebClientResponseException> createResponseException(
520
532
521
533
private static class StatusHandler {
522
534
523
- private final Predicate < HttpStatus > predicate ;
535
+ private final IntPredicate predicate ;
524
536
525
537
private final BiFunction <ClientResponse , HttpRequest , Mono <? extends Throwable >> exceptionFunction ;
526
538
527
- public StatusHandler (Predicate < HttpStatus > predicate ,
539
+ public StatusHandler (IntPredicate predicate ,
528
540
BiFunction <ClientResponse , HttpRequest , Mono <? extends Throwable >> exceptionFunction ) {
529
541
530
542
Assert .notNull (predicate , "Predicate must not be null" );
@@ -533,13 +545,15 @@ public StatusHandler(Predicate<HttpStatus> predicate,
533
545
this .exceptionFunction = exceptionFunction ;
534
546
}
535
547
536
- public boolean test (HttpStatus status ) {
548
+ public boolean test (int status ) {
537
549
return this .predicate .test (status );
538
550
}
539
551
540
552
public Mono <? extends Throwable > apply (ClientResponse response , HttpRequest request ) {
541
553
return this .exceptionFunction .apply (response , request );
542
554
}
555
+
556
+
543
557
}
544
558
}
545
559
0 commit comments