You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultAnalysisCacheLoader.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.cache;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Optional;
  24. import java.util.zip.InflaterInputStream;
  25. import org.sonar.api.scanner.fs.InputProject;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.core.util.Protobuf;
  28. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  29. import org.sonar.scanner.protocol.internal.ScannerInternal;
  30. import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg;
  31. import org.sonar.scanner.scan.branch.BranchConfiguration;
  32. import org.sonarqube.ws.client.GetRequest;
  33. import org.sonarqube.ws.client.HttpException;
  34. import org.sonarqube.ws.client.WsResponse;
  35. /**
  36. * Loads plugin cache into the local storage
  37. */
  38. public class DefaultAnalysisCacheLoader implements AnalysisCacheLoader {
  39. static final String CONTENT_ENCODING = "Content-Encoding";
  40. static final String ACCEPT_ENCODING = "Accept-Encoding";
  41. private static final String URL = "api/analysis_cache/get";
  42. private final DefaultScannerWsClient wsClient;
  43. private final InputProject project;
  44. private final BranchConfiguration branchConfiguration;
  45. public DefaultAnalysisCacheLoader(DefaultScannerWsClient wsClient, InputProject project, BranchConfiguration branchConfiguration) {
  46. this.project = project;
  47. this.branchConfiguration = branchConfiguration;
  48. this.wsClient = wsClient;
  49. }
  50. @Override public Optional<AnalysisCacheMsg> load() {
  51. String url = URL + "?project=" + project.key();
  52. if (branchConfiguration.referenceBranchName() != null) {
  53. url = url + "&branch=" + branchConfiguration.referenceBranchName();
  54. }
  55. GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip");
  56. try (WsResponse response = wsClient.call(request); InputStream is = response.contentStream()) {
  57. Optional<String> contentEncoding = response.header(CONTENT_ENCODING);
  58. if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) {
  59. return Optional.of(decompress(is));
  60. } else {
  61. return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser()));
  62. }
  63. } catch (HttpException e) {
  64. if (e.code() == 404) {
  65. return Optional.empty();
  66. }
  67. throw MessageException.of("Failed to download analysis cache: " + DefaultScannerWsClient.createErrorMessage(e));
  68. } catch (Exception e) {
  69. throw new IllegalStateException("Failed to download analysis cache", e);
  70. }
  71. }
  72. private static AnalysisCacheMsg decompress(InputStream is) {
  73. try (InflaterInputStream iis = new InflaterInputStream(is)) {
  74. return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser());
  75. } catch (IOException e) {
  76. throw new IllegalStateException("Failed to decompress analysis cache", e);
  77. }
  78. }
  79. }