From 02c8865621660708a6301b15770b729681dba94d Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 17 Feb 2017 17:24:44 +0100 Subject: Improve readability of RoutesFilter --- .../org/sonar/server/platform/web/RoutesFilter.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'server') diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/RoutesFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/RoutesFilter.java index 6f4808f2957..3c96011d1a8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/web/RoutesFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/RoutesFilter.java @@ -34,7 +34,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.sonar.core.util.stream.Collectors; -import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; public class RoutesFilter implements Filter { @@ -52,15 +51,21 @@ public class RoutesFilter implements Filter { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String path = extractPath(request); + Predicate match = route -> route.test(path); List routes = ROUTES.stream() - .filter(route -> route.test(path)) + .filter(match) .collect(Collectors.toList()); - checkState(routes.isEmpty() || routes.size() == 1, "Multiple routes have been found for '%s'", path); - if (routes.isEmpty()) { - chain.doFilter(request, response); - return; + + switch (routes.size()) { + case 0: + chain.doFilter(request, response); + break; + case 1: + response.sendRedirect(routes.get(0).apply(request)); + break; + default: + throw new IllegalStateException(format("Multiple routes have been found for '%s'", path)); } - response.sendRedirect(routes.get(0).apply(request)); } @Override -- cgit v1.2.3