Search in sources :

Example 11 with ReactiveAdapter

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

the class HandlerMethodArgumentResolverSupport method checkAnnotatedParamNoReactiveWrapper.

/**
	 * Evaluate the {@code Predicate} on the method parameter type if it has the
	 * given annotation, nesting within {@link java.util.Optional} if necessary,
	 * but raise an {@code IllegalStateException} if the same matches the generic
	 * type within a reactive type wrapper.
	 */
protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper(MethodParameter param, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) {
    A annotation = param.getParameterAnnotation(annotationType);
    if (annotation == null) {
        return false;
    }
    param = param.nestedIfOptional();
    Class<?> type = param.getNestedParameterType();
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
    if (adapter != null) {
        assertHasValues(adapter, param);
        param = param.nested();
        type = param.getNestedParameterType();
    }
    if (typePredicate.test(annotation, type)) {
        if (adapter == null) {
            return true;
        }
        throw getReactiveWrapperError(param);
    }
    return false;
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 12 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) {
        Assert.isTrue(!adapter.isMultiValue(), "Only single-value async return type supported.");
        valueMono = result.getReturnValue().map(value -> Mono.from(adapter.toPublisher(value))).orElse(Mono.empty());
        valueType = adapter.isNoValue() ? ResolvableType.forClass(Void.class) : result.getReturnType().getGeneric(0);
    } else {
        valueMono = Mono.justOrEmpty(result.getReturnValue());
        valueType = result.getReturnType();
    }
    return valueMono.otherwiseIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO).then(returnValue -> {
        Mono<List<View>> viewsMono;
        Model model = result.getModel();
        MethodParameter parameter = result.getReturnTypeSource();
        Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
        Locale locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
        Class<?> clazz = valueType.getRawClass();
        if (clazz == null) {
            clazz = returnValue.getClass();
        }
        if (returnValue == NO_VALUE || Void.class.equals(clazz) || void.class.equals(clazz)) {
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (Model.class.isAssignableFrom(clazz)) {
            model.addAllAttributes(((Model) returnValue).asMap());
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (Map.class.isAssignableFrom(clazz)) {
            model.addAllAttributes((Map<String, ?>) returnValue);
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        } else if (View.class.isAssignableFrom(clazz)) {
            viewsMono = Mono.just(Collections.singletonList((View) returnValue));
        } else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
            viewsMono = resolveViews(returnValue.toString(), locale);
        } else {
            String name = getNameForReturnValue(clazz, parameter);
            model.addAttribute(name, returnValue);
            viewsMono = resolveViews(getDefaultViewName(exchange), locale);
        }
        return resolveAsyncAttributes(model.asMap()).doOnSuccess(aVoid -> addBindingResult(result.getBindingContext(), exchange)).then(viewsMono).then(views -> render(views, model.asMap(), exchange));
    });
}
Also used : Locale(java.util.Locale) Model(org.springframework.ui.Model) ArrayList(java.util.ArrayList) List(java.util.List) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 13 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().getRawClass();
    ReactiveAdapter adapter = getAdapter(result);
    if (adapter != null) {
        if (adapter.isNoValue()) {
            return true;
        }
        type = result.getReturnType().getGeneric(0).getRawClass();
    }
    return (CharSequence.class.isAssignableFrom(type) || View.class.isAssignableFrom(type) || Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type));
}
Also used : Map(java.util.Map) 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