@SuppressWarnings("unchecked") protected void handleRoute(Route route) { if (StringUtils.isNullOrEmpty(route.getName())) { log.debug("Executing handler for {} '{}'", route.getRequestMethod(), route.getUriPattern()); } else { log.debug("Executing '{}' for {} '{}'", route.getName(), route.getRequestMethod(), route.getUriPattern()); } this.route = route; route.getRouteHandler().handle(this); }
/** * Create a {@code HEAD} route. * * @param uriPattern * @param routeHandler * @return */ public static Route HEAD(String uriPattern, RouteHandler routeHandler) { return new Route(HttpConstants.Method.HEAD, uriPattern, routeHandler); }
@Override public Route transform(Route route) { Method method = route.getAttribute("__controllerMethod"); if (method == null) { // it's not a controller route; do nothing return route; } if (method.isAnnotationPresent(Named.class)) { Named named = method.getAnnotation(Named.class); String name = named.value(); log.debug("Set name '{}' for route {} '{}'", name, route.getRequestMethod(), route.getUriPattern()); route.setName(name); } return route; }
@Override public String toString() { return "RouteMatch{" + "requestMethod='" + route.getRequestMethod() + '\'' + ", uriPattern='" + route.getUriPattern() + '\'' + '}'; }
private Route getCompiledRoute(String nameOrUriPattern) { for (Route route : compiledRoutes) { if (nameOrUriPattern.equals(route.getName()) || nameOrUriPattern.equals(route.getUriPattern())) { return route; } } return null; }
protected void processRouteInterceptors(RouteContext routeContext) { if (interceptors.isEmpty()) { return; } List<RouteMatch> chain = new ArrayList<>(); for (RouteHandler interceptor : interceptors) { // create a route for interceptor Route route = new Route(routeContext.getRequestMethod(), routeContext.getRequestUri(), interceptor); route.setName(StringUtils.format("{}<{}>", Interceptor.class.getSimpleName(), route.getRouteHandler().getClass().getSimpleName())); route.bindAll(routeContext.getRoute().getAttributes()); // add route in chain RouteMatch match = new RouteMatch(route, null); chain.add(match); } // TODO DefaultRouteContext is hardcoded RouteContext context = new DefaultRouteContext(routeContext.getApplication(), routeContext.getRequest(), routeContext.getResponse(), chain); context.next(); }
private void removeCompiledRoute(Route route) { String nameOrUriPattern = StringUtils.isNullOrEmpty(route.getName()) ? route.getUriPattern() : route.getName(); Route compiledRoute = getCompiledRoute(nameOrUriPattern); if (compiledRoute == null) { // do nothing; probably the route was not yet compiled return; } compiledRoutes.remove(compiledRoute); // update cache List<Route> cacheEntry = compiledRoutesCache.get(route.getRequestMethod()); if (cacheEntry != null) { cacheEntry.remove(compiledRoute); } uriMatcher.removeUriPattern(compiledRoute.getUriPattern()); }
nameStack.push(route.getName()); nameStack.pushIfNotEmpty(routeGroup.getName()); attributesStack.push(route.getAttributes()); attributesStack.push(routeGroup.getAttributes()); route.setAbsoluteUriPattern(concatUriPattern(uriPattern, route.getUriPattern())); route.setName(StreamSupport.stream(nameStack.spliterator(), false) .collect(Collectors.joining(""))); route.bindAll(StreamSupport.stream(attributesStack.spliterator(), false) .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
protected void processRouteInterceptors(Context context) { if (routeInterceptors.isEmpty()) { return; } List<RouteMatch> chain = new ArrayList<>(); for (RouteHandler<Context> interceptor : routeInterceptors) { Route route = new Route(context.getRequestMethod(), context.getRequestUri(), interceptor); route.setName(StringUtils.format("{}<{}>", RouteInterceptor.class.getSimpleName(), route.getRouteHandler().getClass().getSimpleName())); RouteMatch match = new RouteMatch(route, null); chain.add(match); } Context subContext = new Context(context, chain); subContext.next(); }
/** * Execute all routes that are flagged to run as finally. */ @Override public void runFinallyRoutes() { while (iterator.hasNext()) { Route route = iterator.next().getRoute(); if (route.isRunAsFinally()) { try { handleRoute(route); } catch (Exception e) { log.error("Unexpected error in Finally Route", e); } } else if (log.isDebugEnabled()) { if (StringUtils.isNullOrEmpty(route.getName())) { log.debug("context.next() not called, skipping handler for {} '{}'", route.getRequestMethod(), route.getUriPattern()); } else { log.debug("context.next() not called, skipping '{}' for {} '{}'", route.getName(), route.getRequestMethod(), route.getUriPattern()); } } } }
private Route compileRoute(Route route) { String uriPattern = route.getUriPattern(); UriMatcher.UriPatternBinding binding = uriMatcher.addUriPattern(uriPattern); List<String> parameterNames = binding.getParameterNames(); Route compiledRoute = new Route(route); // add additional metadata as attributes compiledRoute.bind("__pattern", binding.getPattern()); compiledRoute.bind("__parameterNames", parameterNames); return compiledRoute; }
@Override public Route transform(Route route) { Method method = route.getAttribute("__controllerMethod"); if (method == null) { try { method = route.getRouteHandler().getClass().getMethod("handle", RouteContext.class); } catch (NoSuchMethodException e) { throw new PippoRuntimeException(e); Metered metered = method.getAnnotation(Metered.class); String metricName = !metered.value().isEmpty() ? metered.value() : getMetricName(route, method); handler = new MeteredHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Timed.class)) { log.debug("Found '{}' annotation on method '{}'", Timed.class.getSimpleName(), LangUtils.toString(method)); Timed timed = method.getAnnotation(Timed.class); String metricName = !timed.value().isEmpty() ? timed.value() : getMetricName(route, method); handler = new TimedHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Counted.class)) { log.debug("Found '{}' annotation on method '{}'", Counted.class.getSimpleName(), LangUtils.toString(method)); Counted counted = method.getAnnotation(Counted.class); String metricName = !counted.value().isEmpty() ? counted.value() : getMetricName(route, method); handler = new CountedHandler(metricName, counted.active(), metricRegistry, route.getRouteHandler()); route.setRouteHandler(handler);
Route route = new Route(routeRegistration.getRequestMethod(), routeRegistration.getUriPattern(), routeHandler); route.setName(routeRegistration.getName()); if (routeRegistration.isRunAsFinally()) { route.runAsFinally();
@Override protected String getPathParameterValue(String uriPattern, String parameterName, Object parameterValue) { String value = super.getPathParameterValue(uriPattern, parameterName, parameterValue); // retrieve the compiled route for uriPattern Route compiledRoute = compiledRoutes.stream() .filter(route -> uriPattern.equals(route.getUriPattern())) .findAny() .orElseThrow(() -> new PippoRuntimeException("Cannot find a compiled route for '{}'", uriPattern)); RouteHandler handler = compiledRoute.getRouteHandler(); boolean isResourceRoute = ResourceHandler.class.isAssignableFrom(handler.getClass()); // inject version for resource routes if (isResourceRoute && ResourceHandler.PATH_PARAMETER.equals(parameterName)) { ResourceHandler resourceHandler = (ResourceHandler) compiledRoute.getRouteHandler(); if (resourceHandler.isVersioned()) { value = resourceHandler.injectVersion(value); } } return value; }
/** * Register the controller methods as routes. * * @param controllerMethods * @param controller */ private void registerControllerMethods(Map<Method, Class<? extends Annotation>> controllerMethods, Controller controller) { List<Route> controllerRoutes = createControllerRoutes(controllerMethods); for (Route controllerRoute : controllerRoutes) { if (controller != null) { ((ControllerHandler) controllerRoute.getRouteHandler()).setController(controller); controllerRoute.bind("__controller", controller); } } this.routes.addAll(controllerRoutes); }
@Override public String toString() { return "Route{" + "requestMethod='" + requestMethod + '\'' + ", uriPattern='" + getUriPattern() + '\'' + '}'; }
private Route getRoute(Class<? extends ResourceHandler> resourceHandlerClass) { List<Route> routes = getRoutes(); for (Route route : routes) { RouteHandler routeHandler = route.getRouteHandler(); if (resourceHandlerClass.isAssignableFrom(routeHandler.getClass())) { ClasspathResourceHandler resourceHandler = (ClasspathResourceHandler) routeHandler; if (resourceHandlerClass == resourceHandler.getClass()) { return route; } } } return null; }
List<Route> cacheEntry = routesCache.get(route.getRequestMethod()); if (cacheEntry != null) { cacheEntry.remove(route); break; compiledRoute.bind("__transformer", transformer);
private String getMetricName(Route route, Method method) { String metricName = route.getName(); if (StringUtils.isNullOrEmpty(metricName)) { metricName = MetricRegistry.name(method.getDeclaringClass(), method.getName()); } return metricName; }