1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.hack23.sonar.cloudformation.reports.process;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.io.IOUtils;
28 import org.sonar.api.utils.log.Logger;
29 import org.sonar.api.utils.log.Loggers;
30
31 import com.fasterxml.jackson.core.type.TypeReference;
32 import com.fasterxml.jackson.databind.DeserializationFeature;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.json.JsonMapper;
35 import com.hack23.sonar.cloudformation.reports.checkov.CheckovReport;
36
37
38
39
40 public class CheckovReportReader {
41
42
43 private static final Logger LOGGER = Loggers.get(CheckovReportReader.class);
44
45
46
47
48
49
50
51 public List<CheckovReport> readReport(final InputStream input) {
52 final ObjectMapper objectMapper = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build();
53 String report = null;
54 try {
55 report = new String(IOUtils.toByteArray(input));
56
57 return objectMapper.readValue(report, new TypeReference<List<CheckovReport>>() { });
58 } catch (final IOException e) {
59
60 try {
61 final CheckovReport checkovReport = objectMapper.readValue(report, CheckovReport.class);
62 final List<CheckovReport> reportList = new ArrayList<>();
63 reportList.add(checkovReport);
64 return reportList;
65 } catch (final IOException e2) {
66 LOGGER.warn("Problem reading checkov report json:{}", e2.getMessage());
67 return new ArrayList<>();
68 }
69 }
70 }
71 }