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