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

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

Introduction

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

Prototype

boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType);

Source Link

Document

Whether the given object type is supported by this reader.

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));
    }/* w  w w.jav a 2  s  . co  m*/
    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));
}