]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6947 api/permissions/update_template use UpdateTemplateWsRequest
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 13 Nov 2015 10:16:04 +0000 (11:16 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Nov 2015 12:41:04 +0000 (13:41 +0100)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/UpdateTemplateWsRequest.java [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-permissions.proto

index 96c84fc7181ad4ec61d4b2253b24baaf3b5bd176..8ff1bf8e9614030068f6dfb96171c22790c32ec2 100644 (file)
@@ -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) {
index 747c520d4020e86bb8e342a8d9934b0ef8d57096..510cf7adcddd8e122a9af6bd39c038f6a691e057 100644 (file)
@@ -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 (file)
index 0000000..f354366
--- /dev/null
@@ -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;
+  }
+}
index 6f0c6243f0c325384f884ff553159776b99fbbe2..432eda6cb01021c816e888f419d57e7cccf23231 100644 (file)
@@ -62,7 +62,7 @@ message CreateTemplateWsResponse {
   optional PermissionTemplate permissionTemplate = 1;
 }
 
-message WsUpdatePermissionTemplateResponse {
+message UpdateTemplateWsResponse {
   optional PermissionTemplate permissionTemplate = 1;
 }