Search in sources :

Example 1 with HttpMethod

use of com.blade.mvc.http.HttpMethod in project blade by biezhi.

the class RouteMatcher method lookupRoute.

public Route lookupRoute(String httpMethod, String path) throws BladeException {
    path = parsePath(path);
    String routeKey = path + '#' + httpMethod.toUpperCase();
    Route route = staticRoutes.get(routeKey);
    if (null != route) {
        return route;
    }
    route = staticRoutes.get(path + "#ALL");
    if (null != route) {
        return route;
    }
    Map<String, String> uriVariables = CollectionKit.newLinkedHashMap();
    HttpMethod requestMethod = HttpMethod.valueOf(httpMethod);
    try {
        Matcher matcher = regexRoutePatterns.get(requestMethod).matcher(path);
        boolean matched = matcher.matches();
        if (!matched) {
            matcher = regexRoutePatterns.get(HttpMethod.ALL).matcher(path);
            matched = matcher.matches();
        }
        if (matched) {
            int i;
            for (i = 1; matcher.group(i) == null; i++) ;
            FastRouteMappingInfo mappingInfo = regexRoutes.get(requestMethod).get(i);
            route = mappingInfo.getRoute();
            // find path variable
            String uriVariable;
            int j = 0;
            while (++i <= matcher.groupCount() && (uriVariable = matcher.group(i)) != null) {
                uriVariables.put(mappingInfo.getVariableNames().get(j++), uriVariable);
            }
            route.setPathParams(uriVariables);
            LOGGER.trace("lookup path: " + path + " uri variables: " + uriVariables);
        }
        return route;
    } catch (Exception e) {
        throw new BladeException(e);
    }
}
Also used : BladeException(com.blade.exception.BladeException) Matcher(java.util.regex.Matcher) HttpMethod(com.blade.mvc.http.HttpMethod) URISyntaxException(java.net.URISyntaxException) BladeException(com.blade.exception.BladeException)

Example 2 with HttpMethod

use of com.blade.mvc.http.HttpMethod in project blade by biezhi.

the class Routers method route.

public void route(String path, Class<?> clazz, String methodName) {
    Assert.notNull(methodName, "Method name not is null");
    HttpMethod httpMethod = HttpMethod.ALL;
    if (methodName.contains(":")) {
        String[] methodArr = methodName.split(":");
        httpMethod = HttpMethod.valueOf(methodArr[0].toUpperCase());
        methodName = methodArr[1];
    }
    this.route(path, clazz, methodName, httpMethod);
}
Also used : HttpMethod(com.blade.mvc.http.HttpMethod)

Example 3 with HttpMethod

use of com.blade.mvc.http.HttpMethod in project blade by biezhi.

the class Routers method addRoute.

public void addRoute(Route route) {
    String path = route.getPath();
    HttpMethod httpMethod = route.getHttpMethod();
    String key = path + "#" + httpMethod.toString();
    // existent
    if (null != this.routes.get(key)) {
        LOGGER.warn("\tRoute {} -> {} has exist", path, httpMethod.toString());
    }
    if (httpMethod == HttpMethod.BEFORE || httpMethod == HttpMethod.AFTER) {
        if (null != this.interceptors.get(key)) {
            LOGGER.warn("\tInterceptor {} -> {} has exist", path, httpMethod.toString());
        }
        this.interceptors.put(key, route);
        LOGGER.debug("Add Interceptor => {}", route);
    } else {
        this.routes.put(key, route);
        LOGGER.debug("Add Route => {}", route);
    }
}
Also used : HttpMethod(com.blade.mvc.http.HttpMethod)

Example 4 with HttpMethod

use of com.blade.mvc.http.HttpMethod in project blade by biezhi.

the class RouteBuilder method addRouter.

/**
     * Parse all routing in a controller
     *
     * @param router resolve the routing class
     */
