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;
}
Aggregations