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;
}
@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 {
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());
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) {
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;
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;
.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;
}
--- /dev/null
+/*
+ * 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;
+ }
+}