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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.batch;
  21. import org.apache.commons.io.IOUtils;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.batch.protocol.input.GlobalRepositories;
  26. import org.sonar.db.metric.MetricDto;
  27. import org.sonar.core.permission.GlobalPermissions;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.MyBatis;
  30. import org.sonar.db.property.PropertiesDao;
  31. import org.sonar.db.property.PropertyDto;
  32. import org.sonar.server.db.DbClient;
  33. import org.sonar.server.exceptions.ForbiddenException;
  34. import org.sonar.server.plugins.MimeTypes;
  35. import org.sonar.server.user.UserSession;
  36. public class GlobalAction implements BatchWsAction {
  37. private final DbClient dbClient;
  38. private final PropertiesDao propertiesDao;
  39. private final UserSession userSession;
  40. public GlobalAction(DbClient dbClient, PropertiesDao propertiesDao, UserSession userSession) {
  41. this.dbClient = dbClient;
  42. this.propertiesDao = propertiesDao;
  43. this.userSession = userSession;
  44. }
  45. @Override
  46. public void define(WebService.NewController controller) {
  47. controller.createAction("global")
  48. .setDescription("Return metrics and global properties")
  49. .setSince("4.5")
  50. .setInternal(true)
  51. .setHandler(this);
  52. }
  53. @Override
  54. public void handle(Request request, Response response) throws Exception {
  55. boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
  56. boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.PREVIEW_EXECUTION);
  57. if (!hasPreviewPerm && !hasScanPerm) {
  58. throw new ForbiddenException(Messages.NO_PERMISSION);
  59. }
  60. DbSession session = dbClient.openSession(false);
  61. try {
  62. GlobalRepositories ref = new GlobalRepositories();
  63. addMetrics(ref, session);
  64. addSettings(ref, hasScanPerm, hasPreviewPerm, session);
  65. response.stream().setMediaType(MimeTypes.JSON);
  66. IOUtils.write(ref.toJson(), response.stream().output());
  67. } finally {
  68. MyBatis.closeQuietly(session);
  69. }
  70. }
  71. private void addMetrics(GlobalRepositories ref, DbSession session) {
  72. for (MetricDto metric : dbClient.metricDao().selectEnabled(session)) {
  73. ref.addMetric(
  74. new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
  75. metric.getValueType(),
  76. metric.getDescription(),
  77. metric.getDirection(),
  78. metric.getKey(),
  79. metric.isQualitative(),
  80. metric.isUserManaged(),
  81. metric.getWorstValue(),
  82. metric.getBestValue(),
  83. metric.isOptimizedBestValue()));
  84. }
  85. }
  86. private void addSettings(GlobalRepositories ref, boolean hasScanPerm, boolean hasPreviewPerm, DbSession session) {
  87. for (PropertyDto propertyDto : propertiesDao.selectGlobalProperties(session)) {
  88. String key = propertyDto.getKey();
  89. String value = propertyDto.getValue();
  90. if (isPropertyAllowed(key, hasScanPerm, hasPreviewPerm)) {
  91. ref.addGlobalSetting(key, value);
  92. }
  93. }
  94. }
  95. private static boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasPreviewPerm) {
  96. return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasPreviewPerm);
  97. }
  98. }