Search in sources :

Example 1 with BladeException

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);
    }
}
Also used : BladeException(com.blade.exception.BladeException) Matcher(java.util.regex.Matcher) HttpMethod(com.blade.mvc.http.HttpMethod) URISyntaxException(java.net.URISyntaxException) BladeException(com.blade.exception.BladeException)

Example 2 with BladeException

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);
    }
}
Also used : BladeException(com.blade.exception.BladeException) Method(java.lang.reflect.Method) BladeException(com.blade.exception.BladeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with BladeException

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;
}
Also used : RouteException(com.blade.exception.RouteException) BladeException(com.blade.exception.BladeException) InputStream(java.io.InputStream) ClassPathRouteLoader(com.blade.mvc.route.loader.ClassPathRouteLoader) ParseException(java.text.ParseException)

Example 4 with BladeException

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;
    }
}
Also used : BladeException(com.blade.exception.BladeException) ModelAndView(com.blade.mvc.view.ModelAndView) RestController(com.blade.mvc.annotation.RestController) JSON(com.blade.mvc.annotation.JSON) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BladeException(com.blade.exception.BladeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BladeException (com.blade.exception.BladeException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 RouteException (com.blade.exception.RouteException)1 JSON (com.blade.mvc.annotation.JSON)1 RestController (com.blade.mvc.annotation.RestController)1 HttpMethod (com.blade.mvc.http.HttpMethod)1 ClassPathRouteLoader (com.blade.mvc.route.loader.ClassPathRouteLoader)1 ModelAndView (com.blade.mvc.view.ModelAndView)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 Matcher (java.util.regex.Matcher)1