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.

UninstallPluginsWsAction.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.core.permission.GlobalPermissions;
  25. import org.sonar.server.plugins.ServerPluginRepository;
  26. import org.sonar.server.user.UserSession;
  27. import static java.lang.String.format;
  28. /**
  29. * Implementation of the {@code uninstall} action for the Plugins WebService.
  30. */
  31. public class UninstallPluginsWsAction implements PluginsWsAction {
  32. private static final String PARAM_KEY = "key";
  33. private final ServerPluginRepository pluginRepository;
  34. public UninstallPluginsWsAction(ServerPluginRepository pluginRepository) {
  35. this.pluginRepository = pluginRepository;
  36. }
  37. @Override
  38. public void define(WebService.NewController controller) {
  39. WebService.NewAction action = controller.createAction("uninstall")
  40. .setPost(true)
  41. .setDescription("Uninstalls the plugin specified by its key." +
  42. "<br/>" +
  43. "Requires user to be authenticated with Administer System permissions.")
  44. .setHandler(this);
  45. action.createParam(PARAM_KEY)
  46. .setDescription("The key identifying the plugin to uninstall")
  47. .setRequired(true);
  48. }
  49. @Override
  50. public void handle(Request request, Response response) throws Exception {
  51. UserSession.get().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
  52. String key = request.mandatoryParam(PARAM_KEY);
  53. ensurePluginIsInstalled(key);
  54. pluginRepository.uninstall(key);
  55. response.noContent();
  56. }
  57. // FIXME should be moved to {@link ServerPluginRepository#uninstall(String)}
  58. private void ensurePluginIsInstalled(String key) {
  59. if (!pluginRepository.hasPlugin(key)) {
  60. throw new IllegalArgumentException(format("Plugin [%s] is not installed", key));
  61. }
  62. }
  63. }