]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8291 support internal params in WS api/webservices/list
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 17:30:53 +0000 (19:30 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 17 Oct 2016 09:50:06 +0000 (11:50 +0200)
server/sonar-server/src/main/java/org/sonar/server/ws/WebServicesWs.java
server/sonar-server/src/main/resources/org/sonar/server/ws/list-example.json
server/sonar-server/src/test/java/org/sonar/server/ws/WebServicesWsTest.java
server/sonar-server/src/test/resources/org/sonar/server/ws/WebServicesWsTest/list_including_internals.json

index 7d08b5f332423e46fa7b5a641d56955741704cee..486f31ecb7b775cb4b5dd5079ad2e0c389b0cc28 100644 (file)
  */
 package org.sonar.server.ws;
 
-import com.google.common.base.Function;
 import com.google.common.collect.Ordering;
 import java.io.IOException;
 import java.util.List;
 import java.util.Set;
-import javax.annotation.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.util.stream.Collectors;
 
 /**
  * This web service lists all the existing web services, including itself,
@@ -56,12 +55,7 @@ public class WebServicesWs implements WebService {
       .setSince("4.2")
       .setDescription("List web services")
       .setResponseExample(getClass().getResource("list-example.json"))
-      .setHandler(new RequestHandler() {
-        @Override
-        public void handle(Request request, Response response) {
-          handleList(context.controllers(), request, response);
-        }
-      });
+      .setHandler((request, response) -> handleList(context.controllers(), request, response));
     action
       .createParam("include_internals")
       .setDescription("Include web services that are implemented for internal use only. Their forward-compatibility is " +
@@ -123,12 +117,7 @@ public class WebServicesWs implements WebService {
     writer.name("webServices").beginArray();
 
     // sort controllers by path
-    Ordering<Controller> ordering = Ordering.natural().onResultOf(new Function<Controller, String>() {
-      @Override
-      public String apply(Controller controller) {
-        return controller.path();
-      }
-    });
+    Ordering<Controller> ordering = Ordering.natural().onResultOf(Controller::path);
     for (Controller controller : ordering.sortedCopy(controllers)) {
       writeController(writer, controller, includeInternals);
     }
@@ -144,12 +133,7 @@ public class WebServicesWs implements WebService {
       writer.prop("since", controller.since());
       writer.prop("description", controller.description());
       // sort actions by key
-      Ordering<Action> ordering = Ordering.natural().onResultOf(new Function<Action, String>() {
-        @Override
-        public String apply(Action action) {
-          return action.key();
-        }
-      });
+      Ordering<Action> ordering = Ordering.natural().onResultOf(Action::key);
       writer.name("actions").beginArray();
       for (Action action : ordering.sortedCopy(controller.actions())) {
         writeAction(writer, action, includeInternals);
@@ -169,16 +153,12 @@ public class WebServicesWs implements WebService {
       writer.prop("internal", action.isInternal());
       writer.prop("post", action.isPost());
       writer.prop("hasResponseExample", action.responseExample() != null);
-      if (!action.params().isEmpty()) {
+      List<Param> params = action.params().stream().filter(p -> includeInternals || !p.isInternal()).collect(Collectors.toList());
+      if (!params.isEmpty()) {
         // sort parameters by key
-        Ordering<Param> ordering = Ordering.natural().onResultOf(new Function<Param, String>() {
-          @Override
-          public String apply(@Nullable Param param) {
-            return param != null ? param.key() : null;
-          }
-        });
+        Ordering<Param> ordering = Ordering.natural().onResultOf(Param::key);
         writer.name("params").beginArray();
-        for (Param param : ordering.sortedCopy(action.params())) {
+        for (Param param : ordering.sortedCopy(params)) {
           writeParam(writer, param);
         }
         writer.endArray();
@@ -193,6 +173,7 @@ public class WebServicesWs implements WebService {
     writer.prop("description", param.description());
     writer.prop("since", param.since());
     writer.prop("required", param.isRequired());
+    writer.prop("internal", param.isInternal());
     writer.prop("defaultValue", param.defaultValue());
     writer.prop("exampleValue", param.exampleValue());
     writer.prop("deprecatedSince", param.deprecatedSince());
index 32b2dc97b87553773d624b5022e3ac807d8cfb3c..d5c306ba39fb06f756a4a45839a5dc060bffc7c4 100644 (file)
           "params": [
             {
               "key": "name",
-              "required": false
+              "required": false,
+              "internal": false
             },
             {
               "key": "severity",
               "description": "Severity",
               "required": false,
+              "internal": false,
               "defaultValue": "BLOCKER",
               "exampleValue": "INFO",
               "since": "4.4",
@@ -59,6 +61,7 @@
               "key": "include_internals",
               "description": "Include web services that are implemented for internal use only. Their forward-compatibility is not assured",
               "required": false,
+              "internal": false,
               "defaultValue": "false",
               "possibleValues": [
                 "true",
index 554a1c44c60046185bfbc13d4fa1aae516c70ffb..30057f4162c2a788854519905dd1f7dff2934188 100644 (file)
@@ -21,9 +21,6 @@ package org.sonar.server.ws;
 
 import com.google.common.io.Resources;
 import org.junit.Test;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.test.JsonAssert;
 
@@ -31,7 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 public class WebServicesWsTest {
 
-  WebServicesWs ws = new WebServicesWs();
+  private WebServicesWs ws = new WebServicesWs();
 
   @Test
   public void define_ws() {
@@ -95,11 +92,7 @@ public class WebServicesWsTest {
         .setSince("3.2")
         .setDescription("Show Description")
         .setResponseExample(getClass().getResource("web-services-ws-test.txt"))
-        .setHandler(new RequestHandler() {
-          @Override
-          public void handle(Request request, Response response) {
-          }
-        });
+        .setHandler((request, response) -> {});
 
       // action with a lot of overridden values
       NewAction create = newController.createAction("create")
@@ -108,11 +101,7 @@ public class WebServicesWsTest {
         .setDeprecatedSince("5.3")
         .setPost(true)
         .setResponseExample(Resources.getResource(getClass(), "WebServicesWsTest/metrics_example.json"))
-        .setHandler(new RequestHandler() {
-          @Override
-          public void handle(Request request, Response response) {
-          }
-        });
+        .setHandler((request, response) -> {});
       create
         .createParam("severity")
         .setDescription("Severity")
@@ -123,18 +112,14 @@ public class WebServicesWsTest {
         .setExampleValue("INFO")
         .setDefaultValue("BLOCKER");
       create.createParam("name");
+      create.createParam("internal").setInternal(true);
 
       newController.createAction("internal_action")
         .setDescription("Internal Action Description")
         .setResponseExample(getClass().getResource("web-services-ws-test.txt"))
         .setSince("5.3")
         .setInternal(true)
-        .setHandler(new RequestHandler() {
-          @Override
-          public void handle(Request request, Response response) throws Exception {
-
-          }
-        });
+        .setHandler((request, response) -> {});
 
       newController.done();
     }
index 527ccf3dd42792f090a781e694808880c641e840..e4e70d2d40f8dd19163484b3bfe1e2bd147ac419 100644 (file)
         "params": [
           {
             "key": "name",
-            "required": false
+            "required": false,
+            "internal": false
           },
           {
             "key": "severity",
             "description": "Severity",
             "required": false,
+            "internal": false,
             "defaultValue": "BLOCKER",
             "exampleValue": "INFO",
             "possibleValues": ["BLOCKER", "INFO"]
+          },
+          {
+            "key": "internal",
+            "required": false,
+            "internal": true
           }
         ]
       },
@@ -59,6 +66,7 @@
             "key": "include_internals",
             "description": "Include web services that are implemented for internal use only. Their forward-compatibility is not assured",
             "required": false,
+            "internal": false,
             "defaultValue": "false",
             "possibleValues": ["true", "false", "yes", "no"]
           }
           {
             "key": "action",
             "required": true,
+            "internal": false,
             "description": "Action of the web service",
             "exampleValue": "search"
           },
           {
             "key": "controller",
             "required": true,
+            "internal": false,
             "description": "Controller of the web service",
             "exampleValue": "api/issues"
           }