Search in sources :

Example 1 with InternalErrorException

use of com.blade.exception.InternalErrorException 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

InternalErrorException (com.blade.exception.InternalErrorException)1 MethodAccess (com.blade.reflectasm.MethodAccess)1 Method (java.lang.reflect.Method)1