Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

GlobalAction.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ui.ws;
  21. import java.util.HashMap;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.sonar.api.Startable;
  26. import org.sonar.api.config.Configuration;
  27. import org.sonar.api.platform.Server;
  28. import org.sonar.api.resources.ResourceType;
  29. import org.sonar.api.resources.ResourceTypes;
  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.NewController;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.api.utils.text.JsonWriter;
  36. import org.sonar.api.web.page.Page;
  37. import org.sonar.core.documentation.DocumentationLinkGenerator;
  38. import org.sonar.core.platform.PlatformEditionProvider;
  39. import org.sonar.db.DbClient;
  40. import org.sonar.db.DbSession;
  41. import org.sonar.db.dialect.H2;
  42. import org.sonar.server.authentication.DefaultAdminCredentialsVerifier;
  43. import org.sonar.server.issue.index.IssueIndexSyncProgressChecker;
  44. import org.sonar.server.platform.NodeInformation;
  45. import org.sonar.server.ui.PageRepository;
  46. import org.sonar.server.ui.VersionFormatter;
  47. import org.sonar.server.ui.WebAnalyticsLoader;
  48. import org.sonar.server.user.UserSession;
  49. import static org.sonar.api.CoreProperties.DEVELOPER_AGGREGATED_INFO_DISABLED;
  50. import static org.sonar.api.CoreProperties.RATING_GRID;
  51. import static org.sonar.api.internal.MetadataLoader.loadSqVersionEol;
  52. import static org.sonar.core.config.WebConstants.SONAR_LF_ENABLE_GRAVATAR;
  53. import static org.sonar.core.config.WebConstants.SONAR_LF_GRAVATAR_SERVER_URL;
  54. import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_URL;
  55. import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_WIDTH_PX;
  56. import static org.sonar.process.ProcessProperties.Property.SONAR_UPDATECENTER_ACTIVATE;
  57. public class GlobalAction implements NavigationWsAction, Startable {
  58. private static final Set<String> DYNAMIC_SETTING_KEYS = Set.of(
  59. SONAR_LF_LOGO_URL,
  60. SONAR_LF_LOGO_WIDTH_PX,
  61. SONAR_LF_ENABLE_GRAVATAR,
  62. SONAR_LF_GRAVATAR_SERVER_URL,
  63. RATING_GRID,
  64. DEVELOPER_AGGREGATED_INFO_DISABLED);
  65. private final Map<String, String> systemSettingValuesByKey;
  66. private final PageRepository pageRepository;
  67. private final Configuration config;
  68. private final ResourceTypes resourceTypes;
  69. private final Server server;
  70. private final NodeInformation nodeInformation;
  71. private final DbClient dbClient;
  72. private final UserSession userSession;
  73. private final PlatformEditionProvider editionProvider;
  74. private final WebAnalyticsLoader webAnalyticsLoader;
  75. private final IssueIndexSyncProgressChecker issueIndexSyncChecker;
  76. private final DefaultAdminCredentialsVerifier defaultAdminCredentialsVerifier;
  77. private final DocumentationLinkGenerator documentationLinkGenerator;
  78. public GlobalAction(PageRepository pageRepository, Configuration config, ResourceTypes resourceTypes, Server server,
  79. NodeInformation nodeInformation, DbClient dbClient, UserSession userSession, PlatformEditionProvider editionProvider,
  80. WebAnalyticsLoader webAnalyticsLoader, IssueIndexSyncProgressChecker issueIndexSyncChecker,
  81. DefaultAdminCredentialsVerifier defaultAdminCredentialsVerifier, DocumentationLinkGenerator documentationLinkGenerator) {
  82. this.pageRepository = pageRepository;
  83. this.config = config;
  84. this.resourceTypes = resourceTypes;
  85. this.server = server;
  86. this.nodeInformation = nodeInformation;
  87. this.dbClient = dbClient;
  88. this.userSession = userSession;
  89. this.editionProvider = editionProvider;
  90. this.webAnalyticsLoader = webAnalyticsLoader;
  91. this.systemSettingValuesByKey = new HashMap<>();
  92. this.issueIndexSyncChecker = issueIndexSyncChecker;
  93. this.defaultAdminCredentialsVerifier = defaultAdminCredentialsVerifier;
  94. this.documentationLinkGenerator = documentationLinkGenerator;
  95. }
  96. @Override
  97. public void start() {
  98. this.systemSettingValuesByKey.put(SONAR_UPDATECENTER_ACTIVATE.getKey(), config.get(SONAR_UPDATECENTER_ACTIVATE.getKey()).orElse(null));
  99. }
  100. @Override
  101. public void stop() {
  102. // Nothing to do
  103. }
  104. @Override
  105. public void define(NewController context) {
  106. context.createAction("global")
  107. .setDescription("Get information concerning global navigation for the current user.")
  108. .setHandler(this)
  109. .setInternal(true)
  110. .setResponseExample(getClass().getResource("global-example.json"))
  111. .setSince("5.2")
  112. .setChangelog(new Change("10.5", "Field 'versionEOL' added, to indicate the end of support of installed version."));
  113. }
  114. @Override
  115. public void handle(Request request, Response response) throws Exception {
  116. try (JsonWriter json = response.newJsonWriter()) {
  117. json.beginObject();
  118. writeActions(json);
  119. writePages(json);
  120. writeSettings(json);
  121. writeDeprecatedLogoProperties(json);
  122. writeQualifiers(json);
  123. writeVersion(json);
  124. writeVersionEol(json);
  125. writeDatabaseProduction(json);
  126. writeInstanceUsesDefaultAdminCredentials(json);
  127. editionProvider.get().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH)));
  128. writeNeedIssueSync(json);
  129. json.prop("standalone", nodeInformation.isStandalone());
  130. writeWebAnalytics(json);
  131. writeDocumentationUrl(json);
  132. json.endObject();
  133. }
  134. }
  135. private void writeActions(JsonWriter json) {
  136. json.prop("canAdmin", userSession.isSystemAdministrator());
  137. }
  138. private void writePages(JsonWriter json) {
  139. json.name("globalPages").beginArray();
  140. for (Page page : pageRepository.getGlobalPages(false)) {
  141. json.beginObject()
  142. .prop("key", page.getKey())
  143. .prop("name", page.getName())
  144. .endObject();
  145. }
  146. json.endArray();
  147. }
  148. private void writeSettings(JsonWriter json) {
  149. json.name("settings").beginObject();
  150. DYNAMIC_SETTING_KEYS.forEach(key -> json.prop(key, config.get(key).orElse(null)));
  151. systemSettingValuesByKey.forEach(json::prop);
  152. json.endObject();
  153. }
  154. private void writeDeprecatedLogoProperties(JsonWriter json) {
  155. json.prop("logoUrl", config.get(SONAR_LF_LOGO_URL).orElse(null));
  156. json.prop("logoWidth", config.get(SONAR_LF_LOGO_WIDTH_PX).orElse(null));
  157. }
  158. private void writeQualifiers(JsonWriter json) {
  159. json.name("qualifiers").beginArray();
  160. for (ResourceType rootType : resourceTypes.getRoots()) {
  161. json.value(rootType.getQualifier());
  162. }
  163. json.endArray();
  164. }
  165. private void writeVersion(JsonWriter json) {
  166. String displayVersion = VersionFormatter.format(server.getVersion());
  167. json.prop("version", displayVersion);
  168. }
  169. private void writeVersionEol(JsonWriter json) {
  170. json.prop("versionEOL", loadSqVersionEol(System2.INSTANCE));
  171. }
  172. private void writeDatabaseProduction(JsonWriter json) {
  173. json.prop("productionDatabase", !dbClient.getDatabase().getDialect().getId().equals(H2.ID));
  174. }
  175. private void writeInstanceUsesDefaultAdminCredentials(JsonWriter json) {
  176. if (userSession.isSystemAdministrator()) {
  177. json.prop("instanceUsesDefaultAdminCredentials", defaultAdminCredentialsVerifier.hasDefaultCredentialUser());
  178. }
  179. }
  180. private void writeNeedIssueSync(JsonWriter json) {
  181. try (DbSession dbSession = dbClient.openSession(false)) {
  182. json.prop("needIssueSync", issueIndexSyncChecker.isIssueSyncInProgress(dbSession));
  183. }
  184. }
  185. private void writeWebAnalytics(JsonWriter json) {
  186. webAnalyticsLoader.getUrlPathToJs().ifPresent(p -> json.prop("webAnalyticsJsPath", p));
  187. }
  188. private void writeDocumentationUrl(JsonWriter json) {
  189. json.prop("documentationUrl", documentationLinkGenerator.getDocumentationLink(null));
  190. }
  191. }