diff options
7 files changed, 182 insertions, 0 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/plugins_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/plugins_controller.rb new file mode 100644 index 00000000000..147f8ca7071 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/plugins_controller.rb @@ -0,0 +1,66 @@ +# +# Sonar, entreprise quality control tool. +# Copyright (C) 2009 SonarSource SA +# mailto:contact AT sonarsource DOT com +# +# Sonar 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. +# +# Sonar 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 Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# + +require 'json' + +class Api::PluginsController < Api::ApiController + + before_filter :admin_required + + # + # GET /api/plugins + # curl http://localhost:9000/api/plugins -v -u admin:admin + # + def index + respond_to do |format| + format.json { render :json => jsonp(plugins_to_json(Plugin.user_plugins)) } + format.xml { render :xml => plugins_to_xml(Plugin.user_plugins) } + format.text { render :text => text_not_supported } + end + end + + def plugins_to_json(plugins=[]) + json=[] + plugins.each do |p| + json<<plugin_to_json(p) + end + json + end + + def plugin_to_json(plugin) + hash={} + hash['key']=plugin.plugin_key + hash['name']=plugin.name + hash['version']=plugin.version || '-' + hash + end + + def plugins_to_xml(plugins, xml=Builder::XmlMarkup.new(:indent => 0)) + xml.plugins do + plugins.each do |plugin| + xml.plugin do + xml.key(plugin.plugin_key) + xml.name(plugin.name) + xml.version(plugin.version || '-') + end + end + end + end +end diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Plugin.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Plugin.java new file mode 100644 index 00000000000..ce8d4c7e499 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Plugin.java @@ -0,0 +1,39 @@ +package org.sonar.wsclient.services; + +/** + * @since 2.4 + */ +public class Plugin extends Model { + + private String key; + private String name; + private String version; + + public String getKey() { + return key; + } + + public Plugin setKey(String key) { + this.key = key; + return this; + } + + public String getName() { + return name; + } + + public Plugin setName(String name) { + this.name = name; + return this; + } + + public String getVersion() { + return version; + } + + public Plugin setVersion(String version) { + this.version = version; + return this; + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PluginQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PluginQuery.java new file mode 100644 index 00000000000..6ba030cf03c --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PluginQuery.java @@ -0,0 +1,19 @@ +package org.sonar.wsclient.services; + +/** + * @since 2.4 + */ +public class PluginQuery extends Query<Plugin> { + + public static final String BASE_URL = "/api/plugins"; + + @Override + public Class<Plugin> getModelClass() { + return Plugin.class; + } + + @Override + public String getUrl() { + return BASE_URL; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java new file mode 100644 index 00000000000..29d785f485e --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/PluginUnmarshaller.java @@ -0,0 +1,19 @@ +package org.sonar.wsclient.unmarshallers; + +import org.json.simple.JSONObject; +import org.sonar.wsclient.services.Plugin; + +/** + * @since 2.4 + */ +public class PluginUnmarshaller extends AbstractUnmarshaller<Plugin> { + + @Override + protected Plugin parse(JSONObject json) { + return new Plugin() + .setKey(JsonUtils.getString(json, "key")) + .setName(JsonUtils.getString(json, "name")) + .setVersion(JsonUtils.getString(json, "version")); + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java index 3523f1ce836..35f1f31ed58 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java @@ -42,6 +42,7 @@ public final class Unmarshallers { unmarshallers.put(DependencyTree.class, new DependencyTreeUnmarshaller()); unmarshallers.put(Event.class, new EventUnmarshaller()); unmarshallers.put(Favourite.class, new FavouriteUnmarshaller()); + unmarshallers.put(Plugin.class, new PluginUnmarshaller()); } public static <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> modelClass) { diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/PluginQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/PluginQueryTest.java new file mode 100644 index 00000000000..49d226852ee --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/PluginQueryTest.java @@ -0,0 +1,16 @@ +package org.sonar.wsclient.services; + +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class PluginQueryTest { + + @Test + public void index() { + PluginQuery query = new PluginQuery(); + assertThat(query.getUrl(), is("/api/plugins")); + } + +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java new file mode 100644 index 00000000000..615407b15b1 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/PluginUnmarshallerTest.java @@ -0,0 +1,22 @@ +package org.sonar.wsclient.unmarshallers; + +import org.junit.Test; +import org.sonar.wsclient.services.Plugin; + +import java.util.List; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class PluginUnmarshallerTest { + + @Test + public void toModel() throws Exception { + List<Plugin> plugins = new PluginUnmarshaller().toModels("[{\"key\": \"foo\", \"name\": \"Foo\", \"version\": \"1.0\"}]"); + Plugin plugin = plugins.get(0); + assertThat(plugin.getKey(), is("foo")); + assertThat(plugin.getName(), is("Foo")); + assertThat(plugin.getVersion(), is("1.0")); + } + +} |