From 050021cd081cf7f0f34d6fb54f30a217e6dd6c14 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 2 Mar 2017 09:23:08 +0100 Subject: [PATCH] SONAR-8460 Do not fail to write response when profile not found --- .../qualityprofile/ws/ExportAction.java | 23 ++++++++++++------- .../qualityprofile/ws/ExportActionTest.java | 3 --- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java index f7c4d056202..c6202efefbb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java @@ -19,6 +19,7 @@ */ package org.sonar.server.qualityprofile.ws; +import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; @@ -104,11 +105,16 @@ public class ExportAction implements QProfileWsAction { String name = request.param(PARAM_PROFILE_NAME); String language = request.mandatoryParam(PARAM_LANGUAGE); String exporterKey = exporters.exportersForLanguage(language).isEmpty() ? null : request.param(PARAM_FORMAT); + QualityProfileDto profile = getProfile(name, language); + writeResponse(profile, exporterKey, response); + } + + private void writeResponse(QualityProfileDto profile, @Nullable String exporterKey, Response response) throws IOException { Stream stream = response.stream(); - try (DbSession dbSession = dbClient.openSession(false); - OutputStream output = stream.output(); + try ( + DbSession dbSession = dbClient.openSession(false); + OutputStream output = response.stream().output(); Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) { - QualityProfileDto profile = getProfile(dbSession, name, language); if (exporterKey == null) { stream.setMediaType(MediaTypes.XML); backuper.backup(dbSession, profile, writer); @@ -119,10 +125,11 @@ public class ExportAction implements QProfileWsAction { } } - private QualityProfileDto getProfile(DbSession dbSession, @Nullable String name, String language) { - QualityProfileDto profile = name == null ? dbClient.qualityProfileDao().selectDefaultProfile(dbSession, language) - : dbClient.qualityProfileDao().selectByNameAndLanguage(name, language, dbSession); - return checkFound(profile, "Could not find profile with name '%s' for language '%s'", name, language); + private QualityProfileDto getProfile(@Nullable String name, String language) { + try (DbSession dbSession = dbClient.openSession(false)) { + QualityProfileDto profile = name == null ? dbClient.qualityProfileDao().selectDefaultProfile(dbSession, language) + : dbClient.qualityProfileDao().selectByNameAndLanguage(name, language, dbSession); + return checkFound(profile, "Could not find profile with name '%s' for language '%s'", name, language); + } } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java index 619d3720836..2d9e2c05106 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java @@ -32,7 +32,6 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.language.LanguageTesting; @@ -55,8 +54,6 @@ public class ExportActionTest { public DbTester db = DbTester.create(System2.INSTANCE); private DbClient dbClient = db.getDbClient(); - private DbSession session = db.getSession(); - private QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao(); private QProfileBackuper backuper = mock(QProfileBackuper.class); @Test -- 2.39.5