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.

GlobalAction.java 9.1KB

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