Search in sources :

Example 1 with Stream

use of org.sonar.api.server.ws.Response.Stream in project sonarqube by SonarSource.

the class ExportAction method define.

@Override
public void define(WebService.NewController controller) {
    NewAction action = controller.createAction("export").setSince("5.2").setDescription("Export a quality profile.").setResponseExample(getClass().getResource("export-example.xml")).setHandler(this);
    action.createParam(PARAM_PROFILE_NAME).setDescription("The name of the quality profile to export. If left empty, will export the default profile for the language.").setExampleValue("My Sonar way");
    action.createParam(PARAM_LANGUAGE).setDescription("The language for the quality profile.").setExampleValue(LanguageParamUtils.getExampleValue(languages)).setPossibleValues(LanguageParamUtils.getLanguageKeys(languages)).setRequired(true);
    Set<String> exporterKeys = Arrays.stream(languages.all()).map(language -> exporters.exportersForLanguage(language.getKey())).flatMap(Collection::stream).map(ProfileExporter::getKey).collect(Collectors.toSet());
    if (!exporterKeys.isEmpty()) {
        action.createParam(PARAM_FORMAT).setDescription("Output format. If left empty, the same format as api/qualityprofiles/backup is used. " + "Possible values are described by api/qualityprofiles/exporters.").setPossibleValues(exporterKeys).setDeprecatedKey("format", "6.3");
    }
}
Also used : Arrays(java.util.Arrays) QProfileExporters(org.sonar.server.qualityprofile.QProfileExporters) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Languages(org.sonar.api.resources.Languages) WebService(org.sonar.api.server.ws.WebService) WsUtils.checkFound(org.sonar.server.ws.WsUtils.checkFound) Response(org.sonar.api.server.ws.Response) OutputStreamWriter(java.io.OutputStreamWriter) ProfileExporter(org.sonar.api.profiles.ProfileExporter) MediaTypes(org.sonarqube.ws.MediaTypes) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) Collection(java.util.Collection) Stream(org.sonar.api.server.ws.Response.Stream) LanguageParamUtils(org.sonar.server.util.LanguageParamUtils) Set(java.util.Set) Collectors(org.sonar.core.util.stream.Collectors) IOException(java.io.IOException) NewAction(org.sonar.api.server.ws.WebService.NewAction) StandardCharsets(java.nio.charset.StandardCharsets) DbClient(org.sonar.db.DbClient) Writer(java.io.Writer) QProfileBackuper(org.sonar.server.qualityprofile.QProfileBackuper) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Collection(java.util.Collection) NewAction(org.sonar.api.server.ws.WebService.NewAction)

Example 2 with Stream

use of org.sonar.api.server.ws.Response.Stream in project sonarqube by SonarSource.

the class BackupAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    Stream stream = response.stream();
    stream.setMediaType(MediaTypes.XML);
    try (OutputStreamWriter writer = new OutputStreamWriter(stream.output(), StandardCharsets.UTF_8);
        DbSession dbSession = dbClient.openSession(false)) {
        QualityProfileDto profile = profileFactory.find(dbSession, QProfileRef.from(request));
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s.xml", profile.getKee()));
        backuper.backup(profile.getKee(), writer);
    }
}
Also used : DbSession(org.sonar.db.DbSession) Stream(org.sonar.api.server.ws.Response.Stream) OutputStreamWriter(java.io.OutputStreamWriter) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 3 with Stream

use of org.sonar.api.server.ws.Response.Stream in project sonarqube by SonarSource.

the class ExportAction method writeResponse.

private void writeResponse(QualityProfileDto profile, @Nullable String exporterKey, Response response) throws IOException {
    Stream stream = response.stream();
    try (DbSession dbSession = dbClient.openSession(false);
        OutputStream output = response.stream().output();
        Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
        if (exporterKey == null) {
            stream.setMediaType(MediaTypes.XML);
            backuper.backup(dbSession, profile, writer);
        } else {
            stream.setMediaType(exporters.mimeType(exporterKey));
            exporters.export(profile, exporterKey, writer);
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) OutputStream(java.io.OutputStream) OutputStream(java.io.OutputStream) Stream(org.sonar.api.server.ws.Response.Stream) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)3 Stream (org.sonar.api.server.ws.Response.Stream)3 DbSession (org.sonar.db.DbSession)3 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)2 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 ProfileExporter (org.sonar.api.profiles.ProfileExporter)1 Languages (org.sonar.api.resources.Languages)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 WebService (org.sonar.api.server.ws.WebService)1 NewAction (org.sonar.api.server.ws.WebService.NewAction)1 Collectors (org.sonar.core.util.stream.Collectors)1 DbClient (org.sonar.db.DbClient)1