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.

InstalledAction.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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 com.google.common.io.Resources;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Objects;
  26. import java.util.SortedSet;
  27. import java.util.function.Function;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.Nullable;
  30. import org.sonar.api.server.ws.Change;
  31. import org.sonar.api.server.ws.Request;
  32. import org.sonar.api.server.ws.Response;
  33. import org.sonar.api.server.ws.WebService;
  34. import org.sonar.core.platform.PluginInfo;
  35. import org.sonar.core.plugin.PluginType;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.permission.GlobalPermission;
  39. import org.sonar.db.plugin.PluginDto;
  40. import org.sonar.db.plugin.PluginDto.Type;
  41. import org.sonar.server.plugins.ServerPlugin;
  42. import org.sonar.server.plugins.ServerPluginRepository;
  43. import org.sonar.server.plugins.UpdateCenterMatrixFactory;
  44. import org.sonar.server.user.UserSession;
  45. import org.sonar.updatecenter.common.Plugin;
  46. import org.sonarqube.ws.Plugins.InstalledPluginsWsResponse;
  47. import org.sonarqube.ws.Plugins.PluginDetails;
  48. import static com.google.common.collect.ImmutableSortedSet.copyOf;
  49. import static java.lang.String.format;
  50. import static java.util.Collections.emptyMap;
  51. import static java.util.Collections.singleton;
  52. import static java.util.stream.Collectors.toMap;
  53. import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_COMPARATOR;
  54. import static org.sonar.server.plugins.ws.PluginWSCommons.buildPluginDetails;
  55. import static org.sonar.server.plugins.ws.PluginWSCommons.compatiblePluginsByKey;
  56. import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
  57. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  58. /**
  59. * Implementation of the {@code installed} action for the Plugins WebService.
  60. */
  61. public class InstalledAction implements PluginsWsAction {
  62. private static final String FIELD_CATEGORY = "category";
  63. private static final String PARAM_TYPE = "type";
  64. private final UserSession userSession;
  65. private final ServerPluginRepository serverPluginRepository;
  66. private final UpdateCenterMatrixFactory updateCenterMatrixFactory;
  67. private final DbClient dbClient;
  68. public InstalledAction(ServerPluginRepository serverPluginRepository, UserSession userSession, UpdateCenterMatrixFactory updateCenterMatrixFactory, DbClient dbClient) {
  69. this.userSession = userSession;
  70. this.serverPluginRepository = serverPluginRepository;
  71. this.updateCenterMatrixFactory = updateCenterMatrixFactory;
  72. this.dbClient = dbClient;
  73. }
  74. @Override
  75. public void define(WebService.NewController controller) {
  76. WebService.NewAction action = controller.createAction("installed")
  77. .setDescription("Get the list of all the plugins installed on the SonarQube instance, sorted by plugin name.<br/>" +
  78. "Requires authentication.")
  79. .setSince("5.2")
  80. .setChangelog(
  81. new Change("10.4", "The response field 'requiredForLanguages' is added for plugins that support it"),
  82. new Change("9.8", "The 'documentationPath' field is deprecated"),
  83. new Change("9.7", "Authentication check added"),
  84. new Change("8.0", "The 'documentationPath' field is added"),
  85. new Change("7.0", "The fields 'compressedHash' and 'compressedFilename' are added"),
  86. new Change("6.6", "The 'filename' field is added"),
  87. new Change("6.6", "The 'fileHash' field is added"),
  88. new Change("6.6", "The 'sonarLintSupported' field is added"),
  89. new Change("6.6", "The 'updatedAt' field is added"))
  90. .setHandler(this)
  91. .setResponseExample(Resources.getResource(this.getClass(), "example-installed_plugins.json"));
  92. action.createFieldsParam(singleton("category"))
  93. .setDescription(format("Comma-separated list of the additional fields to be returned in response. No additional field is returned by default. Possible values are:" +
  94. "<ul>" +
  95. "<li>%s - category as defined in the Update Center. A connection to the Update Center is needed</li>" +
  96. "</ul>", FIELD_CATEGORY))
  97. .setSince("5.6");
  98. action.createParam(PARAM_TYPE)
  99. .setInternal(true)
  100. .setSince("8.5")
  101. .setPossibleValues(Type.values())
  102. .setDescription("Allows to filter plugins by type");
  103. }
  104. @Override
  105. public void handle(Request request, Response response) throws Exception {
  106. if (!userSession.isLoggedIn() && !userSession.hasPermission(GlobalPermission.SCAN)) {
  107. throw insufficientPrivilegesException();
  108. }
  109. String typeParam = request.param(PARAM_TYPE);
  110. SortedSet<ServerPlugin> installedPlugins = loadInstalledPlugins(typeParam);
  111. Map<String, PluginDto> dtosByKey;
  112. try (DbSession dbSession = dbClient.openSession(false)) {
  113. dtosByKey = dbClient.pluginDao().selectAll(dbSession).stream().collect(toMap(PluginDto::getKee, Function.identity()));
  114. }
  115. List<String> additionalFields = request.paramAsStrings(WebService.Param.FIELDS);
  116. Map<String, Plugin> updateCenterPlugins = (additionalFields == null || additionalFields.isEmpty()) ? emptyMap() : compatiblePluginsByKey(updateCenterMatrixFactory);
  117. List<PluginDetails> pluginList = new LinkedList<>();
  118. for (ServerPlugin installedPlugin : installedPlugins) {
  119. PluginInfo pluginInfo = installedPlugin.getPluginInfo();
  120. PluginDto pluginDto = dtosByKey.get(pluginInfo.getKey());
  121. Objects.requireNonNull(pluginDto, () -> format("Plugin %s is installed but not in DB", pluginInfo.getKey()));
  122. Plugin updateCenterPlugin = updateCenterPlugins.get(pluginInfo.getKey());
  123. pluginList.add(buildPluginDetails(installedPlugin, pluginInfo, pluginDto, updateCenterPlugin));
  124. }
  125. InstalledPluginsWsResponse wsResponse = InstalledPluginsWsResponse.newBuilder()
  126. .addAllPlugins(pluginList)
  127. .build();
  128. writeProtobuf(wsResponse, request, response);
  129. }
  130. private SortedSet<ServerPlugin> loadInstalledPlugins(@Nullable String typeParam) {
  131. if (typeParam != null) {
  132. return copyOf(NAME_KEY_COMPARATOR, serverPluginRepository.getPlugins().stream()
  133. .filter(serverPlugin -> serverPlugin.getType().equals(PluginType.valueOf(typeParam)))
  134. .collect(Collectors.toSet()));
  135. }
  136. return copyOf(NAME_KEY_COMPARATOR, serverPluginRepository.getPlugins());
  137. }
  138. }