Search in sources :

Example 1 with Route

use of com.blade.mvc.route.Route in project blade by biezhi.

the class AbstractFileRouteLoader method load.

/**
     * Load Route
     *
     * @param inputStream route inputstream
     * @return return route list
     * @throws ParseException parse exception
     * @throws IOException    io exception
     */
private List<Route> load(InputStream inputStream) throws ParseException, IOException {
    int line = 0;
    List<Route> routes = new ArrayList<Route>();
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(inputStream));
        String input;
        while ((input = in.readLine()) != null) {
            line++;
            input = input.trim();
            if (!input.equals("") && !input.startsWith(".")) {
                Route route = parse(input, line);
                routes.add(route);
            }
        }
    } finally {
        IOKit.closeQuietly(in);
    }
    return routes;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) Route(com.blade.mvc.route.Route)

Example 2 with Route

use of com.blade.mvc.route.Route 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)

Example 3 with Route

use of com.blade.mvc.route.Route in project blade by biezhi.

the class AbstractFileRouteLoader method buildRoute.

/**
     * Construct a routing object
     *
     * @param httpMethod        request httpMethod
     * @param path                route path
     * @param controllerName    controller name
     * @param methodName        method name
     * @return return route object
     * @throws RouteException
     */
private Route buildRoute(String httpMethod, String path, String controllerName, String methodName) throws RouteException {
    Object controller = controllerLoader.load(controllerName);
    Class<?> controllerType = controller.getClass();
    Method method = getMethod(controllerType, methodName);
    return new Route(HttpMethod.valueOf(httpMethod.toUpperCase()), path, controller, controllerType, method);
}
Also used : HttpMethod(com.blade.mvc.http.HttpMethod) Method(java.lang.reflect.Method) Route(com.blade.mvc.route.Route)

Aggregations

Route (com.blade.mvc.route.Route)3 HttpMethod (com.blade.mvc.http.HttpMethod)1 ServletRequest (com.blade.mvc.http.wrapper.ServletRequest)1 ServletResponse (com.blade.mvc.http.wrapper.ServletResponse)1 TemplateException (com.blade.mvc.view.template.TemplateException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1