use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.
the class DefaultClientResponseTests method bodyToMonoError.
@Test
public void bodyToMonoError() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
when(mockResponse.getHeaders()).thenReturn(httpHeaders);
when(mockResponse.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
StepVerifier.create(resultMono).expectError(WebClientException.class).verify();
}
use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.
the class AbstractMessageReaderArgumentResolver method readBody.
protected Mono<Object> readBody(MethodParameter bodyParameter, boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {
ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve());
ResolvableType elementType = (adapter != null ? bodyType.getGeneric(0) : bodyType);
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
MediaType mediaType = request.getHeaders().getContentType();
if (mediaType == null) {
mediaType = MediaType.APPLICATION_OCTET_STREAM;
}
for (HttpMessageReader<?> reader : getMessageReaders()) {
if (reader.canRead(elementType, mediaType)) {
Map<String, Object> readHints = Collections.emptyMap();
if (adapter != null && adapter.isMultiValue()) {
Flux<?> flux;
if (reader instanceof ServerHttpMessageReader) {
ServerHttpMessageReader<?> serverReader = ((ServerHttpMessageReader<?>) reader);
flux = serverReader.read(bodyType, elementType, request, response, readHints);
} else {
flux = reader.read(elementType, request, readHints);
}
flux = flux.onErrorResumeWith(ex -> Flux.error(getReadError(bodyParameter, ex)));
if (isBodyRequired || !adapter.supportsEmpty()) {
flux = flux.switchIfEmpty(Flux.error(getRequiredBodyError(bodyParameter)));
}
Object[] hints = extractValidationHints(bodyParameter);
if (hints != null) {
flux = flux.doOnNext(target -> validate(target, hints, bodyParameter, bindingContext, exchange));
}
return Mono.just(adapter.fromPublisher(flux));
} else {
Mono<?> mono;
if (reader instanceof ServerHttpMessageReader) {
ServerHttpMessageReader<?> serverReader = (ServerHttpMessageReader<?>) reader;
mono = serverReader.readMono(bodyType, elementType, request, response, readHints);
} else {
mono = reader.readMono(elementType, request, readHints);
}
mono = mono.otherwise(ex -> Mono.error(getReadError(bodyParameter, ex)));
if (isBodyRequired || (adapter != null && !adapter.supportsEmpty())) {
mono = mono.otherwiseIfEmpty(Mono.error(getRequiredBodyError(bodyParameter)));
}
Object[] hints = extractValidationHints(bodyParameter);
if (hints != null) {
mono = mono.doOnNext(target -> validate(target, hints, bodyParameter, bindingContext, exchange));
}
if (adapter != null) {
return Mono.just(adapter.fromPublisher(mono));
} else {
return Mono.from(mono);
}
}
}
}
return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes));
}
use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.
the class BodyExtractors method readWithMessageReaders.
private static <T, S extends Publisher<T>> S readWithMessageReaders(ReactiveHttpInputMessage inputMessage, BodyExtractor.Context context, ResolvableType elementType, Function<HttpMessageReader<T>, S> readerFunction, Function<Throwable, S> unsupportedError) {
MediaType contentType = contentType(inputMessage);
Supplier<Stream<HttpMessageReader<?>>> messageReaders = context.messageReaders();
return messageReaders.get().filter(r -> r.canRead(elementType, contentType)).findFirst().map(BodyExtractors::<T>cast).map(readerFunction).orElseGet(() -> {
List<MediaType> supportedMediaTypes = messageReaders.get().flatMap(reader -> reader.getReadableMediaTypes().stream()).collect(Collectors.toList());
UnsupportedMediaTypeException error = new UnsupportedMediaTypeException(contentType, supportedMediaTypes);
return unsupportedError.apply(error);
});
}
use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.
the class HttpEntityArgumentResolverTests method createResolver.
private HttpEntityArgumentResolver createResolver() {
List<HttpMessageReader<?>> readers = new ArrayList<>();
readers.add(new DecoderHttpMessageReader<>(new StringDecoder()));
return new HttpEntityArgumentResolver(readers, new ReactiveAdapterRegistry());
}
use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.
the class WebFluxConfigurationSupportTests method assertHasMessageReader.
private void assertHasMessageReader(List<HttpMessageReader<?>> readers, Class<?> clazz, MediaType mediaType) {
ResolvableType type = ResolvableType.forClass(clazz);
assertTrue(readers.stream().anyMatch(c -> mediaType == null || c.canRead(type, mediaType)));
}
Aggregations