From: Teryk Bellahsene Date: Fri, 13 Nov 2015 10:16:04 +0000 (+0100) Subject: SONAR-6947 api/permissions/update_template use UpdateTemplateWsRequest X-Git-Tag: 5.3-RC1~249 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ad4cf4fb0a36a1b12e9f2719e9fee8a7960a5ee4;p=sonarqube.git SONAR-6947 api/permissions/update_template use UpdateTemplateWsRequest --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java index 96c84fc7181..8ff1bf8e961 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java @@ -34,24 +34,25 @@ import org.sonar.server.permission.ws.PermissionsWsAction; import org.sonar.server.permission.ws.WsTemplateRef; import org.sonar.server.user.UserSession; import org.sonarqube.ws.WsPermissions.PermissionTemplate; -import org.sonarqube.ws.WsPermissions.WsUpdatePermissionTemplateResponse; +import org.sonarqube.ws.WsPermissions.UpdateTemplateWsResponse; +import org.sonarqube.ws.client.permission.UpdateTemplateWsRequest; import static com.google.common.base.Objects.firstNonNull; import static java.lang.String.format; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN; -import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createIdParameter; -import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateDescriptionParameter; -import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateProjectKeyPatternParameter; import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME; import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern; import static org.sonar.server.permission.ws.PermissionRequestValidator.validateTemplateNameFormat; +import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createIdParameter; +import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateDescriptionParameter; +import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateProjectKeyPatternParameter; import static org.sonar.server.permission.ws.template.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN; public class UpdateTemplateAction implements PermissionsWsAction { private final DbClient dbClient; @@ -87,13 +88,18 @@ public class UpdateTemplateAction implements PermissionsWsAction { } @Override - public void handle(Request wsRequest, Response wsResponse) throws Exception { + public void handle(Request request, Response response) throws Exception { + UpdateTemplateWsResponse updateTemplateWsResponse = doHandle(toUpdateTemplateWsRequest(request)); + writeProtobuf(updateTemplateWsResponse, request, response); + } + + private UpdateTemplateWsResponse doHandle(UpdateTemplateWsRequest request) { checkGlobalAdminUser(userSession); - String uuid = wsRequest.mandatoryParam(PARAM_ID); - String nameParam = wsRequest.param(PARAM_NAME); - String descriptionParam = wsRequest.param(PARAM_DESCRIPTION); - String projectPatternParam = wsRequest.param(PARAM_PROJECT_KEY_PATTERN); + String uuid = request.getId(); + String nameParam = request.getName(); + String descriptionParam = request.getDescription(); + String projectPatternParam = request.getProjectKeyPattern(); DbSession dbSession = dbClient.openSession(false); try { @@ -101,13 +107,20 @@ public class UpdateTemplateAction implements PermissionsWsAction { validateTemplate(dbSession, templateToUpdate); PermissionTemplateDto updatedTemplate = updateTemplate(dbSession, templateToUpdate); - WsUpdatePermissionTemplateResponse response = buildResponse(updatedTemplate); - writeProtobuf(response, wsRequest, wsResponse); + return buildResponse(updatedTemplate); } finally { dbClient.closeSession(dbSession); } } + private static UpdateTemplateWsRequest toUpdateTemplateWsRequest(Request request) { + return new UpdateTemplateWsRequest() + .setId(request.mandatoryParam(PARAM_ID)) + .setName(request.param(PARAM_NAME)) + .setDescription(request.param(PARAM_DESCRIPTION)) + .setProjectKeyPattern(request.param(PARAM_PROJECT_KEY_PATTERN)); + } + private void validateTemplate(DbSession dbSession, PermissionTemplateDto templateToUpdate) { validateTemplateNameForUpdate(dbSession, templateToUpdate.getName(), templateToUpdate.getId()); validateProjectPattern(templateToUpdate.getKeyPattern()); @@ -128,9 +141,9 @@ public class UpdateTemplateAction implements PermissionsWsAction { return dbClient.permissionTemplateDao().update(dbSession, templateToUpdate); } - private static WsUpdatePermissionTemplateResponse buildResponse(PermissionTemplateDto permissionTemplate) { + private static UpdateTemplateWsResponse buildResponse(PermissionTemplateDto permissionTemplate) { PermissionTemplate permissionTemplateBuilder = toPermissionTemplateResponse(permissionTemplate); - return WsUpdatePermissionTemplateResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build(); + return UpdateTemplateWsResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build(); } private void validateTemplateNameForUpdate(DbSession dbSession, String name, long id) { 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 747c520d402..510cf7adcdd 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 @@ -25,6 +25,7 @@ import org.sonarqube.ws.WsPermissions.CreateTemplateWsResponse; import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse; import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse; import org.sonarqube.ws.WsPermissions.WsSearchGlobalPermissionsResponse; +import org.sonarqube.ws.WsPermissions.UpdateTemplateWsResponse; import org.sonarqube.ws.client.WsClient; import static org.sonarqube.ws.client.WsRequest.newGetRequest; @@ -32,6 +33,7 @@ import static org.sonarqube.ws.client.WsRequest.newPostRequest; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; @@ -184,6 +186,16 @@ public class PermissionsWsClient { .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); } + public UpdateTemplateWsResponse updateTemplate(UpdateTemplateWsRequest request) { + return wsClient.execute( + newPostRequest("update_template") + .setParam(PARAM_DESCRIPTION, request.getDescription()) + .setParam(PARAM_ID, request.getId()) + .setParam(PARAM_NAME, request.getName()) + .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()), + UpdateTemplateWsResponse.parser()); + } + private static String action(String action) { return PermissionsWsParameters.ENDPOINT + "/" + action; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UpdateTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UpdateTemplateWsRequest.java new file mode 100644 index 00000000000..f35436674d9 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UpdateTemplateWsRequest.java @@ -0,0 +1,72 @@ +/* + * 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; + +import static java.util.Objects.requireNonNull; + +public class UpdateTemplateWsRequest { + private String id; + private String description; + private String name; + private String projectKeyPattern; + + public String getId() { + return id; + } + + public UpdateTemplateWsRequest setId(String id) { + this.id = requireNonNull(id); + return this; + } + + @CheckForNull + public String getDescription() { + return description; + } + + public UpdateTemplateWsRequest setDescription(@Nullable String description) { + this.description = description; + return this; + } + + @CheckForNull + public String getName() { + return name; + } + + public UpdateTemplateWsRequest setName(@Nullable String name) { + this.name = name; + return this; + } + + @CheckForNull + public String getProjectKeyPattern() { + return projectKeyPattern; + } + + public UpdateTemplateWsRequest setProjectKeyPattern(@Nullable String projectKeyPattern) { + this.projectKeyPattern = projectKeyPattern; + return this; + } +} diff --git a/sonar-ws/src/main/protobuf/ws-permissions.proto b/sonar-ws/src/main/protobuf/ws-permissions.proto index 6f0c6243f0c..432eda6cb01 100644 --- a/sonar-ws/src/main/protobuf/ws-permissions.proto +++ b/sonar-ws/src/main/protobuf/ws-permissions.proto @@ -62,7 +62,7 @@ message CreateTemplateWsResponse { optional PermissionTemplate permissionTemplate = 1; } -message WsUpdatePermissionTemplateResponse { +message UpdateTemplateWsResponse { optional PermissionTemplate permissionTemplate = 1; }