Search in sources :

Example 6 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.

the class HandlerMethodArgumentResolverSupport method checkParamTypeNoReactiveWrapper.

/**
	 * Evaluate the {@code Predicate} on the method parameter type but raise an
	 * {@code IllegalStateException} if the same matches the generic type
	 * within a reactive type wrapper.
	 */
protected boolean checkParamTypeNoReactiveWrapper(MethodParameter param, Predicate<Class<?>> predicate) {
    Class<?> type = param.getParameterType();
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
    if (adapter != null) {
        assertHasValues(adapter, param);
        type = param.nested().getNestedParameterType();
    }
    if (predicate.test(type)) {
        if (adapter == null) {
            return true;
        }
        throw getReactiveWrapperError(param);
    }
    return false;
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 7 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.

the class ResponseEntityResultHandler method handleResult.

@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
    Mono<?> returnValueMono;
    MethodParameter bodyParameter;
    ReactiveAdapter adapter = getAdapter(result);
    if (adapter != null) {
        Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
        returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
        bodyParameter = result.getReturnTypeSource().nested().nested();
    } else {
        returnValueMono = Mono.justOrEmpty(result.getReturnValue());
        bodyParameter = result.getReturnTypeSource().nested();
    }
    return returnValueMono.then(returnValue -> {
        Assert.isInstanceOf(HttpEntity.class, returnValue, "HttpEntity expected");
        HttpEntity<?> httpEntity = (HttpEntity<?>) returnValue;
        if (httpEntity instanceof ResponseEntity) {
            ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
            exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
        }
        HttpHeaders entityHeaders = httpEntity.getHeaders();
        HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
        if (!entityHeaders.isEmpty()) {
            entityHeaders.entrySet().stream().filter(entry -> !responseHeaders.containsKey(entry.getKey())).forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
        }
        if (httpEntity.getBody() == null) {
            return exchange.getResponse().setComplete();
        }
        String etag = entityHeaders.getETag();
        Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
        HttpMethod httpMethod = exchange.getRequest().getMethod();
        if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
            return exchange.getResponse().setComplete();
        }
        return writeBody(httpEntity.getBody(), bodyParameter, exchange);
    });
}
Also used : RequestEntity(org.springframework.http.RequestEntity) Arrays(java.util.Arrays) ReactiveAdapter(org.springframework.core.ReactiveAdapter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResponseEntity(org.springframework.http.ResponseEntity) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) Instant(java.time.Instant) MethodParameter(org.springframework.core.MethodParameter) ReactiveAdapter(org.springframework.core.ReactiveAdapter) HttpMethod(org.springframework.http.HttpMethod)

Example 8 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.

the class ViewResolutionResultHandler method resolveAsyncAttributes.

private Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
    List<String> names = new ArrayList<>();
    List<Mono<?>> valueMonos = new ArrayList<>();
    for (Map.Entry<String, ?> entry : model.entrySet()) {
        ReactiveAdapter adapter = getAdapterRegistry().getAdapter(null, entry.getValue());
        if (adapter != null) {
            names.add(entry.getKey());
            if (adapter.isMultiValue()) {
                Flux<Object> value = Flux.from(adapter.toPublisher(entry.getValue()));
                valueMonos.add(value.collectList().defaultIfEmpty(Collections.emptyList()));
            } else {
                Mono<Object> value = Mono.from(adapter.toPublisher(entry.getValue()));
                valueMonos.add(value.defaultIfEmpty(NO_VALUE));
            }
        }
    }
    if (names.isEmpty()) {
        return Mono.empty();
    }
    return Mono.when(valueMonos, values -> {
        for (int i = 0; i < values.length; i++) {
            if (values[i] != NO_VALUE) {
                model.put(names.get(i), values[i]);
            } else {
                model.remove(names.get(i));
            }
        }
        return NO_VALUE;
    }).then();
}
Also used : HttpRequestPathHelper(org.springframework.web.server.support.HttpRequestPathHelper) Ordered(org.springframework.core.Ordered) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerResultHandlerSupport(org.springframework.web.reactive.result.HandlerResultHandlerSupport) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ClassUtils(org.springframework.util.ClassUtils) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) Collectors(java.util.stream.Collectors) HandlerResult(org.springframework.web.reactive.HandlerResult) Flux(reactor.core.publisher.Flux) List(java.util.List) Collections(java.util.Collections) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) Map(java.util.Map) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 9 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter 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));
}
Also used : ServerHttpMessageReader(org.springframework.http.codec.ServerHttpMessageReader) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Validator(org.springframework.validation.Validator) Conventions(org.springframework.core.Conventions) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ServerWebInputException(org.springframework.web.server.ServerWebInputException) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) BindingContext(org.springframework.web.reactive.BindingContext) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethodArgumentResolverSupport(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ReactiveAdapter(org.springframework.core.ReactiveAdapter) Validated(org.springframework.validation.annotation.Validated) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) List(java.util.List) Annotation(java.lang.annotation.Annotation) Collections(java.util.Collections) Assert(org.springframework.util.Assert) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServerHttpMessageReader(org.springframework.http.codec.ServerHttpMessageReader) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 10 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.

the class HandlerMethodArgumentResolverSupport method checkParamType.

/**
	 * Evaluate the {@code Predicate} on the the method parameter type or on
	 * the generic type within a reactive type wrapper.
	 */
protected boolean checkParamType(MethodParameter param, Predicate<Class<?>> predicate) {
    Class<?> type = param.getParameterType();
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
    if (adapter != null) {
        assertHasValues(adapter, param);
        type = param.nested().getNestedParameterType();
    }
    return predicate.test(type);
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

ReactiveAdapter (org.springframework.core.ReactiveAdapter)13 ResolvableType (org.springframework.core.ResolvableType)7 MethodParameter (org.springframework.core.MethodParameter)6 List (java.util.List)4 Map (java.util.Map)4 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)4 Assert (org.springframework.util.Assert)4 ServerWebExchange (org.springframework.web.server.ServerWebExchange)4 Mono (reactor.core.publisher.Mono)4 MediaType (org.springframework.http.MediaType)3 Model (org.springframework.ui.Model)3 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)3 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)3 BindingContext (org.springframework.web.reactive.BindingContext)3 Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 BeanUtils (org.springframework.beans.BeanUtils)2