Search in sources :

Example 1 with ReactiveAdapter

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

the class ErrorsMethodArgumentResolver method getModelAttributeName.

private String getModelAttributeName(MethodParameter parameter) {
    Assert.isTrue(parameter.getParameterIndex() > 0, "Errors argument must be immediately after a model attribute argument.");
    int index = parameter.getParameterIndex() - 1;
    MethodParameter attributeParam = new MethodParameter(parameter.getMethod(), index);
    Class<?> attributeType = attributeParam.getParameterType();
    ResolvableType type = ResolvableType.forMethodParameter(attributeParam);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
    Assert.isNull(adapter, "Errors/BindingResult cannot be used with an async model attribute. " + "Either declare the model attribute without the async wrapper type " + "or handle WebExchangeBindException through the async type.");
    ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
    if (annot != null && StringUtils.hasText(annot.value())) {
        return annot.value();
    }
    // TODO: Conventions does not deal with async wrappers
    return ClassUtils.getShortNameAsProperty(attributeType);
}
Also used : ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 2 with ReactiveAdapter

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

the class ModelAttributeMethodArgumentResolver method getAttributeMono.

private Mono<?> getAttributeMono(String attributeName, ResolvableType attributeType, Model model) {
    Object attribute = model.asMap().get(attributeName);
    if (attribute == null) {
        attribute = BeanUtils.instantiateClass(attributeType.getRawClass());
    }
    ReactiveAdapter adapterFrom = getAdapterRegistry().getAdapter(null, attribute);
    if (adapterFrom != null) {
        Assert.isTrue(!adapterFrom.isMultiValue(), "Data binding supports single-value async types.");
        return Mono.from(adapterFrom.toPublisher(attribute));
    } else {
        return Mono.justOrEmpty(attribute);
    }
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 3 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 4 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 5 with ReactiveAdapter

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

the class ReactiveAdapterRegistryTests method getAdapterForReactiveSubType.

@Test
public void getAdapterForReactiveSubType() throws Exception {
    ReactiveAdapter adapter1 = getAdapter(Flux.class);
    ReactiveAdapter adapter2 = getAdapter(FluxProcessor.class);
    assertSame(adapter1, adapter2);
    this.registry.registerReactiveType(ReactiveTypeDescriptor.multiValue(FluxProcessor.class, FluxProcessor::empty), o -> (FluxProcessor<?, ?>) o, FluxProcessor::from);
    ReactiveAdapter adapter3 = getAdapter(FluxProcessor.class);
    assertNotNull(adapter3);
    assertNotSame(adapter1, adapter3);
}
Also used : FluxProcessor(reactor.core.publisher.FluxProcessor) ReactiveAdapter(org.springframework.core.ReactiveAdapter) Test(org.junit.Test)

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