From 604dd82bc19dd57340556f6a4a761a0f77ccbbfe Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 23 Jan 2017 16:54:03 +0100 Subject: [PATCH] SONAR-8684 Redirect /api to /api/webservices/list This redirection was previously done by rails --- .../server/platform/web/RoutesFilter.java | 23 ++++++++++++++++++- .../server/platform/web/RoutesFilterTest.java | 9 ++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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 dff0f867a28..23a38770c02 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 @@ -42,6 +42,7 @@ public class RoutesFilter implements Filter { private static final String EMPTY = ""; private static final List ROUTES = ImmutableList.of( + new WebServiceListRoute(), new BatchRoute(), new BatchBootstrapRoute(), new ApiSourcesRoute(), @@ -81,6 +82,19 @@ public class RoutesFilter implements Filter { String apply(HttpServletRequest request); } + private static class WebServiceListRoute implements Route { + + @Override + public boolean test(String path) { + return "/api".equals(path); + } + + @Override + public String apply(HttpServletRequest request) { + return format("%s/api/webservices/list", request.getContextPath()); + } + } + /** * Old scanners were using /batch/file.jar url (see SCANNERAPI-167) */ @@ -154,6 +168,13 @@ public class RoutesFilter implements Filter { } private static String extractPath(HttpServletRequest request) { - return request.getRequestURI().replaceFirst(request.getContextPath(), EMPTY); + return sanitizePath(request.getRequestURI().replaceFirst(request.getContextPath(), EMPTY)); + } + + private static String sanitizePath(String path) { + if (path.length() > 1 && path.endsWith("/")) { + return path.substring(0, path.length() - 1); + } + return path; } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/RoutesFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/RoutesFilterTest.java index 46b374497e9..1e8930a0fee 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/web/RoutesFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/RoutesFilterTest.java @@ -49,6 +49,12 @@ public class RoutesFilterTest { when(request.getContextPath()).thenReturn("/sonarqube"); } + @Test + public void send_redirect_when_url_contains_only_api() throws Exception { + verifyRedirection("/api", null, "/sonarqube/api/webservices/list"); + verifyRedirection("/api/", null, "/sonarqube/api/webservices/list"); + } + @Test public void send_redirect_when_url_contains_batch_with_jar() throws Exception { verifyRedirection("/batch/file.jar", null, "/sonarqube/batch/file?name=file.jar"); @@ -57,16 +63,19 @@ public class RoutesFilterTest { @Test public void send_redirect_when_url_contains_batch_bootstrap() throws Exception { verifyRedirection("/batch_bootstrap/index", null, "/sonarqube/batch/index"); + verifyRedirection("/batch_bootstrap/index/", null, "/sonarqube/batch/index"); } @Test public void send_redirect_when_url_contains_api_sources() throws Exception { verifyRedirection("/api/sources", "resource=my.project", "/sonarqube/api/sources/index?resource=my.project"); + verifyRedirection("/api/sources/", "resource=my.project", "/sonarqube/api/sources/index?resource=my.project"); } @Test public void send_redirect_when_url_contains_profiles_export() throws Exception { verifyRedirection("/profiles/export", "format=pmd", "/sonarqube/api/qualityprofiles/export?format=pmd"); + verifyRedirection("/profiles/export/", "format=pmd", "/sonarqube/api/qualityprofiles/export?format=pmd"); } @Test -- 2.39.5