aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-02 09:23:08 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-02 12:23:48 +0100
commit050021cd081cf7f0f34d6fb54f30a217e6dd6c14 (patch)
treec5fa83a99a13152f7b54816f2eb8472e6861ab87
parent187b578649d987229905533bc7496c6ab3e6785b (diff)
downloadsonarqube-050021cd081cf7f0f34d6fb54f30a217e6dd6c14.tar.gz
sonarqube-050021cd081cf7f0f34d6fb54f30a217e6dd6c14.zip
SONAR-8460 Do not fail to write response when profile not found
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java23
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java3
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