Example usage for org.springframework.web.server ServerWebExchange getLogPrefix

List of usage examples for org.springframework.web.server ServerWebExchange getLogPrefix

Introduction

In this page you can find the example usage for org.springframework.web.server ServerWebExchange getLogPrefix.

Prototype

String getLogPrefix();

Source Link

Document

Return a log message prefix to use to correlate messages for this exchange.

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 w  w w  .j a  v a  2 s .c  om
    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));
}

From source file:org.springframework.web.reactive.result.HandlerResultHandlerSupport.java

/**
 * Select the best media type for the current request through a content
 * negotiation algorithm./*from   w  w  w .j  a  v a  2  s.c  o  m*/
 * @param exchange the current request
 * @param producibleTypesSupplier the media types that can be produced for the current request
 * @return the selected media type or {@code null}
 */
@Nullable
protected MediaType selectMediaType(ServerWebExchange exchange,
        Supplier<List<MediaType>> producibleTypesSupplier) {

    MediaType contentType = exchange.getResponse().getHeaders().getContentType();
    if (contentType != null && contentType.isConcrete()) {
        if (logger.isDebugEnabled()) {
            logger.debug(exchange.getLogPrefix() + "Found 'Content-Type:" + contentType + "' in response");
        }
        return contentType;
    }

    List<MediaType> acceptableTypes = getAcceptableTypes(exchange);
    List<MediaType> producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier);

    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
    for (MediaType acceptable : acceptableTypes) {
        for (MediaType producible : producibleTypes) {
            if (acceptable.isCompatibleWith(producible)) {
                compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible));
            }
        }
    }

    List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(result);

    MediaType selected = null;
    for (MediaType mediaType : result) {
        if (mediaType.isConcrete()) {
            selected = mediaType;
            break;
        } else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
            selected = MediaType.APPLICATION_OCTET_STREAM;
            break;
        }
    }

    if (selected != null) {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Using '" + selected + "' given " + acceptableTypes + " and supported " + producibleTypes);
        }
    } else if (logger.isDebugEnabled()) {
        logger.debug(exchange.getLogPrefix() + "No match for " + acceptableTypes + ", supported: "
                + producibleTypes);
    }

    return selected;
}