Search in sources :

Example 36 with ReactiveAdapter

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

the class ModelInitializer method handleResult.

private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
    Object value = handlerResult.getReturnValue();
    if (value != null) {
        ResolvableType type = handlerResult.getReturnType();
        ReactiveAdapter adapter = this.adapterRegistry.getAdapter(type.resolve(), value);
        if (isAsyncVoidType(type, adapter)) {
            return Mono.from(adapter.toPublisher(value));
        }
        String name = getAttributeName(handlerResult.getReturnTypeSource());
        bindingContext.getModel().asMap().putIfAbsent(name, value);
    }
    return Mono.empty();
}
Also used : ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 37 with ReactiveAdapter

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

the class RequestAttributeMethodArgumentResolver method resolveNamedValue.

@Override
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
    Object value = exchange.getAttribute(name);
    ReactiveAdapter toAdapter = getAdapterRegistry().getAdapter(parameter.getParameterType());
    if (toAdapter != null) {
        if (value == null) {
            Assert.isTrue(toAdapter.supportsEmpty(), () -> "No request attribute '" + name + "' and target type " + parameter.getGenericParameterType() + " doesn't support empty values.");
            return toAdapter.fromPublisher(Mono.empty());
        }
        if (parameter.getParameterType().isAssignableFrom(value.getClass())) {
            return value;
        }
        ReactiveAdapter fromAdapter = getAdapterRegistry().getAdapter(value.getClass());
        Assert.isTrue(fromAdapter != null, () -> getClass().getSimpleName() + " doesn't support " + "reactive type wrapper: " + parameter.getGenericParameterType());
        return toAdapter.fromPublisher(fromAdapter.toPublisher(value));
    }
    return value;
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 38 with ReactiveAdapter

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

the class ViewResolutionResultHandler method handleResult.

@Override
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
    Mono<Object> valueMono;
    ResolvableType valueType;
    ReactiveAdapter adapter = getAdapter(result);
    if (adapter != null) {
        if (adapter.isMultiValue()) {
            throw new IllegalArgumentException("Multi-value reactive types not supported in view resolution: " + result.getReturnType());
        }
        valueMono = (result.getReturnValue() != null ? Mono.from(adapter.toPublisher(result.getReturnValue())) : Mono.empty());
        valueType = (adapter.isNoValue() ? ResolvableType.forClass(Void.class) : result.getReturnType().getGeneric());
    } else {
        valueMono = Mono.justOrEmpty(result.getReturnValue());
        valueType = result.getReturnType();
    }
    return valueMono.switchIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO).flatMap(returnValue -> {
        Mono<List<View>> viewsMono;
        Model model = result.getModel();
        MethodParameter parameter = result.getReturnTypeSource();
        Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());
        Class<?> clazz = valueType.toClass();
        if (clazz == Object.class) {
            clazz = returnValue.getClass();
        }
        if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) {
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
            viewsMono = resolveViews(returnValue.toString(), locale);
        } else if (Rendering.class.isAssignableFrom(clazz)) {
            Rendering render = (Rendering) returnValue;
            HttpStatus status = render.status();
            if (status != null) {
                exchange.getResponse().setStatusCode(status);
            }
            exchange.getResponse().getHeaders().putAll(render.headers());
            model.addAllAttributes(render.modelAttributes());
            Object view = render.view();
            if (view == null) {
                view = getDefaultViewName(exchange);
            }
            viewsMono = (view instanceof String ? resolveViews((String) view, locale) : Mono.just(Collections.singletonList((View) view)));
        } else if (Model.class.isAssignableFrom(clazz)) {
            model.addAllAttributes(((Model) returnValue).asMap());
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (Map.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
            model.addAllAttributes((Map<String, ?>) returnValue);
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (View.class.isAssignableFrom(clazz)) {
            viewsMono = Mono.just(Collections.singletonList((View) returnValue));
        } else {
            String name = getNameForReturnValue(parameter);
            model.addAttribute(name, returnValue);
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        }
        BindingContext bindingContext = result.getBindingContext();
        updateBindingResult(bindingContext, exchange);
        return viewsMono.flatMap(views -> render(views, model.asMap(), bindingContext, exchange));
    });
}
Also used : Locale(java.util.Locale) HttpStatus(org.springframework.http.HttpStatus) BindingContext(org.springframework.web.reactive.BindingContext) Model(org.springframework.ui.Model) ArrayList(java.util.ArrayList) List(java.util.List) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 39 with ReactiveAdapter

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

the class ViewResolutionResultHandler method supports.

@Override
public boolean supports(HandlerResult result) {
    if (hasModelAnnotation(result.getReturnTypeSource())) {
        return true;
    }
    Class<?> type = result.getReturnType().toClass();
    ReactiveAdapter adapter = getAdapter(result);
    if (adapter != null) {
        if (adapter.isNoValue()) {
            return true;
        }
        type = result.getReturnType().getGeneric().toClass();
    }
    return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) || Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || View.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type));
}
Also used : Model(org.springframework.ui.Model) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 40 with ReactiveAdapter

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

the class ResponseEntityResultHandler method supports.

@Override
public boolean supports(HandlerResult result) {
    Class<?> valueType = resolveReturnValueType(result);
    if (isSupportedType(valueType)) {
        return true;
    }
    ReactiveAdapter adapter = getAdapter(result);
    return adapter != null && !adapter.isNoValue() && isSupportedType(result.getReturnType().getGeneric().toClass());
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

ReactiveAdapter (org.springframework.core.ReactiveAdapter)41 ResolvableType (org.springframework.core.ResolvableType)16 Mono (reactor.core.publisher.Mono)14 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)13 List (java.util.List)12 Assert (org.springframework.util.Assert)12 MethodParameter (org.springframework.core.MethodParameter)11 Nullable (org.springframework.lang.Nullable)11 MediaType (org.springframework.http.MediaType)10 Map (java.util.Map)8 Publisher (org.reactivestreams.Publisher)8 Flux (reactor.core.publisher.Flux)8 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 BindingContext (org.springframework.web.reactive.BindingContext)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 Collectors (java.util.stream.Collectors)5 HttpEntity (org.springframework.http.HttpEntity)5 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)5