use of com.blade.exception.BladeException in project blade by biezhi.
the class RouteMatcher method lookupRoute.
public Route lookupRoute(String httpMethod, String path) throws BladeException {
path = parsePath(path);
String routeKey = path + '#' + httpMethod.toUpperCase();
Route route = staticRoutes.get(routeKey);
if (null != route) {
return route;
}
route = staticRoutes.get(path + "#ALL");
if (null != route) {
return route;
}
Map<String, String> uriVariables = CollectionKit.newLinkedHashMap();
HttpMethod requestMethod = HttpMethod.valueOf(httpMethod);
try {
Matcher matcher = regexRoutePatterns.get(requestMethod).matcher(path);
boolean matched = matcher.matches();
if (!matched) {
matcher = regexRoutePatterns.get(HttpMethod.ALL).matcher(path);
matched = matcher.matches();
}
if (matched) {
int i;
for (i = 1; matcher.group(i) == null; i++) ;
FastRouteMappingInfo mappingInfo = regexRoutes.get(requestMethod).get(i);
route = mappingInfo.getRoute();
// find path variable
String uriVariable;
int j = 0;
while (++i <= matcher.groupCount() && (uriVariable = matcher.group(i)) != null) {
uriVariables.put(mappingInfo.getVariableNames().get(j++), uriVariable);
}
route.setPathParams(uriVariables);
LOGGER.trace("lookup path: " + path + " uri variables: " + uriVariables);
}
return route;
} catch (Exception e) {
throw new BladeException(e);
}
}
use of com.blade.exception.BladeException in project blade by biezhi.
the class RouteViewResolve method intercept.
public boolean intercept(Request request, Response response, Route route) throws BladeException {
Method actionMethod = route.getAction();
Object target = route.getTarget();
if (null == target) {
Class<?> clazz = route.getAction().getDeclaringClass();
target = ioc.getBean(clazz);
route.setTarget(target);
}
// execute
int len = actionMethod.getParameterTypes().length;
actionMethod.setAccessible(true);
try {
Object returnParam;
if (len > 0) {
Object[] args = MethodArgument.getArgs(request, response, actionMethod);
returnParam = ReflectKit.invokeMehod(target, actionMethod, args);
} else {
returnParam = ReflectKit.invokeMehod(target, actionMethod);
}
if (null != returnParam) {
Class<?> returnType = returnParam.getClass();
if (returnType == Boolean.class || returnType == boolean.class) {
return (Boolean) returnParam;
}
}
return true;
} catch (Exception e) {
throw new BladeException(e);
}
}
use of com.blade.exception.BladeException in project blade by biezhi.
the class Blade method routeConf.
/**
* Registration of a configuration file, e.g: "com.xxx.route","route.conf"
*
* @param basePackage controller package name
* @param conf Configuration file path, the configuration file must be in
* classpath
* @return return blade
*/
public Blade routeConf(String basePackage, String conf) {
try {
Assert.notBlank(basePackage);
Assert.notBlank(conf);
InputStream ins = Blade.class.getResourceAsStream("/" + conf);
ClassPathRouteLoader routesLoader = new ClassPathRouteLoader(ins);
routesLoader.setBasePackage(basePackage);
List<Route> routes = routesLoader.load();
routers.addRoutes(routes);
} catch (RouteException | ParseException e) {
throw new BladeException(e);
}
return this;
}
use of com.blade.exception.BladeException in project blade by biezhi.
the class RouteViewResolve method handle.
public void handle(Request request, Response response, Route route) throws Exception {
try {
Method actionMethod = route.getAction();
Object target = route.getTarget();
int len = actionMethod.getParameterTypes().length;
Object returnParam;
if (len > 0) {
Object[] args = MethodArgument.getArgs(request, response, actionMethod);
returnParam = ReflectKit.invokeMehod(target, actionMethod, args);
} else {
returnParam = ReflectKit.invokeMehod(target, actionMethod);
}
if (null != returnParam) {
Class<?> returnType = returnParam.getClass();
RestController restController = target.getClass().getAnnotation(RestController.class);
JSON json = actionMethod.getAnnotation(JSON.class);
if (null != restController || null != json) {
response.json(viewSettings.toJSONString(returnParam));
} else {
if (returnType == String.class) {
response.render(returnParam.toString());
} else if (returnType == ModelAndView.class) {
ModelAndView modelAndView = (ModelAndView) returnParam;
response.render(modelAndView);
}
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new BladeException(e.getCause());
} catch (Exception e) {
throw e;
}
}
Aggregations