From 2558913cf42dcea7bff37c6ace59b99a7104d31a Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Thu, 23 Mar 2017 16:50:45 +0100 Subject: [PATCH] SONAR-8857 reduce duplicated code for bulk requests --- .../ws/ActivateRulesAction.java | 25 +--------- .../ws/BulkChangeWsResponse.java | 49 +++++++++++++++++++ .../ws/DeactivateRulesAction.java | 25 +--------- 3 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRulesAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRulesAction.java index fc1b59f19e9..77b79a3b91e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRulesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRulesAction.java @@ -19,13 +19,11 @@ */ package org.sonar.server.qualityprofile.ws; -import java.util.List; import org.sonar.api.rule.Severity; import org.sonar.api.server.ServerSide; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.qualityprofile.BulkChangeResult; import org.sonar.server.qualityprofile.QProfileService; import org.sonar.server.rule.ws.RuleQueryFactory; @@ -74,27 +72,6 @@ public class ActivateRulesAction implements QProfileWsAction { ruleQueryFactory.createRuleQuery(request), request.mandatoryParam(PROFILE_KEY), request.param(SEVERITY)); - writeResponse(result, response); - } - - private static void writeResponse(BulkChangeResult result, Response response) { - JsonWriter json = response.newJsonWriter().beginObject(); - json.prop("succeeded", result.countSucceeded()); - json.prop("failed", result.countFailed()); - writeErrors(json, result.getErrors()); - json.endObject().close(); - } - - private static void writeErrors(JsonWriter json, List errorMessages) { - if (errorMessages.isEmpty()) { - return; - } - json.name("errors").beginArray(); - errorMessages.forEach(message -> { - json.beginObject(); - json.prop("msg", message); - json.endObject(); - }); - json.endArray(); + BulkChangeWsResponse.writeResponse(result, response); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java new file mode 100644 index 00000000000..6f970d988cb --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.sonar.server.qualityprofile.ws; + +import java.util.List; +import org.sonar.api.server.ws.Response; +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.server.qualityprofile.BulkChangeResult; + +class BulkChangeWsResponse { + + static void writeResponse(BulkChangeResult result, Response response) { + JsonWriter json = response.newJsonWriter().beginObject(); + json.prop("succeeded", result.countSucceeded()); + json.prop("failed", result.countFailed()); + writeErrors(json, result.getErrors()); + json.endObject().close(); + } + + private static void writeErrors(JsonWriter json, List errorMessages) { + if (errorMessages.isEmpty()) { + return; + } + json.name("errors").beginArray(); + errorMessages.forEach(message -> { + json.beginObject(); + json.prop("msg", message); + json.endObject(); + }); + json.endArray(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRulesAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRulesAction.java index 36d89923f11..e69773d4381 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRulesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRulesAction.java @@ -19,12 +19,10 @@ */ package org.sonar.server.qualityprofile.ws; -import java.util.List; import org.sonar.api.server.ServerSide; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.qualityprofile.BulkChangeResult; import org.sonar.server.qualityprofile.QProfileService; import org.sonar.server.rule.ws.RuleQueryFactory; @@ -68,27 +66,6 @@ public class DeactivateRulesAction implements QProfileWsAction { BulkChangeResult result = profileService.bulkDeactivate( ruleQueryFactory.createRuleQuery(request), request.mandatoryParam(PROFILE_KEY)); - writeResponse(result, response); - } - - private static void writeResponse(BulkChangeResult result, Response response) { - JsonWriter json = response.newJsonWriter().beginObject(); - json.prop("succeeded", result.countSucceeded()); - json.prop("failed", result.countFailed()); - writeErrors(json, result.getErrors()); - json.endObject().close(); - } - - private static void writeErrors(JsonWriter json, List errorMessages) { - if (errorMessages.isEmpty()) { - return; - } - json.name("errors").beginArray(); - errorMessages.forEach(message -> { - json.beginObject(); - json.prop("msg", message); - json.endObject(); - }); - json.endArray(); + BulkChangeWsResponse.writeResponse(result, response); } } -- 2.39.5