View Javadoc
1   /*
2    * Cloudformation Plugin for SonarQube
3    * Copyright (C) 2019 James Pether Sörling
4    * james@hack23.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 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   * The Class CheckovReportReader.
39   */
40  public class CheckovReportReader {
41  
42  	/** The Constant LOGGER. */
43  	private static final Logger LOGGER = Loggers.get(CheckovReportReader.class);
44  
45  	/**
46  	 * Read report.
47  	 *
48  	 * @param input the input
49  	 * @return the list
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  }