diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-12 12:02:42 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-17 13:41:00 +0100 |
commit | 31f6f53074997281db532ebc75bf5006929e9c6a (patch) | |
tree | 8757412857b12f08843b2b5f6396e64768a62c0c | |
parent | 50f694232ddc2f21f3a9e9d91b5662e489c955cf (diff) | |
download | sonarqube-31f6f53074997281db532ebc75bf5006929e9c6a.tar.gz sonarqube-31f6f53074997281db532ebc75bf5006929e9c6a.zip |
SONAR-6947 Create and use ApplyTemplateWsRequest
4 files changed, 109 insertions, 8 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java index 81f3b767bd3..6384e26b997 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/WsProjectRef.java @@ -25,14 +25,15 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; +import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; -import static org.sonar.server.ws.WsUtils.checkRequest; /** * Project identifiers from a WS request. Guaranties the project id and project key are not provided at the same time. */ public class WsProjectRef { + private static final String MSG_ID_OR_KEY_MUST_BE_PROVIDED = "Project id or project key must be provided, not both."; private final String uuid; private final String key; @@ -63,11 +64,16 @@ public class WsProjectRef { } public static WsProjectRef newWsProjectRef(Request wsRequest) { - checkRequest(hasProjectParam(wsRequest), "Project id or project key must be provided, not both."); + checkRequest(hasProjectParam(wsRequest), MSG_ID_OR_KEY_MUST_BE_PROVIDED); return new WsProjectRef(wsRequest); } + public static WsProjectRef newWsProjectRef(@Nullable String uuid, @Nullable String key) { + checkRequest(uuid == null ^ key == null, MSG_ID_OR_KEY_MUST_BE_PROVIDED); + return new WsProjectRef(uuid, key); + } + @CheckForNull public String uuid() { return this.uuid; diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java index 169b1490094..69bfa361501 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java @@ -31,12 +31,17 @@ import org.sonar.server.permission.ApplyPermissionTemplateQuery; import org.sonar.server.permission.PermissionService; import org.sonar.server.permission.ws.PermissionDependenciesFinder; import org.sonar.server.permission.ws.PermissionsWsAction; -import org.sonar.server.permission.ws.WsProjectRef; -import org.sonar.server.permission.ws.WsTemplateRef; +import org.sonarqube.ws.client.permission.ApplyTemplateWsRequest; import static java.util.Collections.singletonList; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters; +import static org.sonar.server.permission.ws.WsProjectRef.newWsProjectRef; +import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; public class ApplyTemplateAction implements PermissionsWsAction { private final DbClient dbClient; @@ -64,11 +69,16 @@ public class ApplyTemplateAction implements PermissionsWsAction { } @Override - public void handle(Request wsRequest, Response wsResponse) throws Exception { + public void handle(Request request, Response response) throws Exception { + doHandle(toApplyTemplateWsRequest(request)); + response.noContent(); + } + + private void doHandle(ApplyTemplateWsRequest request) { DbSession dbSession = dbClient.openSession(false); try { - PermissionTemplateDto template = finder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest)); - ComponentDto project = finder.getRootComponentOrModule(dbSession, WsProjectRef.newWsProjectRef(wsRequest)); + PermissionTemplateDto template = finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName())); + ComponentDto project = finder.getRootComponentOrModule(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey())); ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create( template.getUuid(), @@ -77,7 +87,13 @@ public class ApplyTemplateAction implements PermissionsWsAction { } finally { dbClient.closeSession(dbSession); } + } - wsResponse.noContent(); + private static ApplyTemplateWsRequest toApplyTemplateWsRequest(Request request) { + return new ApplyTemplateWsRequest() + .setProjectId(request.param(PARAM_PROJECT_ID)) + .setProjectKey(request.param(PARAM_PROJECT_KEY)) + .setTemplateId(request.param(PARAM_TEMPLATE_ID)) + .setTemplateName(request.param(PARAM_TEMPLATE_NAME)); } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/ApplyTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/ApplyTemplateWsRequest.java new file mode 100644 index 00000000000..6201872414e --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/ApplyTemplateWsRequest.java @@ -0,0 +1,71 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonarqube.ws.client.permission; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public class ApplyTemplateWsRequest { + private String projectId; + private String projectKey; + private String templateId; + private String templateName; + + @CheckForNull + public String getProjectId() { + return projectId; + } + + public ApplyTemplateWsRequest setProjectId(@Nullable String projectId) { + this.projectId = projectId; + return this; + } + + @CheckForNull + public String getProjectKey() { + return projectKey; + } + + public ApplyTemplateWsRequest setProjectKey(@Nullable String projectKey) { + this.projectKey = projectKey; + return this; + } + + @CheckForNull + public String getTemplateId() { + return templateId; + } + + public ApplyTemplateWsRequest setTemplateId(@Nullable String templateId) { + this.templateId = templateId; + return this; + } + + @CheckForNull + public String getTemplateName() { + return templateName; + } + + public ApplyTemplateWsRequest setTemplateName(@Nullable String templateName) { + this.templateName = templateName; + return this; + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java index ba75a5cef85..40da04c5800 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java @@ -87,6 +87,14 @@ public class PermissionsWsClient { .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); } + public void applyTemplate(ApplyTemplateWsRequest request) { + wsClient.execute(newPostRequest(action("apply_template")) + .setParam(PARAM_PROJECT_ID, request.getProjectId()) + .setParam(PARAM_PROJECT_KEY, request.getProjectKey()) + .setParam(PARAM_TEMPLATE_ID, request.getTemplateId()) + .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); + } + private static String action(String action) { return PermissionsWsParameters.ENDPOINT + "/" + action; } |