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.

SarifSerializerImpl.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.core.sarif;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.gson.Gson;
  23. import com.google.gson.JsonIOException;
  24. import com.google.gson.JsonSyntaxException;
  25. import java.io.IOException;
  26. import java.io.Reader;
  27. import java.nio.file.NoSuchFileException;
  28. import java.nio.file.Path;
  29. import javax.inject.Inject;
  30. import org.sonar.api.ce.ComputeEngineSide;
  31. import org.sonar.api.scanner.ScannerSide;
  32. import static java.lang.String.format;
  33. import static java.nio.charset.StandardCharsets.UTF_8;
  34. import static java.nio.file.Files.newBufferedReader;
  35. @ScannerSide
  36. @ComputeEngineSide
  37. public class SarifSerializerImpl implements SarifSerializer {
  38. private static final String SARIF_REPORT_ERROR = "Failed to read SARIF report at '%s'";
  39. private static final String SARIF_JSON_SYNTAX_ERROR = SARIF_REPORT_ERROR + ": invalid JSON syntax or file is not UTF-8 encoded";
  40. private final Gson gson;
  41. @Inject
  42. public SarifSerializerImpl() {
  43. this(new Gson());
  44. }
  45. @VisibleForTesting
  46. SarifSerializerImpl(Gson gson) {
  47. this.gson = gson;
  48. }
  49. @Override
  50. public String serialize(Sarif210 sarif210) {
  51. return gson.toJson(sarif210);
  52. }
  53. @Override
  54. public Sarif210 deserialize(Path reportPath) throws NoSuchFileException {
  55. try (Reader reader = newBufferedReader(reportPath, UTF_8)) {
  56. Sarif210 sarif = gson.fromJson(reader, Sarif210.class);
  57. SarifVersionValidator.validateSarifVersion(sarif.getVersion());
  58. return sarif;
  59. } catch (NoSuchFileException e) {
  60. throw e;
  61. } catch (JsonIOException | IOException e) {
  62. throw new IllegalStateException(format(SARIF_REPORT_ERROR, reportPath), e);
  63. } catch (JsonSyntaxException e) {
  64. throw new IllegalStateException(format(SARIF_JSON_SYNTAX_ERROR, reportPath), e);
  65. }
  66. }
  67. }