|
8 | 8 | import com.fasterxml.jackson.core.type.TypeReference;
|
9 | 9 | import com.fasterxml.jackson.databind.ObjectMapper;
|
10 | 10 | import org.apache.commons.lang3.StringUtils;
|
| 11 | +import org.apache.commons.lang3.reflect.FieldUtils; |
11 | 12 | import org.slf4j.Logger;
|
12 | 13 | import org.slf4j.LoggerFactory;
|
13 | 14 |
|
14 | 15 | import java.io.*;
|
15 | 16 | import java.net.URI;
|
16 | 17 | import java.net.URISyntaxException;
|
17 | 18 | import java.net.URL;
|
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
18 | 23 | import java.security.CodeSource;
|
19 | 24 | import java.security.ProtectionDomain;
|
20 | 25 | import java.util.ArrayList;
|
@@ -185,43 +190,57 @@ public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedExce
|
185 | 190 | return scanObject;
|
186 | 191 | }
|
187 | 192 |
|
188 |
| - public String cxGetResultsSummary(String scanID, String formatType, String target) |
| 193 | + public String cxGetResultsSummary(String scanID) throws IOException { |
| 194 | + return runResultExecutionCommands(scanID, "summaryHTML", ".html"); |
| 195 | + } |
| 196 | + |
| 197 | + public String cxGetResultsList(String scanID) throws IOException { |
| 198 | + return runResultExecutionCommands(scanID, "json", ".json"); |
| 199 | + } |
| 200 | + |
| 201 | + public void cxGetResults(String resultType, String scanID, String fileName, String target) |
189 | 202 | throws IOException {
|
190 |
| - List<String> commands = initialCommandsCommon(); |
191 |
| - commands.add("result"); |
192 |
| - commands.add("summary"); |
193 |
| - if (scanID.isEmpty()) { |
| 203 | + |
| 204 | + List<String> commands = buildResultCommand(resultType, scanID, fileName, target); |
| 205 | + |
| 206 | + runResultExecutionCommands(commands); |
| 207 | + } |
| 208 | + |
| 209 | + private List<String> buildResultCommand(String resultType, String scanId, String outputName, String outputTarget) { |
| 210 | + if (scanId.isEmpty()) { |
194 | 211 | throw new CxException("Please provide the scan id ");
|
195 | 212 | }
|
196 |
| - commands.add("--scan-id"); |
197 |
| - commands.add(scanID); |
198 |
| - if (!formatType.isEmpty()) { |
199 |
| - commands.add("--format"); |
200 |
| - commands.add(formatType); |
201 |
| - } |
202 |
| - if (!target.isEmpty()) { |
203 |
| - commands.add("--target"); |
204 |
| - commands.add(target); |
205 |
| - } |
206 |
| - return runResultExecutionCommands(commands); |
207 |
| - } |
208 | 213 |
|
209 |
| - public String cxGetResultsList(String scanID, String formatType) |
210 |
| - throws IOException { |
211 | 214 | List<String> commands = initialCommandsCommon();
|
212 | 215 | commands.add("result");
|
213 |
| - commands.add("list"); |
214 |
| - if (scanID.isEmpty()) { |
215 |
| - throw new CxException("Please provide the scan id "); |
216 |
| - } |
217 | 216 | commands.add("--scan-id");
|
218 |
| - commands.add(scanID); |
219 |
| - if (!formatType.isEmpty()) { |
220 |
| - commands.add("--format"); |
221 |
| - commands.add(formatType); |
| 217 | + commands.add(scanId); |
| 218 | + commands.add("--report-format"); |
| 219 | + commands.add(resultType); |
| 220 | + |
| 221 | + if (StringUtils.isNotBlank(outputName)) { |
| 222 | + commands.add("--output-name"); |
| 223 | + commands.add(outputName); |
| 224 | + } |
| 225 | + |
| 226 | + if (StringUtils.isNotBlank(outputTarget)) { |
| 227 | + commands.add("--output-path"); |
| 228 | + commands.add(outputTarget); |
222 | 229 | }
|
223 |
| - |
224 |
| - return runResultExecutionCommands(commands); |
| 230 | + |
| 231 | + return commands; |
| 232 | + } |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | + private String runResultExecutionCommands(String scanId, String resultType, String extension) throws IOException { |
| 237 | + Path tempDir = Files.createTempDirectory("cx"); |
| 238 | + String fileName = Long.toString(System.nanoTime()); |
| 239 | + List<String> commands = buildResultCommand(resultType, scanId, fileName, tempDir.toAbsolutePath().toString()); |
| 240 | + runResultExecutionCommands(commands); |
| 241 | + |
| 242 | + File outputFile = new File(tempDir.toAbsolutePath().toString(), fileName + extension); |
| 243 | + return new String(Files.readAllBytes(Paths.get(outputFile.getAbsolutePath())), StandardCharsets.UTF_8); |
225 | 244 | }
|
226 | 245 |
|
227 | 246 | private String runResultExecutionCommands(List<String> commands) throws IOException {
|
|
0 commit comments