private void addCompiledRoute(Route compiledRoute) { compiledRoutes.add(compiledRoute); // update cache String requestMethod = compiledRoute.getRequestMethod(); if (!compiledRoutesCache.containsKey(requestMethod)) { compiledRoutesCache.put(requestMethod, new ArrayList<>()); } compiledRoutesCache.get(requestMethod).add(compiledRoute); // it's added on compileRoute method // uriMatcher.addUriPattern(compiledRoute.getUriPattern()); }
@Override public void addRoute(Route route) { log.debug("Add route for {} '{}'", route.getRequestMethod(), route.getUriPattern()); validateRoute(route); routes.add(route); // update cache List<Route> cacheEntry = routesCache.get(route.getRequestMethod()); if (cacheEntry == null) { cacheEntry = new ArrayList<>(); } cacheEntry.add(route); routesCache.put(route.getRequestMethod(), cacheEntry); }
@Override public String toString() { return "RouteMatch{" + "requestMethod='" + route.getRequestMethod() + '\'' + ", uriPattern='" + route.getUriPattern() + '\'' + '}'; }
@Override public void removeRoute(Route route) { log.debug("Removing route for {} '{}'", route.getRequestMethod(), route.getUriPattern()); boolean removed = routes.remove(route); if (removed) { // updates cache List<Route> cacheEntry = routesCache.get(route.getRequestMethod()); if (cacheEntry != null) { cacheEntry.remove(route); } removeCompiledRoute(route); } }
@Override public List<RouteMatch> findRoutes(String requestMethod, String requestUri) { log.trace("Finding route matches for {} '{}'", requestMethod, requestUri); // force compile routes compileRoutes(); List<RouteMatch> routeMatches = new ArrayList<>(); for (Route route : compiledRoutes) { boolean methodMatches = route.getRequestMethod().equals(requestMethod) || route.getRequestMethod().equals(HttpConstants.Method.ANY); if (methodMatches) { String uriPattern = route.getUriPattern(); Map<String, String> parameters = uriMatcher.match(requestUri, uriPattern); if (parameters != null) { // complete matching => add route match ro returned list routeMatches.add(new RouteMatch(route, parameters)); } } } log.debug("Found {} route matches for {} '{}'", routeMatches.size(), requestMethod, requestUri); return routeMatches; }
@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); }
@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; }
List<Route> cacheEntry = routesCache.get(route.getRequestMethod()); if (cacheEntry != null) { cacheEntry.remove(route);
/** * 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()); } } } }
content.append('\t').append(route.getRequestMethod()).append(' ').append(route.getUriPattern()); content.append('\n');
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()); }
protected void validateRoute(Route route) { // validate the request method if (StringUtils.isNullOrEmpty(route.getRequestMethod())) { throw new PippoRuntimeException("Unspecified request method!"); } // validate the uri pattern String uriPattern = route.getUriPattern(); if (StringUtils.isNullOrEmpty(uriPattern)) { throw new PippoRuntimeException("The uri pattern cannot be null or empty"); } }
@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; }
if (!METHODS.contains(route.getRequestMethod().toUpperCase())) { log.debug("Skip {} {}, {} Swagger does not support specified HTTP method", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; if (produces.isEmpty()) { log.debug("Skip {} {}, {} does not declare @Produces", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; || handler.getControllerMethod().getDeclaringClass().isAnnotationPresent(Undocumented.class)) { log.debug("Skip {} {}, {} is annotated as @Undocumented", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod()), relativeSwaggerBasePath); return false;
if (!METHODS.contains(route.getRequestMethod().toUpperCase())) { log.debug("Skip {} {}, {} Swagger does not support specified HTTP method", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; if (produces.isEmpty()) { log.debug("Skip {} {}, {} does not declare @Produces", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; || handler.getControllerMethod().getDeclaringClass().isAnnotationPresent(Undocumented.class)) { log.debug("Skip {} {}, {} is annotated as @Undocumented", route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod())); return false; route.getRequestMethod(), route.getUriPattern(), Util.toString(handler.getControllerMethod()), relativeSwaggerBasePath); return false;
path.set(route.getRequestMethod().toLowerCase(), operation); log.debug("Add {} {} => {}", route.getRequestMethod(), operationPath, Util.toString(method));
maxMethodLen = Math.max(maxMethodLen, route.getRequestMethod().length()); maxPathLen = Math.max(maxPathLen, route.getUriPattern().length()); if (logHandlers) { log.info("{} {} => {}()", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '), Util.toString(handler.getControllerMethod())); } else { log.info("{} {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); if (logHandlers) { if (logHandlers) { log.info("{} {} => {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '), route.getName()); } else { log.info("{} {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '));
path.set(route.getRequestMethod().toLowerCase(), operation); log.debug("Add {} {} => {}", route.getRequestMethod(), operationPath, Util.toString(method));
maxMethodLen = Math.max(maxMethodLen, route.getRequestMethod().length()); maxPathLen = Math.max(maxPathLen, route.getUriPattern().length()); if (logHandlers) { log.info("{} {} => {}()", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '), Util.toString(handler.getControllerMethod())); } else { log.info("{} {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); if (logHandlers) { if (logHandlers) { log.info("{} {} => {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '), route.getName()); } else { log.info("{} {}", Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' ')); Strings.padEnd(route.getRequestMethod(), maxMethodLen, ' '), Strings.padEnd(route.getUriPattern(), maxPathLen, ' '));