Search in sources :

Example 1 with RouteException

use of com.blade.exception.RouteException in project blade by biezhi.

the class MethodArgument method getArgs.

public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
    Class<?>[] parameters = actionMethod.getParameterTypes();
    Annotation[][] annotations = actionMethod.getParameterAnnotations();
    Object[] args = new Object[parameters.length];
    actionMethod.setAccessible(true);
    String[] paramaterNames = AsmKit.getMethodParamNames(actionMethod);
    for (int i = 0, len = parameters.length; i < len; i++) {
        Class<?> argType = parameters[i];
        if (argType == Request.class) {
            args[i] = request;
            continue;
        }
        if (argType == Response.class) {
            args[i] = response;
            continue;
        }
        if (argType == Session.class) {
            args[i] = request.session();
            continue;
        }
        if (argType == ModelAndView.class) {
            args[i] = new ModelAndView();
            continue;
        }
        if (argType == Map.class) {
            args[i] = request.querys();
            continue;
        }
        Annotation annotation = annotations[i][0];
        if (null != annotation) {
            // query param
            if (annotation.annotationType() == QueryParam.class) {
                QueryParam queryParam = (QueryParam) annotation;
                String paramName = queryParam.value();
                String val = request.query(paramName);
                boolean required = queryParam.required();
                if (StringKit.isBlank(paramName)) {
                    assert paramaterNames != null;
                    paramName = paramaterNames[i];
                    val = request.query(paramName);
                }
                if (StringKit.isBlank(val)) {
                    val = queryParam.defaultValue();
                }
                if (required && StringKit.isBlank(val)) {
                    throw new RouteException("query param [" + paramName + "] not is empty.");
                }
                args[i] = getRequestParam(argType, val);
                continue;
            }
            // path param
            if (annotation.annotationType() == PathParam.class) {
                PathParam pathParam = (PathParam) annotation;
                String paramName = pathParam.value();
                String val = request.pathParam(paramName);
                if (StringKit.isBlank(paramName)) {
                    assert paramaterNames != null;
                    paramName = paramaterNames[i];
                    val = request.pathParam(paramName);
                }
                if (StringKit.isBlank(val)) {
                    val = pathParam.defaultValue();
                }
                args[i] = getRequestParam(argType, val);
            }
            // header param
            if (annotation.annotationType() == HeaderParam.class) {
                HeaderParam headerParam = (HeaderParam) annotation;
                String paramName = headerParam.value();
                String val = request.header(paramName);
                boolean required = headerParam.required();
                if (StringKit.isBlank(paramName)) {
                    assert paramaterNames != null;
                    paramName = paramaterNames[i];
                    val = request.header(paramName);
                }
                if (StringKit.isBlank(val)) {
                    val = headerParam.defaultValue();
                }
                if (required && StringKit.isBlank(val)) {
                    throw new RouteException("header param [" + paramName + "] not is empty.");
                }
                args[i] = getRequestParam(argType, val);
                continue;
            }
            // cookie param
            if (annotation.annotationType() == CookieParam.class) {
                CookieParam cookieParam = (CookieParam) annotation;
                String paramName = cookieParam.value();
                String val = request.cookie(paramName);
                boolean required = cookieParam.required();
                if (StringKit.isBlank(paramName)) {
                    assert paramaterNames != null;
                    paramName = paramaterNames[i];
                    val = request.cookie(paramName);
                }
                if (StringKit.isBlank(val)) {
                    val = cookieParam.defaultValue();
                }
                if (required && StringKit.isBlank(val)) {
                    throw new RouteException("cookie param [" + paramName + "] not is empty.");
                }
                args[i] = getRequestParam(argType, val);
                continue;
            }
            // form multipart
            if (annotation.annotationType() == MultipartParam.class && argType == FileItem.class) {
                MultipartParam multipartParam = (MultipartParam) annotation;
                String paramName = multipartParam.value();
                FileItem val = request.fileItem(paramName);
                if (StringKit.isBlank(paramName)) {
                    assert paramaterNames != null;
                    paramName = paramaterNames[i];
                    val = request.fileItem(paramName);
                }
                args[i] = val;
                continue;
            }
        }
    }
    return args;
}
Also used : RouteException(com.blade.exception.RouteException) ModelAndView(com.blade.mvc.view.ModelAndView) Annotation(java.lang.annotation.Annotation) FileItem(com.blade.mvc.multipart.FileItem)

Example 2 with RouteException

use of com.blade.exception.RouteException in project blade by biezhi.

the class ClassPathControllerLoader method load.

@Override
public Object load(String controllerName) throws RouteException {
    String className = basePackage + controllerName;
    try {
        // Load controller instance
        Class<?> controllerClass = classLoader.loadClass(className);
        Object controller = ioc.getBean(controllerClass);
        if (null == controller) {
            ioc.addBean(controllerClass);
            controller = ioc.getBean(controllerClass);
        }
        return controller;
    } catch (Exception e) {
        throw new RouteException(e);
    }
}
Also used : RouteException(com.blade.exception.RouteException) RouteException(com.blade.exception.RouteException)

Example 3 with RouteException

use of com.blade.exception.RouteException 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)

Aggregations

RouteException (com.blade.exception.RouteException)3 BladeException (com.blade.exception.BladeException)1 FileItem (com.blade.mvc.multipart.FileItem)1 ClassPathRouteLoader (com.blade.mvc.route.loader.ClassPathRouteLoader)1 ModelAndView (com.blade.mvc.view.ModelAndView)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 ParseException (java.text.ParseException)1