diff options
Diffstat (limited to 'tests/plugins/ws-plugin/src')
-rw-r--r-- | tests/plugins/ws-plugin/src/main/java/LocalCallWebService.java | 84 | ||||
-rw-r--r-- | tests/plugins/ws-plugin/src/main/java/WsPlugin.java | 29 |
2 files changed, 113 insertions, 0 deletions
diff --git a/tests/plugins/ws-plugin/src/main/java/LocalCallWebService.java b/tests/plugins/ws-plugin/src/main/java/LocalCallWebService.java new file mode 100644 index 00000000000..7de752bba25 --- /dev/null +++ b/tests/plugins/ws-plugin/src/main/java/LocalCallWebService.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +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.sonarqube.ws.MediaTypes; +import org.sonarqube.ws.WsCe; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.LocalWsClientFactory; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsResponse; + +public final class LocalCallWebService implements WebService { + + private final LocalWsClientFactory wsClientFactory; + + public LocalCallWebService(LocalWsClientFactory wsClientFactory) { + this.wsClientFactory = wsClientFactory; + } + + @Override + public void define(Context context) { + NewController controller = context.createController("local_ws_call"); + controller.createAction("protobuf_data").setHandler(new ProtobufHandler()); + controller.createAction("json_data").setHandler(new JsonHandler()); + controller.createAction("require_permission").setHandler(new RequirePermissionHandler()); + controller.done(); + } + + private class ProtobufHandler implements RequestHandler { + @Override + public void handle(Request request, Response response) throws Exception { + WsClient client = wsClientFactory.newClient(request.localConnector()); + + WsCe.TaskTypesWsResponse ceTaskTypes = client.ce().taskTypes(); + response.stream().setStatus(ceTaskTypes.getTaskTypesCount() > 0 ? 200 : 500); + } + } + + private class JsonHandler implements RequestHandler { + @Override + public void handle(Request request, Response response) throws Exception { + WsClient client = wsClientFactory.newClient(request.localConnector()); + + WsResponse jsonResponse = client.wsConnector().call(new GetRequest("api/issues/search")); + boolean ok = jsonResponse.contentType().equals(MediaTypes.JSON) + && jsonResponse.isSuccessful() + && jsonResponse.content().contains("\"issues\":"); + response.stream().setStatus(ok ? 200 : 500); + } + } + + private class RequirePermissionHandler implements RequestHandler { + @Override + public void handle(Request request, Response response) throws Exception { + WsClient client = wsClientFactory.newClient(request.localConnector()); + + WsResponse jsonResponse = client.wsConnector().call(new GetRequest("api/system/info")); + + boolean ok = MediaTypes.JSON.equals(jsonResponse.contentType()) + && jsonResponse.isSuccessful() + && jsonResponse.content().startsWith("{"); + response.stream().setStatus(ok ? 200 : 500); + } + } +} diff --git a/tests/plugins/ws-plugin/src/main/java/WsPlugin.java b/tests/plugins/ws-plugin/src/main/java/WsPlugin.java new file mode 100644 index 00000000000..e36a8551d0e --- /dev/null +++ b/tests/plugins/ws-plugin/src/main/java/WsPlugin.java @@ -0,0 +1,29 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import java.util.Arrays; +import java.util.List; +import org.sonar.api.SonarPlugin; +import org.sonarqube.ws.client.WsClientFactories; + +public class WsPlugin extends SonarPlugin { + public List getExtensions() { + return Arrays.asList(LocalCallWebService.class, WsClientFactories.getLocal()); + } +} |