diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-04-06 09:57:19 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-04-07 12:12:32 +0200 |
commit | a2be8c1b0c660f10b2e86ac046af3cfaea61214d (patch) | |
tree | a21057b61d9fb834c5c751e6f88021632d7fbcc3 /it/it-plugins | |
parent | f6bfd1d2f0373da2d2929d9217721a81fc3afe5d (diff) | |
download | sonarqube-a2be8c1b0c660f10b2e86ac046af3cfaea61214d.tar.gz sonarqube-a2be8c1b0c660f10b2e86ac046af3cfaea61214d.zip |
SONAR-6948 Ability for Java server extensions to call web services
Diffstat (limited to 'it/it-plugins')
-rw-r--r-- | it/it-plugins/pom.xml | 3 | ||||
-rw-r--r-- | it/it-plugins/ws-plugin/pom.xml | 43 | ||||
-rw-r--r-- | it/it-plugins/ws-plugin/src/main/java/LocalCallWebService.java | 85 | ||||
-rw-r--r-- | it/it-plugins/ws-plugin/src/main/java/WsPlugin.java | 30 |
4 files changed, 160 insertions, 1 deletions
diff --git a/it/it-plugins/pom.xml b/it/it-plugins/pom.xml index bee94bef359..c931e338939 100644 --- a/it/it-plugins/pom.xml +++ b/it/it-plugins/pom.xml @@ -36,6 +36,7 @@ <module>dashboard-plugin</module> <module>extension-lifecycle-plugin</module> <module>global-property-change-plugin</module> + <module>issue-filter-plugin</module> <module>l10n-fr-pack</module> <module>license-plugin</module> <module>oauth2-auth-plugin</module> @@ -50,7 +51,7 @@ <module>sonar-fake-plugin</module> <module>sonar-subcategories-plugin</module> <module>ui-extensions-plugin</module> - <module>issue-filter-plugin</module> <module>posttask-plugin</module> + <module>ws-plugin</module> </modules> </project> diff --git a/it/it-plugins/ws-plugin/pom.xml b/it/it-plugins/ws-plugin/pom.xml new file mode 100644 index 00000000000..6ac4a5d3fd8 --- /dev/null +++ b/it/it-plugins/ws-plugin/pom.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.sonarsource.sonarqube</groupId> + <artifactId>it-plugins</artifactId> + <version>5.5-SNAPSHOT</version> + </parent> + + <artifactId>ws-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>sonar-plugin</packaging> + <name>SonarQube Integration Tests :: Plugins :: Ws</name> + <description>Plugin for WS tests</description> + + <dependencies> + <dependency> + <groupId>org.sonarsource.sonarqube</groupId> + <artifactId>sonar-plugin-api</artifactId> + <version>${apiVersion}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.sonarsource.sonarqube</groupId> + <artifactId>sonar-ws</artifactId> + <version>${apiVersion}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> + <artifactId>sonar-packaging-maven-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <pluginClass>WsPlugin</pluginClass> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/it/it-plugins/ws-plugin/src/main/java/LocalCallWebService.java b/it/it-plugins/ws-plugin/src/main/java/LocalCallWebService.java new file mode 100644 index 00000000000..b878ef2c47f --- /dev/null +++ b/it/it-plugins/ws-plugin/src/main/java/LocalCallWebService.java @@ -0,0 +1,85 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.getLocalConnector()); + + 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.getLocalConnector()); + + 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.getLocalConnector()); + + 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/it/it-plugins/ws-plugin/src/main/java/WsPlugin.java b/it/it-plugins/ws-plugin/src/main/java/WsPlugin.java new file mode 100644 index 00000000000..d907059bcee --- /dev/null +++ b/it/it-plugins/ws-plugin/src/main/java/WsPlugin.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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()); + } +} |