|
| 1 | +package com.checkmarx.ast.learnMore; |
| 2 | + |
| 3 | +import com.checkmarx.ast.codebashing.CodeBashing; |
| 4 | +import com.checkmarx.ast.remediation.KicsRemediation; |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 7 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 12 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 13 | +import lombok.Value; |
| 14 | +import org.apache.commons.lang3.StringUtils; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +@Value |
| 20 | +@JsonDeserialize() |
| 21 | +@JsonInclude(JsonInclude.Include.NON_NULL) |
| 22 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 23 | + |
| 24 | +public class LearnMore { |
| 25 | + |
| 26 | + String queryId; |
| 27 | + String queryName; |
| 28 | + String queryDescriptionId; |
| 29 | + String resultDescription; |
| 30 | + String risk; |
| 31 | + String cause; |
| 32 | + String generalRecommendations; |
| 33 | + List<Sample> samples; |
| 34 | + |
| 35 | + @JsonCreator |
| 36 | + public LearnMore(@JsonProperty("queryID") String queryId, @JsonProperty("queryName") String queryName,@JsonProperty("queryDescriptionID") String queryDescriptionId, @JsonProperty("resultDescription") String resultDescription,@JsonProperty("risk") String risk,@JsonProperty("cause") String cause,@JsonProperty("generalRecommendations") String generalRecommendations,@JsonProperty("samples") List<Sample> samples) { |
| 37 | + this.queryId = queryId; |
| 38 | + this.queryName = queryName; |
| 39 | + this.queryDescriptionId = queryDescriptionId; |
| 40 | + this.resultDescription = resultDescription; |
| 41 | + this.risk = risk; |
| 42 | + this.cause = cause; |
| 43 | + this.generalRecommendations = generalRecommendations; |
| 44 | + this.samples = samples; |
| 45 | + } |
| 46 | + |
| 47 | + public static <T> List<T> listFromLine(String line) { |
| 48 | + return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, LearnMore.class)); |
| 49 | + } |
| 50 | + |
| 51 | + public static <T> T fromLine(String line) { |
| 52 | + return parse(line, TypeFactory.defaultInstance().constructType(LearnMore.class)); |
| 53 | + } |
| 54 | + |
| 55 | + private static <T> T parse(String line, JavaType type) { |
| 56 | + T result = null; |
| 57 | + try { |
| 58 | + if (!StringUtils.isBlank(line) && isValidJSON(line)) { |
| 59 | + result = new ObjectMapper().readValue(line, type); |
| 60 | + |
| 61 | + } |
| 62 | + } catch (IOException e) { |
| 63 | + e.printStackTrace(); |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private static boolean isValidJSON(final String json) { |
| 69 | + try { |
| 70 | + final ObjectMapper mapper = new ObjectMapper(); |
| 71 | + mapper.readTree(json); |
| 72 | + return true; |
| 73 | + } catch (IOException e) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments