Search in sources :

Example 1 with MethodAccess

use of com.blade.reflectasm.MethodAccess 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 2 with MethodAccess

use of com.blade.reflectasm.MethodAccess in project blade by biezhi.

the class RouteMethodHandler method invokeHook.

/**
 * Invoke WebHook
 *
 * @param context   current execute route handler signature
 * @param hookRoute current webhook route handler
 * @return Return true then next handler, and else interrupt request
 * @throws Exception throw like parse param exception
 */
private boolean invokeHook(RouteContext context, Route hookRoute) throws Exception {
    Method hookMethod = hookRoute.getAction();
    Object target = WebContext.blade().ioc().getBean(hookRoute.getTargetType());
    if (null == target) {
        Class<?> clazz = hookRoute.getAction().getDeclaringClass();
        target = WebContext.blade().ioc().getBean(clazz);
        hookRoute.setTarget(target);
    }
    // execute
    int len = hookMethod.getParameterTypes().length;
    hookMethod.setAccessible(true);
    Object returnParam;
    if (len > 0) {
        if (len == 1) {
            MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
            returnParam = methodAccess.invoke(target, hookMethod.getName(), context);
        } else if (len == 2) {
            MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
            returnParam = methodAccess.invoke(target, hookMethod.getName(), context.request(), context.response());
        } else {
            throw new InternalErrorException("Bad web hook structure");
        }
    } else {
        returnParam = ReflectKit.invokeMethod(target, hookMethod);
    }
    if (null == returnParam)
        return true;
    Class<?> returnType = returnParam.getClass();
    if (returnType == Boolean.class || returnType == boolean.class) {
        return Boolean.valueOf(returnParam.toString());
    }
    return true;
}
Also used : Method(java.lang.reflect.Method) InternalErrorException(com.blade.exception.InternalErrorException) MethodAccess(com.blade.reflectasm.MethodAccess)

Aggregations

MethodAccess (com.blade.reflectasm.MethodAccess)2 Method (java.lang.reflect.Method)2 InternalErrorException (com.blade.exception.InternalErrorException)1 JSON (com.blade.mvc.annotation.JSON)1 Path (com.blade.mvc.annotation.Path)1 RouteHandler (com.blade.mvc.handler.RouteHandler)1 RouteHandler0 (com.blade.mvc.handler.RouteHandler0)1 ModelAndView (com.blade.mvc.ui.ModelAndView)1