diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-03-14 11:28:22 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-18 20:02:57 +0000 |
commit | ecf844b39473fe21acaa1f832d244a4cc4c9f811 (patch) | |
tree | a03e87cb743b877be05c92774e6f009bad3932f5 /sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java | |
parent | 1269984e8e09338c057d068d715ade7df5a0c354 (diff) | |
download | sonarqube-ecf844b39473fe21acaa1f832d244a4cc4c9f811.tar.gz sonarqube-ecf844b39473fe21acaa1f832d244a4cc4c9f811.zip |
SONAR-16097 Add plugin cache to the Sensor API
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java new file mode 100644 index 00000000000..420a5d5f852 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java @@ -0,0 +1,87 @@ +/* + * 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.sonar.scanner.cache; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.Optional; +import java.util.zip.InflaterInputStream; +import org.sonar.api.scanner.fs.InputProject; +import org.sonar.core.util.Protobuf; +import org.sonar.scanner.bootstrap.DefaultScannerWsClient; +import org.sonar.scanner.protocol.internal.ScannerInternal; +import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonar.scanner.scan.branch.BranchType; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsResponse; + +/** + * Loads plugin cache into the local storage + */ +public class AnalysisCacheLoader { + static final String CONTENT_ENCODING = "Content-Encoding"; + static final String ACCEPT_ENCODING = "Accept-Encoding"; + private static final String URL = "api/scanner_cache/get"; + + private final DefaultScannerWsClient wsClient; + private final InputProject project; + private final BranchConfiguration branchConfiguration; + + public AnalysisCacheLoader(DefaultScannerWsClient wsClient, InputProject project, BranchConfiguration branchConfiguration) { + this.project = project; + this.branchConfiguration = branchConfiguration; + this.wsClient = wsClient; + } + + public Optional<AnalysisCacheMsg> load() { + String url = URL + "?project=" + project.key(); + if (branchConfiguration.branchType() == BranchType.BRANCH && branchConfiguration.branchName() != null) { + url = url + "&branch=" + branchConfiguration.branchName(); + } + + GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip"); + + try (WsResponse response = wsClient.call(request)) { + if (response.code() == HttpURLConnection.HTTP_NOT_FOUND) { + return Optional.empty(); + } + try (InputStream is = response.contentStream()) { + Optional<String> contentEncoding = response.header(CONTENT_ENCODING); + if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) { + return Optional.of(decompress(is)); + } else { + return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser())); + } + } catch (IOException e) { + throw new IllegalStateException("Failed to download cache", e); + } + } + } + + private static AnalysisCacheMsg decompress(InputStream is) { + try (InflaterInputStream iis = new InflaterInputStream(is)) { + return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser()); + } catch (IOException e) { + throw new IllegalStateException("Failed to decompress plugin cache", e); + } + } +} |