Search in sources :

Example 1 with ModelAndView

use of com.blade.mvc.ui.ModelAndView in project blade by biezhi.

the class RouteActionArguments method getCustomType.

private static Object getCustomType(Parameter parameter, String paramName, RouteContext context) {
    Type argType = parameter.getParameterizedType();
    if (argType == RouteContext.class) {
        return context;
    } else if (argType == Request.class) {
        return context.request();
    } else if (argType == Response.class) {
        return context.response();
    } else if (argType == Session.class || argType == HttpSession.class) {
        return context.request().session();
    } else if (argType == FileItem.class) {
        return new ArrayList<>(context.request().fileItems().values()).get(0);
    } else if (argType == ModelAndView.class) {
        return new ModelAndView();
    } else if (argType == Map.class) {
        return context.request().parameters();
    } else if (argType == Optional.class) {
        ParameterizedType firstParam = (ParameterizedType) parameter.getParameterizedType();
        Type paramsOfFirstGeneric = firstParam.getActualTypeArguments()[0];
        Class<?> modelType = ReflectKit.form(paramsOfFirstGeneric.getTypeName());
        return Optional.ofNullable(parseModel(modelType, context.request(), null));
    } else if (ParameterizedType.class.isInstance(argType)) {
        String name = parameter.getName();
        List<String> values = context.request().parameters().get(name);
        return getParameterizedTypeValues(values, argType);
    } else if (ReflectKit.isArray(argType)) {
        List<String> values = context.request().parameters().get(paramName);
        if (null == values) {
            return null;
        }
        Class arrayCls = (Class) argType;
        Object aObject = Array.newInstance(arrayCls.getComponentType(), values.size());
        for (int i = 0; i < values.size(); i++) {
            Array.set(aObject, i, ReflectKit.convert(arrayCls.getComponentType(), values.get(i)));
        }
        return aObject;
    } else {
        return parseModel(ReflectKit.typeToClass(argType), context.request(), null);
    }
}
Also used : Request(com.blade.mvc.http.Request) ModelAndView(com.blade.mvc.ui.ModelAndView)

Example 2 with ModelAndView

use of com.blade.mvc.ui.ModelAndView in project blade by biezhi.

the class DefaultExceptionHandler method handleBladeException.

protected void handleBladeException(BladeException e, Request request, Response response) {
    var blade = WebContext.blade();
    response.status(e.getStatus());
    var modelAndView = new ModelAndView();
    modelAndView.add("title", e.getStatus() + " " + e.getName());
    modelAndView.add("message", e.getMessage());
    if (null != e.getCause()) {
        request.attribute(VARIABLE_STACKTRACE, getStackTrace(e));
    }
    if (e.getStatus() == InternalErrorException.STATUS) {
        log.error("", e);
        this.render500(request, response);
    }
    String paddingMethod = BladeCache.getPaddingMethod(request.method());
    if (e.getStatus() == NotFoundException.STATUS) {
        log404(log, paddingMethod, request.uri());
        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(NotFoundException.STATUS, "Not Found [" + request.uri() + "]"));
        } else {
            var page404 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_404, null));
            if (page404.isPresent()) {
                modelAndView.setView(page404.get());
                renderPage(response, modelAndView);
                response.render(page404.get());
            } else {
                HtmlCreator htmlCreator = new HtmlCreator();
                htmlCreator.center("<h1>404 Not Found - " + request.uri() + "</h1>");
                htmlCreator.hr();
                response.html(htmlCreator.html());
            }
        }
    }
    if (e.getStatus() == MethodNotAllowedException.STATUS) {
        log405(log, paddingMethod, request.uri());
        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(MethodNotAllowedException.STATUS, e.getMessage()));
        } else {
            response.text(e.getMessage());
        }
    }
}
Also used : HtmlCreator(com.blade.mvc.ui.HtmlCreator) lombok.var(lombok.var) ModelAndView(com.blade.mvc.ui.ModelAndView)

Example 3 with ModelAndView

use of com.blade.mvc.ui.ModelAndView in project blade by biezhi.

the class DefaultExceptionHandler method render500.

protected void render500(Request request, Response response) {
    var blade = WebContext.blade();
    var page500 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_500, null));
    if (page500.isPresent()) {
        this.renderPage(response, new ModelAndView(page500.get()));
    } else {
        if (blade.devMode()) {
            var htmlCreator = new HtmlCreator();
            htmlCreator.center("<h1>" + request.attribute("title") + "</h1>");
            htmlCreator.startP("message-header");
            htmlCreator.add("Request URI: " + request.uri());
            htmlCreator.startP("message-header");
            htmlCreator.add("Error Message: " + request.attribute("message"));
            htmlCreator.endP();
            if (null != request.attribute(VARIABLE_STACKTRACE)) {
                htmlCreator.startP("message-body");
                htmlCreator.add(request.attribute(VARIABLE_STACKTRACE).toString().replace("\n", "<br/>"));
                htmlCreator.endP();
            }
            response.html(htmlCreator.html());
        } else {
            response.html(INTERNAL_SERVER_ERROR_HTML);
        }
    }
}
Also used : HtmlCreator(com.blade.mvc.ui.HtmlCreator) lombok.var(lombok.var) ModelAndView(com.blade.mvc.ui.ModelAndView)

Example 4 with ModelAndView

use of com.blade.mvc.ui.ModelAndView 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));
        }
    }
}
Also used : Path(com.blade.mvc.annotation.Path) ModelAndView(com.blade.mvc.ui.ModelAndView) RouteHandler0(com.blade.mvc.handler.RouteHandler0) JSON(com.blade.mvc.annotation.JSON) Method(java.lang.reflect.Method) MethodAccess(com.blade.reflectasm.MethodAccess) RouteHandler(com.blade.mvc.handler.RouteHandler)

Aggregations

ModelAndView (com.blade.mvc.ui.ModelAndView)4 HtmlCreator (com.blade.mvc.ui.HtmlCreator)2 lombok.var (lombok.var)2 JSON (com.blade.mvc.annotation.JSON)1 Path (com.blade.mvc.annotation.Path)1 RouteHandler (com.blade.mvc.handler.RouteHandler)1 RouteHandler0 (com.blade.mvc.handler.RouteHandler0)1 Request (com.blade.mvc.http.Request)1 MethodAccess (com.blade.reflectasm.MethodAccess)1 Method (java.lang.reflect.Method)1