public void addRouter(final Class<?> router) {
    Method[] methods = router.getMethods();
    if (null == methods || methods.length == 0) {
        return;
    }
    String nameSpace = null, suffix = null;
    if (null != router.getAnnotation(Controller.class)) {
        nameSpace = router.getAnnotation(Controller.class).value();
        suffix = router.getAnnotation(Controller.class).suffix();
    }
    if (null != router.getAnnotation(RestController.class)) {
        nameSpace = router.getAnnotation(RestController.class).value();
        suffix = router.getAnnotation(RestController.class).suffix();
    }
    if (null == nameSpace) {
        LOGGER.warn("Route [{}] not controller annotation", router.getName());
        return;
    }
    for (Method method : methods) {
        Route mapping = method.getAnnotation(Route.class);
        //route method
        if (null != mapping) {
            // build multiple route
            HttpMethod methodType = mapping.method();
            String[] paths = mapping.values();
            if (mapping.value().length > 1 || !mapping.value()[0].equals("/")) {
                paths = mapping.value();
            }
            if (paths.length > 0) {
                for (String path : paths) {
                    String pathV = getRoutePath(path, nameSpace, suffix);
                    this.buildRoute(router, method, pathV, methodType);
                }
            }
        }
    }
}
Also used : RestController(com.blade.mvc.annotation.RestController) HttpMethod(com.blade.mvc.http.HttpMethod) Method(java.lang.reflect.Method) RestController(com.blade.mvc.annotation.RestController) Controller(com.blade.mvc.annotation.Controller) Route(com.blade.mvc.annotation.Route) HttpMethod(com.blade.mvc.http.HttpMethod)

Example 5 with HttpMethod

use of com.blade.mvc.http.HttpMethod in project blade by biezhi.

the class RouteMatcher method register.

// a bad way
void register() {
    List<Route> routeHandlers = new ArrayList<>(routes.values());
    routeHandlers.addAll(interceptors.values());
    for (Route route : routeHandlers) {
        String path = parsePath(route.getPath());
        Matcher matcher = PATH_VARIABLE_PATTERN.matcher(path);
        boolean find = false;
        List<String> uriVariableNames = new ArrayList<>();
        while (matcher.find()) {
            if (!find) {
                find = true;
            }
            String group = matcher.group(0);
            // {id} -> id
            uriVariableNames.add(group.substring(1));
        }
        HttpMethod httpMethod = route.getHttpMethod();
        if (find || (httpMethod == HttpMethod.AFTER || httpMethod == HttpMethod.BEFORE)) {
            if (regexRoutes.get(httpMethod) == null) {
                regexRoutes.put(httpMethod, new HashMap<>());
                patternBuilders.put(httpMethod, new StringBuilder("^"));
                indexes.put(httpMethod, 1);
            }
            int i = indexes.get(httpMethod);
            regexRoutes.get(httpMethod).put(i, new FastRouteMappingInfo(route, uriVariableNames));
            indexes.put(httpMethod, i + uriVariableNames.size() + 1);
            patternBuilders.get(httpMethod).append("(").append(matcher.replaceAll(PATH_VARIABLE_REPLACE)).append(")|");
        } else {
            String routeKey = path + '#' + httpMethod.toString();
            if (staticRoutes.get(routeKey) == null) {
                staticRoutes.put(routeKey, route);
            }
        }
    }
    for (Map.Entry<HttpMethod, StringBuilder> entry : patternBuilders.entrySet()) {
        HttpMethod httpMethod = entry.getKey();
        StringBuilder patternBuilder = entry.getValue();
        if (patternBuilder.length() > 1) {
            patternBuilder.setCharAt(patternBuilder.length() - 1, '$');
        }
        LOGGER.debug("Fast Route Method: {}, regex: {}", httpMethod, patternBuilder);
        regexRoutePatterns.put(httpMethod, Pattern.compile(patternBuilder.toString()));
    }
}
Also used : Matcher(java.util.regex.Matcher) HttpMethod(com.blade.mvc.http.HttpMethod)

Aggregations

HttpMethod (com.blade.mvc.http.HttpMethod)8 Method (java.lang.reflect.Method)4 BladeException (com.blade.exception.BladeException)2 Matcher (java.util.regex.Matcher)2 Controller (com.blade.mvc.annotation.Controller)1 RestController (com.blade.mvc.annotation.RestController)1 Route (com.blade.mvc.annotation.Route)1 Route (com.blade.mvc.route.Route)1 URISyntaxException (java.net.URISyntaxException)1