Skip to content

Commit f5da737

Browse files
committed
Polish
1 parent c187cb2 commit f5da737

File tree

2 files changed

+47
-71
lines changed

2 files changed

+47
-71
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private static <T> BodyExtractor<Mono<T>, ReactiveHttpInputMessage> toMono(Resol
8484
return (inputMessage, context) ->
8585
readWithMessageReaders(inputMessage, context, elementType,
8686
(HttpMessageReader<T> reader) -> readToMono(inputMessage, context, elementType, reader),
87-
ex -> Mono.from(unsupportedErrorHandler(inputMessage, context, ex)),
88-
skipBodyAsMono(inputMessage, context));
87+
ex -> Mono.from(unsupportedErrorHandler(inputMessage, ex)),
88+
skipBodyAsMono(inputMessage));
8989
}
9090

9191
/**
@@ -113,8 +113,8 @@ private static <T> BodyExtractor<Flux<T>, ReactiveHttpInputMessage> toFlux(Resol
113113
return (inputMessage, context) ->
114114
readWithMessageReaders(inputMessage, context, elementType,
115115
(HttpMessageReader<T> reader) -> readToFlux(inputMessage, context, elementType, reader),
116-
ex -> unsupportedErrorHandler(inputMessage, context, ex),
117-
skipBodyAsFlux(inputMessage, context));
116+
ex -> unsupportedErrorHandler(inputMessage, ex),
117+
skipBodyAsFlux(inputMessage));
118118
}
119119

120120

@@ -194,39 +194,13 @@ private static <T, S extends Publisher<T>> S readWithMessageReaders(
194194
.findFirst()
195195
.map(BodyExtractors::<T>cast)
196196
.map(readerFunction)
197-
.orElseGet(() -> errorFunction.apply(unsupportedError(context, elementType, contentType)));
198-
}
199-
200-
private static <T> Supplier<Flux<T>> skipBodyAsFlux(ReactiveHttpInputMessage message,
201-
BodyExtractor.Context context) {
202-
203-
if (isExtractingForClient(message)) {
204-
return () -> consumeAndCancel(message).thenMany(Flux.empty());
205-
}
206-
else {
207-
return Flux::empty;
208-
}
209-
}
210-
211-
private static <T> Supplier<Mono<T>> skipBodyAsMono(ReactiveHttpInputMessage message,
212-
BodyExtractor.Context context) {
213-
214-
if (isExtractingForClient(message)) {
215-
return () -> consumeAndCancel(message).then(Mono.empty());
216-
}
217-
else {
218-
return Mono::empty;
219-
}
220-
}
221-
222-
private static UnsupportedMediaTypeException unsupportedError(BodyExtractor.Context context,
223-
ResolvableType elementType, MediaType contentType) {
224-
225-
List<MediaType> supportedMediaTypes = context.messageReaders().stream()
226-
.flatMap(reader -> reader.getReadableMediaTypes().stream())
227-
.collect(Collectors.toList());
228-
229-
return new UnsupportedMediaTypeException(contentType, supportedMediaTypes, elementType);
197+
.orElseGet(() -> {
198+
List<MediaType> mediaTypes = context.messageReaders().stream()
199+
.flatMap(reader -> reader.getReadableMediaTypes().stream())
200+
.collect(Collectors.toList());
201+
return errorFunction.apply(
202+
new UnsupportedMediaTypeException(contentType, mediaTypes, elementType));
203+
});
230204
}
231205

232206
private static <T> Mono<T> readToMono(ReactiveHttpInputMessage message, BodyExtractor.Context context,
@@ -246,21 +220,22 @@ private static <T> Flux<T> readToFlux(ReactiveHttpInputMessage message, BodyExtr
246220
}
247221

248222
private static <T> Flux<T> unsupportedErrorHandler(
249-
ReactiveHttpInputMessage inputMessage, BodyExtractor.Context context,
250-
UnsupportedMediaTypeException ex) {
223+
ReactiveHttpInputMessage message, UnsupportedMediaTypeException ex) {
251224

252225
Flux<T> result;
253-
if (inputMessage.getHeaders().getContentType() == null) {
254-
// Empty body with no content type is ok
255-
result = inputMessage.getBody().map(o -> {
226+
if (message.getHeaders().getContentType() == null) {
227+
// Maybe it's okay, if there is no content..
228+
result = message.getBody().map(o -> {
256229
throw ex;
257230
});
258231
}
259232
else {
260233
result = Flux.error(ex);
261234
}
262-
return isExtractingForClient(inputMessage) ?
263-
consumeAndCancel(inputMessage).thenMany(result) : result;
235+
if (message instanceof ClientHttpResponse) {
236+
result = consumeAndCancel(message).thenMany(result);
237+
}
238+
return result;
264239
}
265240

266241
private static <T> HttpMessageReader<T> findReader(
@@ -279,8 +254,15 @@ private static <T> HttpMessageReader<T> cast(HttpMessageReader<?> reader) {
279254
return (HttpMessageReader<T>) reader;
280255
}
281256

282-
private static boolean isExtractingForClient(ReactiveHttpInputMessage message) {
283-
return message instanceof ClientHttpResponse;
257+
private static <T> Supplier<Flux<T>> skipBodyAsFlux(ReactiveHttpInputMessage message) {
258+
return message instanceof ClientHttpResponse ?
259+
() -> consumeAndCancel(message).thenMany(Mono.empty()) : Flux::empty;
260+
}
261+
262+
@SuppressWarnings("unchecked")
263+
private static <T> Supplier<Mono<T>> skipBodyAsMono(ReactiveHttpInputMessage message) {
264+
return message instanceof ClientHttpResponse ?
265+
() -> consumeAndCancel(message).then(Mono.empty()) : Mono::empty;
284266
}
285267

286268
private static Mono<Void> consumeAndCancel(ReactiveHttpInputMessage message) {
@@ -296,4 +278,5 @@ private static Mono<Void> consumeAndCancel(ReactiveHttpInputMessage message) {
296278
@SuppressWarnings("serial")
297279
private static class ReadCancellationException extends RuntimeException {
298280
}
281+
299282
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,14 @@ private static class DefaultResponseSpec implements ResponseSpec {
378378

379379
private final List<StatusHandler> statusHandlers = new ArrayList<>(1);
380380

381+
381382
DefaultResponseSpec(Mono<ClientResponse> responseMono) {
382383
this.responseMono = responseMono;
383384
this.statusHandlers.add(DEFAULT_STATUS_HANDLER);
384385
}
385386

387+
388+
386389
@Override
387390
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
388391
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
@@ -396,45 +399,33 @@ public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
396399
}
397400

398401
@Override
399-
@SuppressWarnings("unchecked")
400402
public <T> Mono<T> bodyToMono(Class<T> bodyType) {
401-
// Use bodyToMono (vs BodyExtractors) to ensure proper handling of Void.class...
402-
return this.responseMono.flatMap(
403-
response -> bodyToPublisher(response, response.bodyToMono(bodyType),
404-
this::monoThrowableToMono));
403+
return this.responseMono.flatMap(response -> handleBody(response,
404+
response.bodyToMono(bodyType), mono -> mono.flatMap(Mono::error)));
405405
}
406406

407407
@Override
408408
@SuppressWarnings("unchecked")
409-
public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) {
410-
return this.responseMono.flatMap(
411-
response -> bodyToPublisher(response, response.bodyToMono(typeReference),
412-
this::monoThrowableToMono));
413-
}
414-
415-
private <T> Mono<T> monoThrowableToMono(Mono<? extends Throwable> mono) {
416-
return mono.flatMap(Mono::error);
409+
public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> bodyType) {
410+
return this.responseMono.flatMap(response ->
411+
handleBody(response, response.bodyToMono(bodyType), mono -> mono.flatMap(Mono::error)));
417412
}
418413

419414
@Override
415+
@SuppressWarnings("unchecked")
420416
public <T> Flux<T> bodyToFlux(Class<T> elementType) {
421-
return this.responseMono.flatMapMany(
422-
response -> bodyToPublisher(response, response.bodyToFlux(elementType),
423-
this::monoThrowableToFlux));
417+
return this.responseMono.flatMapMany(response ->
418+
handleBody(response, response.bodyToFlux(elementType), mono -> mono.flatMapMany(Flux::error)));
424419
}
425420

426421
@Override
427-
public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference) {
428-
return this.responseMono.flatMapMany(
429-
response -> bodyToPublisher(response, response.bodyToFlux(typeReference),
430-
this::monoThrowableToFlux));
431-
}
432-
433-
private <T> Flux<T> monoThrowableToFlux(Mono<? extends Throwable> mono) {
434-
return mono.flatMapMany(Flux::error);
422+
@SuppressWarnings("unchecked")
423+
public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> elementType) {
424+
return this.responseMono.flatMapMany(response -> handleBody(response,
425+
response.bodyToFlux(elementType), mono -> mono.flatMapMany(Flux::error)));
435426
}
436427

437-
private <T extends Publisher<?>> T bodyToPublisher(ClientResponse response,
428+
private <T extends Publisher<?>> T handleBody(ClientResponse response,
438429
T bodyPublisher, Function<Mono<? extends Throwable>, T> errorFunction) {
439430

440431
if (HttpStatus.resolve(response.rawStatusCode()) != null) {
@@ -498,6 +489,7 @@ private static class StatusHandler {
498489

499490
private final Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction;
500491

492+
501493
public StatusHandler(Predicate<HttpStatus> predicate,
502494
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
503495

@@ -507,6 +499,7 @@ public StatusHandler(Predicate<HttpStatus> predicate,
507499
this.exceptionFunction = exceptionFunction;
508500
}
509501

502+
510503
public boolean test(HttpStatus status) {
511504
return this.predicate.test(status);
512505
}

0 commit comments

Comments
 (0)