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