Search in sources :

Example 1 with ActionMappingAnnotationHandler

use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-infrastructure by TAKETODAY.

the class ReturnValueHandlerManagerTests method addHandler.

@Test
void addHandler() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.getHandlers()).hasSize(1);
    HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(List.of(highestValueHandler));
    assertThat(manager.getHandlers()).hasSize(2);
    assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
    assertThat(manager.getByReturnValue("")).isNull();
    assertThat(manager.getByReturnValue(null)).isNull();
    assertThat(manager.getHandler("")).isNull();
    // getHandler(handler)
    HandlerMethod handler = Mockito.mock(HandlerMethod.class);
    Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
    ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {

        @Override
        public Object getHandlerObject() {
            return null;
        }
    };
    assertThat(manager.getHandler(annotationHandler)).isNotNull();
    // obtainHandler
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    // sort
    // returnValueHandler.setOrder(2);
    // highestValueHandler.setOrder(1);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
    // returnValueHandler.setOrder(1);
    // highestValueHandler.setOrder(2);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler) HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 2 with ActionMappingAnnotationHandler

use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.

the class HandlerMethodRegistry method buildHandlerMethod.

/**
 * Set Action Mapping
 *
 * @param beanName bean name
 * @param method Action or Handler
 * @param beanClass Controller
 * @param controllerMapping find mapping on class
 */
protected void buildHandlerMethod(String beanName, Method method, Class<?> beanClass, @Nullable MergedAnnotation<ActionMapping> controllerMapping) {
    MergedAnnotation<ActionMapping> annotation = MergedAnnotations.from(method).get(ActionMapping.class);
    if (annotation.isPresent()) {
        // build HandlerMethod
        ActionMappingAnnotationHandler handler = createHandler(beanName, beanClass, method);
        // do mapping url
        mappingHandlerMethod(handler, controllerMapping, annotation);
    }
}
Also used : ActionMapping(cn.taketoday.web.annotation.ActionMapping) ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler)

Example 3 with ActionMappingAnnotationHandler

use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.

the class ReturnValueHandlerManagerTests method addHandler.

@Test
void addHandler() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.getHandlers()).hasSize(1);
    HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(List.of(highestValueHandler));
    assertThat(manager.getHandlers()).hasSize(2);
    assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
    assertThat(manager.getByReturnValue("")).isNull();
    assertThat(manager.getByReturnValue(null)).isNull();
    assertThat(manager.getHandler("")).isNull();
    // getHandler(handler)
    HandlerMethod handler = Mockito.mock(HandlerMethod.class);
    Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
    ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {

        @Override
        public Object getHandlerObject() {
            return null;
        }
    };
    assertThat(manager.getHandler(annotationHandler)).isNotNull();
    // obtainHandler
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    // sort
    // returnValueHandler.setOrder(2);
    // highestValueHandler.setOrder(1);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
    // returnValueHandler.setOrder(1);
    // highestValueHandler.setOrder(2);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler) HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 4 with ActionMappingAnnotationHandler

use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-framework by TAKETODAY.

the class ViewControllerHandlerAdapter method invokeHandlerMethod.

protected Object invokeHandlerMethod(final RequestContext context, final ViewController view) throws Throwable {
    final ActionMappingAnnotationHandler handler = view.getHandler();
    final Object result = handler.invokeHandler(context);
    // If return type is void or result is null use xml configuration's resource
    if (result == null || handler.getMethod().isReturn(void.class)) {
        return view.getResource();
    }
    handler.handleReturnValue(context, handler, result);
    return NONE_RETURN_VALUE;
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler)

Example 5 with ActionMappingAnnotationHandler

use of cn.taketoday.web.handler.method.ActionMappingAnnotationHandler in project today-infrastructure by TAKETODAY.

the class ViewControllerHandlerAdapter method invokeHandlerMethod.

protected Object invokeHandlerMethod(final RequestContext context, final ViewController view) throws Throwable {
    final ActionMappingAnnotationHandler handler = view.getHandler();
    final Object result = handler.invokeHandler(context);
    // If return type is void or result is null use xml configuration's resource
    if (result == null || handler.getMethod().isReturn(void.class)) {
        return view.getResource();
    }
    handler.handleReturnValue(context, handler, result);
    return NONE_RETURN_VALUE;
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler)

Aggregations

ActionMappingAnnotationHandler (cn.taketoday.web.handler.method.ActionMappingAnnotationHandler)6 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)2 HttpStatusReturnValueHandler (cn.taketoday.web.handler.result.HttpStatusReturnValueHandler)2 Test (org.junit.jupiter.api.Test)2 ActionMapping (cn.taketoday.web.annotation.ActionMapping)1 PathPattern (cn.taketoday.web.util.pattern.PathPattern)1