You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PendingPluginsWsAction.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins.ws;
  21. import org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.api.utils.text.JsonWriter;
  25. import org.sonar.core.platform.PluginInfo;
  26. import org.sonar.server.plugins.PluginDownloader;
  27. import org.sonar.server.plugins.ServerPluginRepository;
  28. import java.util.Collection;
  29. import static com.google.common.collect.ImmutableSortedSet.copyOf;
  30. import static com.google.common.io.Resources.getResource;
  31. import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_PLUGIN_METADATA_COMPARATOR;
  32. /**
  33. * Implementation of the {@code pending} action for the Plugins WebService.
  34. */
  35. public class PendingPluginsWsAction implements PluginsWsAction {
  36. private static final String ARRAY_INSTALLING = "installing";
  37. private static final String ARRAY_REMOVING = "removing";
  38. private final PluginDownloader pluginDownloader;
  39. private final ServerPluginRepository installer;
  40. private final PluginWSCommons pluginWSCommons;
  41. public PendingPluginsWsAction(PluginDownloader pluginDownloader,
  42. ServerPluginRepository installer,
  43. PluginWSCommons pluginWSCommons) {
  44. this.pluginDownloader = pluginDownloader;
  45. this.installer = installer;
  46. this.pluginWSCommons = pluginWSCommons;
  47. }
  48. @Override
  49. public void define(WebService.NewController controller) {
  50. controller.createAction("pending")
  51. .setDescription("Get the list of plugins which will either be installed or removed at the next startup of the SonarQube instance, sorted by plugin name")
  52. .setSince("5.2")
  53. .setHandler(this)
  54. .setResponseExample(getResource(this.getClass(), "example-pending_plugins.json"));
  55. }
  56. @Override
  57. public void handle(Request request, Response response) throws Exception {
  58. JsonWriter jsonWriter = response.newJsonWriter();
  59. jsonWriter.beginObject();
  60. writeInstalling(jsonWriter);
  61. writeRemoving(jsonWriter);
  62. jsonWriter.endObject();
  63. jsonWriter.close();
  64. }
  65. private void writeInstalling(JsonWriter jsonWriter) {
  66. jsonWriter.name(ARRAY_INSTALLING);
  67. jsonWriter.beginArray();
  68. Collection<PluginInfo> plugins = pluginDownloader.getDownloadedPlugins();
  69. for (PluginInfo pluginMetadata : copyOf(NAME_KEY_PLUGIN_METADATA_COMPARATOR, plugins)) {
  70. pluginWSCommons.writePluginMetadata(jsonWriter, pluginMetadata);
  71. }
  72. jsonWriter.endArray();
  73. }
  74. private void writeRemoving(JsonWriter jsonWriter) {
  75. jsonWriter.name(ARRAY_REMOVING);
  76. jsonWriter.beginArray();
  77. Collection<PluginInfo> plugins = installer.getUninstalledPlugins();
  78. for (PluginInfo pluginMetadata : copyOf(NAME_KEY_PLUGIN_METADATA_COMPARATOR, plugins)) {
  79. pluginWSCommons.writePluginMetadata(jsonWriter, pluginMetadata);
  80. }
  81. jsonWriter.endArray();
  82. }
  83. }