Search in sources :

Example 1 with NotFoundException

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

the class MethodArgumentN method getArgs.

public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
    int parameterCount = 0;
    for (final Parameter parameter : actionMethod.getParameters()) {
        System.out.println("\targ" + parameterCount++ + ": " + (parameter.isNamePresent() ? parameter.getName() : "Parameter Name not provided,") + (isParameterFinal(parameter) ? " IS " : " is NOT ") + "final, type " + parameter.getType().getCanonicalName() + ", and parameterized type of " + parameter.getParameterizedType() + " and " + (parameter.isVarArgs() ? "IS " : "is NOT ") + "variable.");
    }
    Parameter[] parameters = actionMethod.getParameters();
    Object[] args = new Object[parameters.length];
    actionMethod.setAccessible(true);
    for (int i = 0, len = parameters.length; i < len; i++) {
        Parameter parameter = parameters[i];
        Class<?> argType = parameter.getType();
        if (argType == Request.class) {
            args[i] = request;
            continue;
        }
        if (argType == Response.class) {
            args[i] = response;
            continue;
        }
        if (argType == ModelAndView.class) {
            args[i] = new ModelAndView();
            continue;
        }
        if (argType == Map.class) {
            args[i] = request.querys();
            continue;
        }
        QueryParam queryParam = parameter.getAnnotation(QueryParam.class);
        if (null != queryParam) {
            String paramName = queryParam.value();
            String val = request.query(paramName);
            if (StringKit.isBlank(paramName)) {
                paramName = parameter.getName();
                val = request.query(paramName);
            }
            if (StringKit.isBlank(val)) {
                val = queryParam.defaultValue();
            }
            args[i] = MethodArgument.getRequestParam(argType, val);
        }
        PathParam pathParam = parameter.getAnnotation(PathParam.class);
        if (null != pathParam) {
            String paramName = pathParam.value();
            String val = request.pathParam(paramName);
            if (StringKit.isBlank(paramName)) {
                paramName = parameter.getName();
                val = request.pathParam(paramName);
            }
            if (StringKit.isBlank(val)) {
                throw new NotFoundException("path param [" + paramName + "] is null");
            }
            args[i] = MethodArgument.getRequestParam(argType, val);
        }
        HeaderParam headerParam = parameter.getAnnotation(HeaderParam.class);
        if (null != headerParam) {
            String paramName = headerParam.value();
            String val = request.header(paramName);
            if (StringKit.isBlank(paramName)) {
                paramName = parameter.getName();
                val = request.header(paramName);
            }
            args[i] = MethodArgument.getRequestParam(argType, val);
        }
        CookieParam cookieParam = parameter.getAnnotation(CookieParam.class);
        if (null != cookieParam) {
            String paramName = cookieParam.value();
            String val = request.cookie(paramName);
            if (StringKit.isBlank(paramName)) {
                paramName = parameter.getName();
                val = request.cookie(paramName);
            }
            args[i] = MethodArgument.getRequestParam(argType, val);
        }
        MultipartParam multipartParam = parameter.getAnnotation(MultipartParam.class);
        if (null != multipartParam && argType == FileItem.class) {
            String paramName = multipartParam.value();
            FileItem val = request.fileItem(paramName);
            if (StringKit.isBlank(paramName)) {
                paramName = parameter.getName();
                val = request.fileItem(paramName);
            }
            args[i] = val;
        }
    }
    return args;
}
Also used : ModelAndView(com.blade.mvc.view.ModelAndView) NotFoundException(com.blade.exception.NotFoundException) FileItem(com.blade.mvc.multipart.FileItem) Parameter(java.lang.reflect.Parameter)

Aggregations

NotFoundException (com.blade.exception.NotFoundException)1 FileItem (com.blade.mvc.multipart.FileItem)1 ModelAndView (com.blade.mvc.view.ModelAndView)1 Parameter (java.lang.reflect.Parameter)1