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.

CreateAction.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.qualityprofile.ws;
  21. import java.io.InputStream;
  22. import org.sonar.api.profiles.ProfileImporter;
  23. import org.sonar.api.resources.Languages;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.server.ws.WebService.NewAction;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.organization.OrganizationDto;
  31. import org.sonar.db.qualityprofile.QProfileDto;
  32. import org.sonar.server.qualityprofile.QProfileExporters;
  33. import org.sonar.server.qualityprofile.QProfileFactory;
  34. import org.sonar.server.qualityprofile.QProfileName;
  35. import org.sonar.server.qualityprofile.QProfileResult;
  36. import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
  37. import org.sonar.server.user.UserSession;
  38. import org.sonarqube.ws.Qualityprofiles.CreateWsResponse;
  39. import javax.annotation.Nullable;
  40. import static com.google.common.base.Preconditions.checkArgument;
  41. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
  42. import static org.sonar.server.qualityprofile.ws.QProfileWsSupport.createOrganizationParam;
  43. import static org.sonar.server.util.LanguageParamUtils.getOrderedLanguageKeys;
  44. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  45. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_CREATE;
  46. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
  47. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_NAME;
  48. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
  49. public class CreateAction implements QProfileWsAction {
  50. private static final String PARAM_BACKUP_FORMAT = "backup_%s";
  51. static final int NAME_MAXIMUM_LENGTH = 100;
  52. private final DbClient dbClient;
  53. private final QProfileFactory profileFactory;
  54. private final QProfileExporters exporters;
  55. private final Languages languages;
  56. private final ProfileImporter[] importers;
  57. private final QProfileWsSupport qProfileWsSupport;
  58. private final UserSession userSession;
  59. private final ActiveRuleIndexer activeRuleIndexer;
  60. public CreateAction(DbClient dbClient, QProfileFactory profileFactory, QProfileExporters exporters, Languages languages,
  61. QProfileWsSupport qProfileWsSupport, UserSession userSession, ActiveRuleIndexer activeRuleIndexer, ProfileImporter... importers) {
  62. this.dbClient = dbClient;
  63. this.profileFactory = profileFactory;
  64. this.exporters = exporters;
  65. this.languages = languages;
  66. this.qProfileWsSupport = qProfileWsSupport;
  67. this.userSession = userSession;
  68. this.activeRuleIndexer = activeRuleIndexer;
  69. this.importers = importers;
  70. }
  71. public CreateAction(DbClient dbClient, QProfileFactory profileFactory, QProfileExporters exporters, Languages languages,
  72. QProfileWsSupport qProfileWsSupport, UserSession userSession, ActiveRuleIndexer activeRuleIndexer) {
  73. this(dbClient, profileFactory, exporters, languages, qProfileWsSupport, userSession, activeRuleIndexer, new ProfileImporter[0]);
  74. }
  75. @Override
  76. public void define(WebService.NewController controller) {
  77. NewAction create = controller.createAction(ACTION_CREATE)
  78. .setPost(true)
  79. .setDescription("Create a quality profile.<br>" +
  80. "Requires to be logged in and the 'Administer Quality Profiles' permission.")
  81. .setResponseExample(getClass().getResource("create-example.json"))
  82. .setSince("5.2")
  83. .setHandler(this);
  84. createOrganizationParam(create)
  85. .setSince("6.4");
  86. create.createParam(PARAM_NAME)
  87. .setRequired(true)
  88. .setMaximumLength(NAME_MAXIMUM_LENGTH)
  89. .setDescription("Quality profile name")
  90. .setExampleValue("My Sonar way")
  91. .setDeprecatedKey("profileName", "6.6");
  92. create.createParam(PARAM_LANGUAGE)
  93. .setRequired(true)
  94. .setDescription("Quality profile language")
  95. .setExampleValue("js")
  96. .setPossibleValues(getOrderedLanguageKeys(languages));
  97. for (ProfileImporter importer : importers) {
  98. create.createParam(getBackupParamName(importer.getKey()))
  99. .setDescription(String.format("A configuration file for %s.", importer.getName()));
  100. }
  101. }
  102. @Override
  103. public void handle(Request request, Response response) throws Exception {
  104. userSession.checkLoggedIn();
  105. try (DbSession dbSession = dbClient.openSession(false)) {
  106. OrganizationDto organization = qProfileWsSupport.getOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION));
  107. userSession.checkPermission(ADMINISTER_QUALITY_PROFILES, organization);
  108. CreateRequest createRequest = toRequest(request, organization);
  109. writeProtobuf(doHandle(dbSession, createRequest, request, organization), request, response);
  110. }
  111. }
  112. private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request, OrganizationDto organization) {
  113. QProfileResult result = new QProfileResult();
  114. QProfileDto profile = profileFactory.checkAndCreateCustom(dbSession, organization,
  115. QProfileName.createFor(createRequest.getLanguage(), createRequest.getName()));
  116. result.setProfile(profile);
  117. for (ProfileImporter importer : importers) {
  118. String importerKey = importer.getKey();
  119. InputStream contentToImport = request.paramAsInputStream(getBackupParamName(importerKey));
  120. if (contentToImport != null) {
  121. result.add(exporters.importXml(profile, importerKey, contentToImport, dbSession));
  122. }
  123. }
  124. activeRuleIndexer.commitAndIndex(dbSession, result.getChanges());
  125. return buildResponse(result, organization);
  126. }
  127. private static CreateRequest toRequest(Request request, OrganizationDto organization) {
  128. Builder builder = CreateRequest.builder()
  129. .setOrganizationKey(organization.getKey())
  130. .setLanguage(request.mandatoryParam(PARAM_LANGUAGE))
  131. .setName(request.mandatoryParam(PARAM_NAME));
  132. return builder.build();
  133. }
  134. private CreateWsResponse buildResponse(QProfileResult result, OrganizationDto organization) {
  135. String language = result.profile().getLanguage();
  136. CreateWsResponse.QualityProfile.Builder builder = CreateWsResponse.QualityProfile.newBuilder()
  137. .setOrganization(organization.getKey())
  138. .setKey(result.profile().getKee())
  139. .setName(result.profile().getName())
  140. .setLanguage(language)
  141. .setLanguageName(languages.get(result.profile().getLanguage()).getName())
  142. .setIsDefault(false)
  143. .setIsInherited(false);
  144. if (!result.infos().isEmpty()) {
  145. builder.getInfosBuilder().addAllInfos(result.infos());
  146. }
  147. if (!result.warnings().isEmpty()) {
  148. builder.getWarningsBuilder().addAllWarnings(result.warnings());
  149. }
  150. return CreateWsResponse.newBuilder().setProfile(builder.build()).build();
  151. }
  152. private static String getBackupParamName(String importerKey) {
  153. return String.format(PARAM_BACKUP_FORMAT, importerKey);
  154. }
  155. private static class CreateRequest {
  156. private final String name;
  157. private final String language;
  158. private final String organizationKey;
  159. private CreateRequest(Builder builder) {
  160. this.name = builder.name;
  161. this.language = builder.language;
  162. this.organizationKey = builder.organizationKey;
  163. }
  164. public String getLanguage() {
  165. return language;
  166. }
  167. public String getName() {
  168. return name;
  169. }
  170. public String getOrganizationKey() {
  171. return organizationKey;
  172. }
  173. public static Builder builder() {
  174. return new Builder();
  175. }
  176. }
  177. private static class Builder {
  178. private String language;
  179. private String name;
  180. private String organizationKey;
  181. private Builder() {
  182. // enforce factory method use
  183. }
  184. public Builder setLanguage(@Nullable String language) {
  185. this.language = language;
  186. return this;
  187. }
  188. public Builder setName(@Nullable String profileName) {
  189. this.name = profileName;
  190. return this;
  191. }
  192. public Builder setOrganizationKey(@Nullable String organizationKey) {
  193. this.organizationKey = organizationKey;
  194. return this;
  195. }
  196. public CreateRequest build() {
  197. checkArgument(language != null && !language.isEmpty(), "Language is mandatory and must not be empty.");
  198. checkArgument(name != null && !name.isEmpty(), "Profile name is mandatory and must not be empty.");
  199. checkArgument(organizationKey == null || !organizationKey.isEmpty(), "Organization key may be either null or not empty. Empty organization key is invalid.");
  200. return new CreateRequest(this);
  201. }
  202. }
  203. }