]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8684 Redirect /api to /api/webservices/list
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 23 Jan 2017 15:54:03 +0000 (16:54 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 24 Jan 2017 17:36:49 +0000 (18:36 +0100)
This redirection was previously done by rails

server/sonar-server/src/main/java/org/sonar/server/platform/web/RoutesFilter.java
server/sonar-server/src/test/java/org/sonar/server/platform/web/RoutesFilterTest.java

index dff0f867a28a95c7567f0bc2c7d89b6a39fb6589..23a38770c02c80c26388d0ad294cee4d0323359f 100644 (file)
@@ -42,6 +42,7 @@ public class RoutesFilter implements Filter {
   private static final String EMPTY = "";
 
   private static final List<Route> 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;
   }
 }
index 46b374497e9e4b11368150628e9197632dba3b9d..1e8930a0feea49741c8ad387e891b2d2ce06604c 100644 (file)
@@ -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