Search in sources :

Example 1 with InvocableHandlerMethod

use of org.springframework.web.reactive.result.method.InvocableHandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    Assert.notNull(handler, "Expected handler");
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    BindingContext bindingContext = new InitBinderBindingContext(getWebBindingInitializer(), getBinderMethods(handlerMethod));
    return this.modelInitializer.initModel(bindingContext, getAttributeMethods(handlerMethod), exchange).then(() -> {
        Function<Throwable, Mono<HandlerResult>> exceptionHandler = ex -> handleException(exchange, handlerMethod, bindingContext, ex);
        InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
        invocable.setArgumentResolvers(getArgumentResolvers());
        return invocable.invoke(exchange, bindingContext).doOnNext(result -> result.setExceptionHandler(exceptionHandler)).otherwise(exceptionHandler);
    });
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BindingContext(org.springframework.web.reactive.BindingContext) MethodIntrospector.selectMethods(org.springframework.core.MethodIntrospector.selectMethods) Function(java.util.function.Function) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) LinkedHashMap(java.util.LinkedHashMap) HandlerMethod(org.springframework.web.method.HandlerMethod) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) Map(java.util.Map) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Method(java.lang.reflect.Method) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HandlerAdapter(org.springframework.web.reactive.HandlerAdapter) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) StringDecoder(org.springframework.core.codec.StringDecoder) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) Mono(reactor.core.publisher.Mono) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ApplicationContext(org.springframework.context.ApplicationContext) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) HandlerResult(org.springframework.web.reactive.HandlerResult) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Objects(java.util.Objects) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) InitBinder(org.springframework.web.bind.annotation.InitBinder) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) Assert(org.springframework.util.Assert) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)

Example 2 with InvocableHandlerMethod

use of org.springframework.web.reactive.result.method.InvocableHandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method getExceptionHandlerMethod.

private InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {
    Class<?> handlerType = handlerMethod.getBeanType();
    ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new);
    return Optional.ofNullable(resolver.resolveMethodByThrowable(ex)).map(method -> createHandlerMethod(handlerMethod.getBean(), method)).orElseGet(() -> this.exceptionHandlerAdviceCache.entrySet().stream().map(entry -> {
        if (entry.getKey().isApplicableToBeanType(handlerType)) {
            Method method = entry.getValue().resolveMethodByThrowable(ex);
            if (method != null) {
                Object bean = entry.getKey().resolveBean();
                return createHandlerMethod(bean, method);
            }
        }
        return null;
    }).filter(Objects::nonNull).findFirst().orElse(null));
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BindingContext(org.springframework.web.reactive.BindingContext) MethodIntrospector.selectMethods(org.springframework.core.MethodIntrospector.selectMethods) Function(java.util.function.Function) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) LinkedHashMap(java.util.LinkedHashMap) HandlerMethod(org.springframework.web.method.HandlerMethod) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) Map(java.util.Map) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Method(java.lang.reflect.Method) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HandlerAdapter(org.springframework.web.reactive.HandlerAdapter) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) StringDecoder(org.springframework.core.codec.StringDecoder) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) Mono(reactor.core.publisher.Mono) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ApplicationContext(org.springframework.context.ApplicationContext) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) HandlerResult(org.springframework.web.reactive.HandlerResult) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Objects(java.util.Objects) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) InitBinder(org.springframework.web.bind.annotation.InitBinder) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) Assert(org.springframework.util.Assert) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)

Example 3 with InvocableHandlerMethod

use of org.springframework.web.reactive.result.method.InvocableHandlerMethod in project spring-framework by spring-projects.

the class ModelInitializerTests method basic.

@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
    TestController controller = new TestController();
    Validator validator = mock(Validator.class);
    controller.setValidator(validator);
    List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
    List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
    WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
    this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
    WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
    assertEquals(Collections.singletonList(validator), binder.getValidators());
    Map<String, Object> model = bindingContext.getModel().asMap();
    assertEquals(5, model.size());
    Object value = model.get("bean");
    assertEquals("Bean", ((TestBean) value).getName());
    value = model.get("monoBean");
    assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName());
    value = model.get("singleBean");
    assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName());
    value = model.get("voidMethodBean");
    assertEquals("Void Method Bean", ((TestBean) value).getName());
    value = model.get("voidMonoMethodBean");
    assertEquals("Void Mono Method Bean", ((TestBean) value).getName());
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Test(org.junit.Test)

Example 4 with InvocableHandlerMethod

use of org.springframework.web.reactive.result.method.InvocableHandlerMethod in project spring-framework by spring-projects.

the class ModelInitializerTests method toInvocable.

private InvocableHandlerMethod toInvocable(Object controller, Method method) {
    ModelArgumentResolver resolver = new ModelArgumentResolver(new ReactiveAdapterRegistry());
    InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(controller, method);
    handlerMethod.setArgumentResolvers(Collections.singletonList(resolver));
    return handlerMethod;
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry)

Example 5 with InvocableHandlerMethod

use of org.springframework.web.reactive.result.method.InvocableHandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method handleException.

private Mono<HandlerResult> handleException(ServerWebExchange exchange, HandlerMethod handlerMethod, BindingContext bindingContext, Throwable ex) {
    InvocableHandlerMethod invocable = getExceptionHandlerMethod(ex, handlerMethod);
    if (invocable != null) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Invoking @ExceptionHandler method: " + invocable.getMethod());
            }
            bindingContext.getModel().asMap().clear();
            Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
            return invocable.invoke(exchange, bindingContext, cause, handlerMethod);
        } catch (Throwable invocationEx) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to invoke: " + invocable.getMethod(), invocationEx);
            }
        }
    }
    return Mono.error(ex);
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)

Aggregations

InvocableHandlerMethod (org.springframework.web.reactive.result.method.InvocableHandlerMethod)6 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)6 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)3 WebBindingInitializer (org.springframework.web.bind.support.WebBindingInitializer)3 BindingContext (org.springframework.web.reactive.BindingContext)3 Mono (reactor.core.publisher.Mono)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Function (java.util.function.Function)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 InitializingBean (org.springframework.beans.factory.InitializingBean)2 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2