Example usage for org.springframework.http.codec HttpMessageReader read

List of usage examples for org.springframework.http.codec HttpMessageReader read

Introduction

In this page you can find the example usage for org.springframework.http.codec HttpMessageReader read.

Prototype

default Flux<T> read(ResolvableType actualType, ResolvableType elementType, ServerHttpRequest request,
        ServerHttpResponse response, Map<String, Object> hints) 

Source Link

Document

Server-side only alternative to #read(ResolvableType,ReactiveHttpInputMessage,Map) with additional context available.

Usage

From source file:org.springframework.cloud.function.web.RequestProcessor.java

private Publisher<?> body(Object handler, ServerWebExchange exchange) {
    ResolvableType elementType = ResolvableType.forClass(this.inspector.getInputType(handler));
    ResolvableType actualType = elementType;
    Class<?> resolvedType = elementType.resolve();
    ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);

    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();

    MediaType contentType = request.getHeaders().getContentType();
    MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);

    if (logger.isDebugEnabled()) {
        logger.debug(exchange.getLogPrefix() + (contentType != null ? "Content-Type:" + contentType
                : "No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
    }/*from ww  w  . j a va2s .com*/
    boolean isBodyRequired = (adapter != null && !adapter.supportsEmpty());

    MethodParameter bodyParam = new MethodParameter(handlerMethod(handler), 0);
    for (HttpMessageReader<?> reader : getMessageReaders()) {
        if (reader.canRead(elementType, mediaType)) {
            Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix());
            if (adapter != null && adapter.isMultiValue()) {
                if (logger.isDebugEnabled()) {
                    logger.debug(exchange.getLogPrefix() + "0..N [" + elementType + "]");
                }
                Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
                flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
                if (isBodyRequired) {
                    flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
                }
                return Mono.just(adapter.fromPublisher(flux));
            } else {
                // Single-value (with or without reactive type wrapper)
                if (logger.isDebugEnabled()) {
                    logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
                }
                Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
                mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
                if (isBodyRequired) {
                    mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
                }
                return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
            }
        }
    }

    return Mono.error(new UnsupportedMediaTypeStatusException(mediaType,
            Arrays.asList(MediaType.APPLICATION_JSON), elementType));
}