Search in sources :

Example 1 with RouteHandler

use of com.blade.mvc.handler.RouteHandler in project blade by biezhi.

the class RouteMethodHandler method invokeHook.

/**
 * invoke hooks
 *
 * @param hooks   webHook list
 * @param context http request
 * @return return invoke hook is abort
 */
private boolean invokeHook(List<Route> hooks, RouteContext context) throws Exception {
    for (Route hook : hooks) {
        if (hook.getTargetType() == RouteHandler.class) {
            RouteHandler routeHandler = (RouteHandler) hook.getTarget();
            routeHandler.handle(context);
            if (context.isAbort()) {
                return false;
            }
        } else if (hook.getTargetType() == RouteHandler0.class) {
            RouteHandler0 routeHandler = (RouteHandler0) hook.getTarget();
            routeHandler.handle(context.request(), context.response());
        } else {
            boolean flag = this.invokeHook(context, hook);
            if (!flag)
                return false;
        }
    }
    return true;
}
Also used : RouteHandler0(com.blade.mvc.handler.RouteHandler0) Route(com.blade.mvc.route.Route) RouteHandler(com.blade.mvc.handler.RouteHandler)

Example 2 with RouteHandler

use of com.blade.mvc.handler.RouteHandler in project blade by biezhi.

the class RouteMethodHandler method routeHandle.

/**
 * Actual routing method execution
 *
 * @param context route context
 */
private void routeHandle(RouteContext context) {
    Object target = context.routeTarget();
    if (null == target) {
        Class<?> clazz = context.routeAction().getDeclaringClass();
        target = WebContext.blade().getBean(clazz);
    }
    if (context.targetType() == RouteHandler.class) {
        RouteHandler routeHandler = (RouteHandler) target;
        routeHandler.handle(context);
    } else if (context.targetType() == RouteHandler0.class) {
        RouteHandler0 routeHandler = (RouteHandler0) target;
        routeHandler.handle(context.request(), context.response());
    } else {
        Method actionMethod = context.routeAction();
        Class<?> returnType = actionMethod.getReturnType();
        Path path = target.getClass().getAnnotation(Path.class);
        JSON JSON = actionMethod.getAnnotation(JSON.class);
        boolean isRestful = (null != JSON) || (null != path && path.restful());
        // if request is restful and not InternetExplorer userAgent
        if (isRestful) {
            if (!context.isIE()) {
                context.contentType(Const.CONTENT_TYPE_JSON);
            } else {
                context.contentType(Const.CONTENT_TYPE_HTML);
            }
        }
        int len = actionMethod.getParameterTypes().length;
        MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
        Object returnParam = methodAccess.invoke(target, actionMethod.getName(), len > 0 ? context.routeParameters() : null);
        if (null == returnParam) {
            return;
        }
        if (isRestful) {
            context.json(returnParam);
            return;
        }
        if (returnType == String.class) {
            context.body(ViewBody.of(new ModelAndView(returnParam.toString())));
            return;
        }
        if (returnType == ModelAndView.class) {
            context.body(ViewBody.of((ModelAndView) returnParam));
        }
    }
}
Also used : Path(com.blade.mvc.annotation.Path) ModelAndView(com.blade.mvc.ui.ModelAndView) RouteHandler0(com.blade.mvc.handler.RouteHandler0) JSON(com.blade.mvc.annotation.JSON) Method(java.lang.reflect.Method) MethodAccess(com.blade.reflectasm.MethodAccess) RouteHandler(com.blade.mvc.handler.RouteHandler)

Example 3 with RouteHandler

use of com.blade.mvc.handler.RouteHandler in project blade by biezhi.

the class BladeTest method testRouteCode.

@Test
public void testRouteCode() {
    RouteHandler routeHandler = mock(RouteHandler.class);
    blade.get("/", routeHandler);
    blade.post("/", routeHandler);
    blade.delete("/", routeHandler);
    blade.put("/", routeHandler);
    blade.before("/", routeHandler);
    blade.after("/", routeHandler);
    blade.routeMatcher().register();
    Assert.assertNotNull(blade.routeMatcher().lookupRoute("GET", "/"));
    Assert.assertNotNull(blade.routeMatcher().lookupRoute("POST", "/"));
    Assert.assertNotNull(blade.routeMatcher().lookupRoute("DELETE", "/"));
    Assert.assertNotNull(blade.routeMatcher().lookupRoute("PUT", "/"));
    Assert.assertNotNull(blade.routeMatcher().getBefore("/"));
    Assert.assertNotNull(blade.routeMatcher().getAfter("/"));
}
Also used : RouteHandler(com.blade.mvc.handler.RouteHandler) Test(org.junit.Test)

Aggregations

RouteHandler (com.blade.mvc.handler.RouteHandler)3 RouteHandler0 (com.blade.mvc.handler.RouteHandler0)2 JSON (com.blade.mvc.annotation.JSON)1 Path (com.blade.mvc.annotation.Path)1 Route (com.blade.mvc.route.Route)1 ModelAndView (com.blade.mvc.ui.ModelAndView)1 MethodAccess (com.blade.reflectasm.MethodAccess)1 Method (java.lang.reflect.Method)1 Test (org.junit.Test)1