diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-09-15 16:18:01 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-09-20 20:03:30 +0000 |
commit | a8fdb0fc9d296db66ddd5cf14c88023e9bd9a364 (patch) | |
tree | 9439c50824f6d7bcd5de3451841bfeb448e13d3c | |
parent | 1731498e4f43bee8fc3c57ea58b1985db352baea (diff) | |
download | sonarqube-a8fdb0fc9d296db66ddd5cf14c88023e9bd9a364.tar.gz sonarqube-a8fdb0fc9d296db66ddd5cf14c88023e9bd9a364.zip |
SONAR-17337 Scanner download of analyzer cache doesn't handle compression correctly
8 files changed, 186 insertions, 13 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java index b714f785f6c..08a232ae765 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java @@ -41,12 +41,10 @@ import static org.sonar.db.permission.GlobalPermission.SCAN; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; -import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001; public class GetAction implements AnalysisCacheWsAction { private static final String PROJECT = "project"; private static final String BRANCH = "branch"; - private static final String PR = "pullRequest"; private final DbClient dbClient; private final UserSession userSession; @@ -111,13 +109,11 @@ public class GetAction implements AnalysisCacheWsAction { } private static boolean requestedCompressedData(Request request) { - String encoding = request.getHeaders().get("Accept-Encoding"); - if (encoding == null) { - return false; - } - return Arrays.stream(encoding.split(",")) - .map(String::trim) - .anyMatch("gzip"::equals); + return request.header("Accept-Encoding") + .map(encoding -> Arrays.stream(encoding.split(",")) + .map(String::trim) + .anyMatch("gzip"::equals)) + .orElse(false); } private void checkPermission(ComponentDto project) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java index 1a308f48ce0..319e444026d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java @@ -22,6 +22,7 @@ package org.sonar.scanner.cache; import java.io.IOException; import java.io.InputStream; import java.util.Optional; +import java.util.zip.GZIPInputStream; import java.util.zip.InflaterInputStream; import org.sonar.api.scanner.fs.InputProject; import org.sonar.api.utils.MessageException; @@ -94,8 +95,8 @@ public class DefaultAnalysisCacheLoader implements AnalysisCacheLoader { } private static AnalysisCacheMsg decompress(InputStream is) { - try (InflaterInputStream iis = new InflaterInputStream(is)) { - return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser()); + try (GZIPInputStream gzipInputStream = new GZIPInputStream(is)) { + return Protobuf.read(gzipInputStream, ScannerInternal.AnalysisCacheMsg.parser()); } catch (IOException e) { throw new IllegalStateException("Failed to decompress analysis cache", e); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java index 4f666a58a53..3147eadbb8c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java @@ -27,6 +27,8 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.zip.DeflaterInputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -130,7 +132,7 @@ public class DefaultAnalysisCacheLoaderTest { } private void setCompressedResponse(AnalysisCacheMsg msg) throws IOException { - when(response.contentStream()).thenReturn(new DeflaterInputStream(createInputStream(msg))); + when(response.contentStream()).thenReturn(createCompressedInputStream(msg)); when(response.header(CONTENT_ENCODING)).thenReturn(Optional.of("gzip")); } @@ -144,4 +146,12 @@ public class DefaultAnalysisCacheLoaderTest { analysisCacheMsg.writeTo(serialized); return new ByteArrayInputStream(serialized.toByteArray()); } + + private InputStream createCompressedInputStream(AnalysisCacheMsg analysisCacheMsg) throws IOException { + ByteArrayOutputStream serialized = new ByteArrayOutputStream(analysisCacheMsg.getSerializedSize()); + GZIPOutputStream compressed = new GZIPOutputStream(serialized); + analysisCacheMsg.writeTo(compressed); + compressed.close(); + return new ByteArrayInputStream(serialized.toByteArray()); + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java index 9b7b205059c..20de62dceff 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java @@ -22,6 +22,7 @@ package org.sonarqube.ws.client; import javax.annotation.Generated; import org.sonarqube.ws.client.almintegrations.AlmIntegrationsService; import org.sonarqube.ws.client.almsettings.AlmSettingsService; +import org.sonarqube.ws.client.analysiscache.AnalysisCacheService; import org.sonarqube.ws.client.analysisreports.AnalysisReportsService; import org.sonarqube.ws.client.applications.ApplicationsService; import org.sonarqube.ws.client.authentication.AuthenticationService; @@ -89,6 +90,7 @@ class DefaultWsClient implements WsClient { private final AlmIntegrationsService almIntegrationsService; private final AlmSettingsService almSettingsService; + private final AnalysisCacheService analysisCacheService; private final AnalysisReportsService analysisReportsService; private final ApplicationsService applicationsService; private final AuthenticationService authenticationService; @@ -148,6 +150,7 @@ class DefaultWsClient implements WsClient { this.almIntegrationsService = new AlmIntegrationsService(wsConnector); this.almSettingsService = new AlmSettingsService(wsConnector); + this.analysisCacheService = new AnalysisCacheService(wsConnector); this.analysisReportsService = new AnalysisReportsService(wsConnector); this.applicationsService = new ApplicationsService(wsConnector); this.authenticationService = new AuthenticationService(wsConnector); @@ -220,6 +223,11 @@ class DefaultWsClient implements WsClient { } @Override + public AnalysisCacheService analysisCache() { + return analysisCacheService; + } + + @Override public AnalysisReportsService analysisReports() { return analysisReportsService; } @@ -253,7 +261,7 @@ class DefaultWsClient implements WsClient { public RegulatoryReportsService regulatoryReports() { return regulatoryReportsService; } - + @Override public DuplicationsService duplications() { return duplicationsService; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java index c70da8022ae..07cc79baef8 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java @@ -22,6 +22,7 @@ package org.sonarqube.ws.client; import javax.annotation.Generated; import org.sonarqube.ws.client.almintegrations.AlmIntegrationsService; import org.sonarqube.ws.client.almsettings.AlmSettingsService; +import org.sonarqube.ws.client.analysiscache.AnalysisCacheService; import org.sonarqube.ws.client.analysisreports.AnalysisReportsService; import org.sonarqube.ws.client.applications.ApplicationsService; import org.sonarqube.ws.client.authentication.AuthenticationService; @@ -103,6 +104,8 @@ public interface WsClient { AlmSettingsService almSettings(); + AnalysisCacheService analysisCache(); + AnalysisReportsService analysisReports(); ApplicationsService applications(); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/AnalysisCacheService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/AnalysisCacheService.java new file mode 100644 index 00000000000..05ff43152eb --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/AnalysisCacheService.java @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.sonarqube.ws.client.analysiscache; + +import java.io.InputStream; +import javax.annotation.Generated; +import org.sonarqube.ws.MediaTypes; +import org.sonarqube.ws.client.BaseService; +import org.sonarqube.ws.client.PostRequest; +import org.sonarqube.ws.client.WsConnector; + +/** + * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/analysis_cache">Further information about this web service online</a> + */ +@Generated("sonar-ws-generator") +public class AnalysisCacheService extends BaseService { + + public AnalysisCacheService(WsConnector wsConnector) { + super(wsConnector, "api/analysis_cache"); + } + + /** + * This is part of the internal API. + * This is a POST request. + * + * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/analysis_cache/clear">Further information about this action online (including a response example)</a> + * @since 9.4 + */ + public void clear() { + call( + new PostRequest(path("clear")) + .setMediaType(MediaTypes.JSON) + ).content(); + } + + /** + * This is part of the internal API. + * This is a GET request. + * + * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/analysis_cache/get">Further information about this action online (including a response example)</a> + * @since 9.4 + */ + public InputStream get(GetRequest request) { + return call( + new org.sonarqube.ws.client.GetRequest(path("get")) + .setParam("branch", request.getBranch()) + .setParam("project", request.getProject()) + .setMediaType(MediaTypes.JSON) + ).contentStream(); + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/GetRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/GetRequest.java new file mode 100644 index 00000000000..d4b7b73e862 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/GetRequest.java @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.sonarqube.ws.client.analysiscache; + +import java.util.List; +import javax.annotation.Generated; + +/** + * This is part of the internal API. + * This is a POST request. + * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/analysis_cache/get">Further information about this action online (including a response example)</a> + * @since 9.4 + */ +@Generated("sonar-ws-generator") +public class GetRequest { + + private String branch; + private String project; + + /** + * Example value: "feature/my_branch" + */ + public GetRequest setBranch(String branch) { + this.branch = branch; + return this; + } + + public String getBranch() { + return branch; + } + + /** + * This is a mandatory parameter. + * Example value: "my_project" + */ + public GetRequest setProject(String project) { + this.project = project; + return this; + } + + public String getProject() { + return project; + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/package-info.java new file mode 100644 index 00000000000..dc513b11a5c --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/analysiscache/package-info.java @@ -0,0 +1,26 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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. + */ +@ParametersAreNonnullByDefault +@Generated("sonar-ws-generator") +package org.sonarqube.ws.client.analysiscache; + +import javax.annotation.ParametersAreNonnullByDefault; +import javax.annotation.Generated; + |