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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.branch.BranchFeatureProxy;
  41. import org.sonar.server.organization.DefaultOrganizationProvider;
  42. import org.sonar.server.organization.OrganizationFlags;
  43. import org.sonar.server.platform.WebServer;
  44. import org.sonar.server.ui.PageRepository;
  45. import org.sonar.server.ui.VersionFormatter;
  46. import org.sonar.server.user.UserSession;
  47. import static org.sonar.api.CoreProperties.RATING_GRID;
  48. import static org.sonar.core.config.WebConstants.SONAR_LF_ENABLE_GRAVATAR;
  49. import static org.sonar.core.config.WebConstants.SONAR_LF_GRAVATAR_SERVER_URL;
  50. import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_URL;
  51. import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_WIDTH_PX;
  52. import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_ENABLED;
  53. import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_HOMEPAGE_URL;
  54. import static org.sonar.process.ProcessProperties.Property.SONAR_ANALYTICS_GTM_TRACKING_ID;
  55. import static org.sonar.process.ProcessProperties.Property.SONAR_ANALYTICS_GA_TRACKING_ID;
  56. import static org.sonar.process.ProcessProperties.Property.SONAR_PRISMIC_ACCESS_TOKEN;
  57. import static org.sonar.process.ProcessProperties.Property.SONAR_UPDATECENTER_ACTIVATE;
  58. public class GlobalAction implements NavigationWsAction, Startable {
  59. private static final Set<String> DYNAMIC_SETTING_KEYS = ImmutableSet.of(
  60. SONAR_LF_LOGO_URL,
  61. SONAR_LF_LOGO_WIDTH_PX,
  62. SONAR_LF_ENABLE_GRAVATAR,
  63. SONAR_LF_GRAVATAR_SERVER_URL,
  64. RATING_GRID);
  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 WebServer webServer;
  71. private final DbClient dbClient;
  72. private final OrganizationFlags organizationFlags;
  73. private final DefaultOrganizationProvider defaultOrganizationProvider;
  74. private final BranchFeatureProxy branchFeature;
  75. private final UserSession userSession;
  76. private final PlatformEditionProvider editionProvider;
  77. public GlobalAction(PageRepository pageRepository, Configuration config, ResourceTypes resourceTypes, Server server,
  78. WebServer webServer, DbClient dbClient, OrganizationFlags organizationFlags,
  79. DefaultOrganizationProvider defaultOrganizationProvider, BranchFeatureProxy branchFeature, UserSession userSession, PlatformEditionProvider editionProvider) {
  80. this.pageRepository = pageRepository;
  81. this.config = config;
  82. this.resourceTypes = resourceTypes;
  83. this.server = server;
  84. this.webServer = webServer;
  85. this.dbClient = dbClient;
  86. this.organizationFlags = organizationFlags;
  87. this.defaultOrganizationProvider = defaultOrganizationProvider;
  88. this.branchFeature = branchFeature;
  89. this.userSession = userSession;
  90. this.editionProvider = editionProvider;
  91. this.systemSettingValuesByKey = new HashMap<>();
  92. }
  93. @Override
  94. public void start() {
  95. this.systemSettingValuesByKey.put(SONAR_UPDATECENTER_ACTIVATE.getKey(), config.get(SONAR_UPDATECENTER_ACTIVATE.getKey()).orElse(null));
  96. boolean isOnSonarCloud = config.getBoolean(SONARCLOUD_ENABLED.getKey()).orElse(false);
  97. if (isOnSonarCloud) {
  98. this.systemSettingValuesByKey.put(SONAR_PRISMIC_ACCESS_TOKEN.getKey(), config.get(SONAR_PRISMIC_ACCESS_TOKEN.getKey()).orElse(null));
  99. this.systemSettingValuesByKey.put(SONAR_ANALYTICS_GA_TRACKING_ID.getKey(), config.get(SONAR_ANALYTICS_GA_TRACKING_ID.getKey()).orElse(null));
  100. this.systemSettingValuesByKey.put(SONAR_ANALYTICS_GTM_TRACKING_ID.getKey(), config.get(SONAR_ANALYTICS_GTM_TRACKING_ID.getKey()).orElse(null));
  101. this.systemSettingValuesByKey.put(SONARCLOUD_HOMEPAGE_URL.getKey(), config.get(SONARCLOUD_HOMEPAGE_URL.getKey()).orElse(null));
  102. }
  103. }
  104. @Override
  105. public void stop() {
  106. // Nothing to do
  107. }
  108. @Override
  109. public void define(NewController context) {
  110. context.createAction("global")
  111. .setDescription("Get information concerning global navigation for the current user.")
  112. .setHandler(this)
  113. .setInternal(true)
  114. .setResponseExample(getClass().getResource("global-example.json"))
  115. .setSince("5.2");
  116. }
  117. @Override
  118. public void handle(Request request, Response response) throws Exception {
  119. try (JsonWriter json = response.newJsonWriter()) {
  120. json.beginObject();
  121. writeActions(json);
  122. writePages(json);
  123. writeSettings(json);
  124. writeDeprecatedLogoProperties(json);
  125. writeQualifiers(json);
  126. writeVersion(json);
  127. writeDatabaseProduction(json);
  128. writeOrganizationSupport(json);
  129. writeBranchSupport(json);
  130. editionProvider.get().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH)));
  131. json.prop("standalone", webServer.isStandalone());
  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 writeDatabaseProduction(JsonWriter json) {
  170. json.prop("productionDatabase", !dbClient.getDatabase().getDialect().getId().equals(H2.ID));
  171. }
  172. private void writeOrganizationSupport(JsonWriter json) {
  173. try (DbSession dbSession = dbClient.openSession(false)) {
  174. json.prop("organizationsEnabled", organizationFlags.isEnabled(dbSession));
  175. json.prop("defaultOrganization", defaultOrganizationProvider.get().getKey());
  176. }
  177. }
  178. private void writeBranchSupport(JsonWriter json) {
  179. json.prop("branchesEnabled", branchFeature.isEnabled());
  180. }
  181. }