-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIacRealtimeResults.java
More file actions
98 lines (90 loc) · 3.83 KB
/
Copy pathIacRealtimeResults.java
File metadata and controls
98 lines (90 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.checkmarx.ast.iacrealtime;
import com.checkmarx.ast.realtime.RealtimeLocation;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Value;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@Value
@JsonDeserialize
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class IacRealtimeResults {
private static final Logger log = LoggerFactory.getLogger(IacRealtimeResults.class);
@JsonProperty("Results") List<Issue> results; // Normalized list (array or single object)
@JsonCreator
public IacRealtimeResults(@JsonProperty("Results") List<Issue> results) {
this.results = results == null ? Collections.emptyList() : results;
}
@Value
@JsonDeserialize
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Issue {
@JsonProperty("Title") String title;
@JsonProperty("Description") String description;
@JsonProperty("SimilarityID") String similarityId;
@JsonProperty("FilePath") String filePath;
@JsonProperty("Severity") String severity;
@JsonProperty("ExpectedValue") String expectedValue;
@JsonProperty("ActualValue") String actualValue;
@JsonProperty("Locations") List<RealtimeLocation> locations;
@JsonCreator
public Issue(@JsonProperty("Title") String title,
@JsonProperty("Description") String description,
@JsonProperty("SimilarityID") String similarityId,
@JsonProperty("FilePath") String filePath,
@JsonProperty("Severity") String severity,
@JsonProperty("ExpectedValue") String expectedValue,
@JsonProperty("ActualValue") String actualValue,
@JsonProperty("Locations") List<RealtimeLocation> locations) {
this.title = title;
this.description = description;
this.similarityId = similarityId;
this.filePath = filePath;
this.severity = severity;
this.expectedValue = expectedValue;
this.actualValue = actualValue;
this.locations = locations == null ? Collections.emptyList() : locations;
}
}
public static IacRealtimeResults fromLine(String line) {
if (StringUtils.isBlank(line)) {
return null;
}
try {
if (!isValidJSON(line)) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
String trimmed = line.trim();
if (trimmed.startsWith("[")) {
List<Issue> list = mapper.readValue(trimmed, mapper.getTypeFactory().constructCollectionType(List.class, Issue.class));
return new IacRealtimeResults(list == null ? Collections.emptyList() : list);
}
if (trimmed.startsWith("{")) {
Issue single = mapper.readValue(trimmed, Issue.class);
return new IacRealtimeResults(Collections.singletonList(single));
}
} catch (IOException e) {
log.debug("Failed to parse iac realtime JSON line: {}", line, e);
}
return null;
}
private static boolean isValidJSON(String json) {
try {
new ObjectMapper().readTree(json);
return true;
} catch (IOException e) {
return false;
}
}
}