Skip to content

Commit

Permalink
Shortcut handling of bodyToFlux(DataBuffer.class)
Browse files Browse the repository at this point in the history
Given that the body is a Flux<DataBuffer> there probably could be a
Flux<DataBuffer> body();

At least bodyToFlux(DataBuffer.class) which is used when mutating and
is a common case should not incur overhead.

See gh-24680
  • Loading branch information
rstoyanchev committed May 11, 2020
1 parent 67a06f5 commit 0e9ecb6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.Hints;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
Expand Down Expand Up @@ -66,6 +67,8 @@ class DefaultClientResponse implements ClientResponse {

private final Supplier<HttpRequest> requestSupplier;

private final BodyExtractor.Context bodyExtractorContext;


public DefaultClientResponse(ClientHttpResponse response, ExchangeStrategies strategies,
String logPrefix, String requestDescription, Supplier<HttpRequest> requestSupplier) {
Expand All @@ -76,6 +79,22 @@ public DefaultClientResponse(ClientHttpResponse response, ExchangeStrategies str
this.logPrefix = logPrefix;
this.requestDescription = requestDescription;
this.requestSupplier = requestSupplier;
this.bodyExtractorContext = new BodyExtractor.Context() {
@Override
public List<HttpMessageReader<?>> messageReaders() {
return strategies.messageReaders();
}

@Override
public Optional<ServerHttpResponse> serverResponse() {
return Optional.empty();
}

@Override
public Map<String, Object> hints() {
return Hints.from(Hints.LOG_PREFIX_HINT, logPrefix);
}
};
}


Expand Down Expand Up @@ -107,22 +126,7 @@ public MultiValueMap<String, ResponseCookie> cookies() {
@SuppressWarnings("unchecked")
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
T result = extractor.extract(this.response, new BodyExtractor.Context() {
@Override
public List<HttpMessageReader<?>> messageReaders() {
return strategies.messageReaders();
}

@Override
public Optional<ServerHttpResponse> serverResponse() {
return Optional.empty();
}

@Override
public Map<String, Object> hints() {
return Hints.from(Hints.LOG_PREFIX_HINT, logPrefix);
}
});
T result = extractor.extract(this.response, this.bodyExtractorContext);
String description = "Body from " + this.requestDescription + " [DefaultClientResponse]";
if (result instanceof Mono) {
return (T) ((Mono<?>) result).checkpoint(description);
Expand All @@ -146,8 +150,10 @@ public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> elementTypeRef) {
}

@Override
@SuppressWarnings("unchecked")
public <T> Flux<T> bodyToFlux(Class<? extends T> elementClass) {
return body(BodyExtractors.toFlux(elementClass));
return elementClass.equals(DataBuffer.class) ?
(Flux<T>) body(BodyExtractors.toDataBuffers()) : body(BodyExtractors.toFlux(elementClass));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.Hints;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
Expand Down Expand Up @@ -195,8 +196,10 @@ public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) {
}

@Override
@SuppressWarnings("unchecked")
public <T> Flux<T> bodyToFlux(Class<? extends T> elementClass) {
Flux<T> flux = body(BodyExtractors.toFlux(elementClass));
Flux<T> flux = (elementClass.equals(DataBuffer.class) ?
(Flux<T>) request().getBody() : body(BodyExtractors.toFlux(elementClass)));
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
.onErrorMap(DecodingException.class, DECODING_MAPPER);
}
Expand Down

0 comments on commit 0e9ecb6

Please sign in to comment.