@Override public void removeRoute(Route route) { log.debug("Removing route for {} '{}'", route.getRequestMethod(), route.getUriPattern()); routes.remove(route); List<Route> cacheEntry = cache.get(route.getRequestMethod()); if (cacheEntry != null) { cacheEntry.remove(route); } removeBinding(route); }
@Override public void addRoute(Route route) { log.debug("Add route for {} '{}'", route.getRequestMethod(), route.getUriPattern()); validateRoute(route); routes.add(route); List<Route> cacheEntry = cache.get(route.getRequestMethod()); if (cacheEntry == null) { cacheEntry = new ArrayList<>(); } cacheEntry.add(route); cache.put(route.getRequestMethod(), cacheEntry); addBinding(route); }
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"); } }
private void addBinding(Route route) { String uriPattern = route.getUriPattern(); // TODO improve (it's possible to have the same uriPattern for many routes => same pattern) String regex = getRegex(uriPattern); Pattern pattern = Pattern.compile(regex); List<String> parameterNames = getParameterNames(uriPattern); PatternBinding binding = new PatternBinding(pattern, route, parameterNames); String requestMethod = route.getRequestMethod(); if (!bindingsCache.containsKey(requestMethod)) { bindingsCache.put(requestMethod, new ArrayList<PatternBinding>()); } bindingsCache.get(requestMethod).add(binding); }
private void removeBinding(Route route) { String nameOrUriPattern = StringUtils.isNullOrEmpty(route.getName()) ? route.getUriPattern() : route.getName(); PatternBinding binding = getBinding(nameOrUriPattern); bindingsCache.get(route.getRequestMethod()).remove(binding); }