Search in sources :

Example 1 with ServletResponse

use of com.blade.mvc.http.wrapper.ServletResponse in project blade by biezhi.

the class DispatcherHandler method handle.

public void handle(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    // Create Response
    Response response = new ServletResponse(httpResponse);
    response.contentType("text/html;charset=utf-8");
    try {
        // http method, GET/POST ...
        String method = httpRequest.getMethod();
        // reuqest uri
        String uri = Path.getRelativePath(httpRequest.getRequestURI(), httpRequest.getContextPath());
        LOGGER.info("{}\t{}\t{}", method, uri, httpRequest.getProtocol());
        Request request = new ServletRequest(httpRequest);
        WebContextHolder.init(request, response);
        Route route = routeMatcher.getRoute(method, uri);
        if (null != route) {
            if (route.getHttpMethod() != HttpMethod.valueOf(method) && route.getHttpMethod() != HttpMethod.ALL) {
                render405(response, uri);
            } else {
                request.setRoute(route);
                // before inteceptor
                List<Route> befores = routeMatcher.getBefore(uri);
                boolean result = invokeInterceptor(request, response, befores);
                if (result) {
                    // execute
                    this.routeHandle(request, response, route);
                    if (!request.isAbort()) {
                        // after inteceptor
                        List<Route> afters = routeMatcher.getAfter(uri);
                        invokeInterceptor(request, response, afters);
                    }
                }
            }
        } else {
            // Not found
            render404(response, uri);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        DispatchKit.printError(e, 500, response);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(com.blade.mvc.http.wrapper.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(com.blade.mvc.http.wrapper.ServletResponse) ServletRequest(com.blade.mvc.http.wrapper.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(com.blade.mvc.http.wrapper.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Route(com.blade.mvc.route.Route) IOException(java.io.IOException) TemplateException(com.blade.mvc.view.template.TemplateException)

Aggregations

ServletRequest (com.blade.mvc.http.wrapper.ServletRequest)1 ServletResponse (com.blade.mvc.http.wrapper.ServletResponse)1 Route (com.blade.mvc.route.Route)1 TemplateException (com.blade.mvc.view.template.TemplateException)1 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1