|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
22 | 22 | import java.util.Set;
|
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
23 | 25 |
|
| 26 | +import javax.xml.bind.ValidationException; |
24 | 27 | import org.apache.commons.lang3.StringUtils;
|
25 | 28 | import org.slf4j.Logger;
|
26 | 29 | import org.slf4j.LoggerFactory;
|
|
33 | 36 | import com.github.fge.jsonschema.core.report.ProcessingReport;
|
34 | 37 | import com.github.fge.jsonschema.main.JsonSchema;
|
35 | 38 | import com.github.fge.jsonschema.main.JsonSchemaFactory;
|
36 |
| -import com.google.gson.Gson; |
37 |
| -import com.google.gson.GsonBuilder; |
38 | 39 | import com.google.gson.JsonArray;
|
39 | 40 | import com.google.gson.JsonElement;
|
40 | 41 | import com.google.gson.JsonObject;
|
41 | 42 | import com.google.gson.JsonParser;
|
| 43 | +import com.jayway.jsonpath.DocumentContext; |
| 44 | +import com.jayway.jsonpath.JsonPath; |
42 | 45 |
|
43 | 46 | public class EiffelValidator {
|
44 |
| - public static final Logger log = LoggerFactory.getLogger(EiffelValidator.class); |
45 | 47 |
|
| 48 | + private static final String REGEX_PATH = "\\/\\d*"; |
| 49 | + private static final String CUSTOM_DATA = "customData"; |
| 50 | + private static final String SLASH = "/"; |
| 51 | + private static final String DOT = "."; |
| 52 | + private static final String REMREM_GENERATE_FAILURES = "remremGenerateFailures"; |
| 53 | + private static final String PATH = "path"; |
| 54 | + private static final String MESSAGE = "message"; |
| 55 | + private static final String TYPE = "type"; |
| 56 | + private static final String REQUIRED = "required"; |
| 57 | + private static final String POINTER = "pointer"; |
| 58 | + private static final String INSTANCE = "instance"; |
| 59 | + private static final String KEYWORD = "keyword"; |
| 60 | + public static final Logger log = LoggerFactory.getLogger(EiffelValidator.class); |
| 61 | + private static final String DATA = "data"; |
46 | 62 | private JsonSchema validationSchema;
|
47 | 63 | private String schemaResourceName;
|
48 | 64 | private String eventType;
|
@@ -73,41 +89,139 @@ public EiffelValidator(String schemaResourceName, String eventType, List<String>
|
73 | 89 | }
|
74 | 90 | }
|
75 | 91 |
|
76 |
| - public void validate(JsonObject jsonObjectInput) throws EiffelValidationException { |
| 92 | + public JsonObject validate(JsonObject jsonObjectInput) throws EiffelValidationException { |
| 93 | + return validate(jsonObjectInput, false); |
| 94 | + } |
| 95 | + |
| 96 | + public JsonObject validate(JsonObject jsonObjectInput, Boolean lenientValidation) throws EiffelValidationException { |
| 97 | + JsonArray remremGenerateFailures = new JsonArray(); |
| 98 | + log.debug("VALIDATING Schema used: {}", schemaResourceName); |
77 | 99 | try {
|
78 |
| - ProcessingReport report = validationSchema.validate(JsonLoader.fromString(jsonObjectInput.toString())); |
79 |
| - if (!report.isSuccess()) { |
80 |
| - log.warn(jsonObjectInput.toString()); |
81 |
| - throw new EiffelValidationException(getErrorsList(report)); |
| 100 | + final ProcessingReport report = validationSchema.validate(JsonLoader.fromString(jsonObjectInput.toString()), |
| 101 | + true); |
| 102 | + if (!report.isSuccess() && lenientValidation) { |
| 103 | + log.info("Trying to revalidate the Eiffel message with only mandatory Eiffel message fields."); |
| 104 | + String revalidatedJson = removeErrorProperties(report, jsonObjectInput, remremGenerateFailures); |
| 105 | + ProcessingReport report2 = validationSchema.validate(JsonLoader.fromString(revalidatedJson), true); |
| 106 | + handleErrorReport(jsonObjectInput, report2); |
| 107 | + log.debug("VALIDATED. Schema used: {}", schemaResourceName); |
| 108 | + return addRemremGenerateFailuresToCustomData(new JsonParser().parse(revalidatedJson).getAsJsonObject(), remremGenerateFailures); |
| 109 | + } else { |
| 110 | + handleErrorReport(jsonObjectInput, report); |
82 | 111 | }
|
83 | 112 | log.debug("VALIDATED. Schema used: {}", schemaResourceName);
|
| 113 | + return jsonObjectInput; |
84 | 114 | } catch (Exception e) {
|
85 |
| - String message = "Cannot validate given JSON string"; |
| 115 | + final String message = "Cannot validate given JSON string"; |
86 | 116 | log.debug(message, e);
|
87 | 117 | throw new EiffelValidationException(message, e);
|
88 | 118 | }
|
89 | 119 | }
|
90 | 120 |
|
| 121 | + private void handleErrorReport(JsonObject jsonObjectInput, ProcessingReport report) throws EiffelValidationException { |
| 122 | + if (!report.isSuccess()) { |
| 123 | + throw new EiffelValidationException(getErrorsList(report)); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * removeErrorProperties for removing the validation failures from Eiffel event and list the validation failures |
| 129 | + * @param report |
| 130 | + * @param jsonObjectInput |
| 131 | + * @param remremGenerateFailures |
| 132 | + * @return |
| 133 | + * @throws ValidationException |
| 134 | + */ |
| 135 | + private String removeErrorProperties(ProcessingReport report, JsonObject jsonObjectInput, JsonArray remremGenerateFailures) throws EiffelValidationException { |
| 136 | + JsonParser parser = new JsonParser(); |
| 137 | + DocumentContext doc = JsonPath.parse(jsonObjectInput.toString()); |
| 138 | + for (ProcessingMessage processingMessage : report) { |
| 139 | + if (LogLevel.ERROR.equals(processingMessage.getLogLevel())) { |
| 140 | + JsonElement element = parser.parse(processingMessage.asJson().toString()); |
| 141 | + if (element.getAsJsonObject().get(KEYWORD).getAsString().equals(REQUIRED) || element.getAsJsonObject().get(KEYWORD).getAsString().equals(TYPE)) { |
| 142 | + throw new EiffelValidationException(getErrorsList(report)); |
| 143 | + } |
| 144 | + String errorPath = element.getAsJsonObject().get(INSTANCE).getAsJsonObject().get(POINTER) |
| 145 | + .getAsString(); |
| 146 | + JsonObject addValidationFailurs = addValidationFailures(element, processingMessage.getMessage()); |
| 147 | + remremGenerateFailures.add(addValidationFailurs); |
| 148 | + doc.delete(getPath(errorPath)); |
| 149 | + } |
| 150 | + } |
| 151 | + return doc.jsonString(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * getPath for create the DocumentContext path from validation failure path |
| 156 | + * example |
| 157 | + * 1) meta/tags/0 --> $.meta.tags[0] |
| 158 | + * 2) links/0/target --> $.links[0] |
| 159 | + * 3) /data/outcome/conclusion --> $.data.outcome.conclusion |
| 160 | + * @param errorPath |
| 161 | + * @return path |
| 162 | + */ |
| 163 | + private String getPath(String errorPath) { |
| 164 | + final Pattern pattern = Pattern.compile(REGEX_PATH, Pattern.MULTILINE); |
| 165 | + final Matcher matcher = pattern.matcher(errorPath); |
| 166 | + while (matcher.find()) { |
| 167 | + if(matcher.group(0).length()>1) { |
| 168 | + errorPath = errorPath.substring(0, errorPath.indexOf(matcher.group(0))+matcher.group(0).length()); |
| 169 | + errorPath = errorPath.replaceAll(matcher.group(0), "["+matcher.group(0).substring(1)+"]"); |
| 170 | + } |
| 171 | + } |
| 172 | + return "$" + errorPath.replace(SLASH, DOT); |
| 173 | + } |
| 174 | + |
| 175 | + private JsonObject addValidationFailures(JsonElement element, String message) { |
| 176 | + log.debug("Adding the error field information to the array"); |
| 177 | + String type = element.getAsJsonObject().get(KEYWORD).getAsString(); |
| 178 | + String path = element.getAsJsonObject().get(INSTANCE).getAsJsonObject().get(POINTER).getAsString(); |
| 179 | + JsonObject object = new JsonObject(); |
| 180 | + object.addProperty(TYPE, type); |
| 181 | + object.addProperty(MESSAGE, message); |
| 182 | + object.addProperty(PATH, path); |
| 183 | + return object; |
| 184 | + } |
| 185 | + |
| 186 | + private JsonObject addRemremGenerateFailuresToCustomData(JsonObject inputJson, JsonArray remremGenerateFailures) { |
| 187 | + if (remremGenerateFailures.size() > 0) { |
| 188 | + JsonArray customData = getCustomData(inputJson); |
| 189 | + JsonObject object = new JsonObject(); |
| 190 | + object.addProperty("key", REMREM_GENERATE_FAILURES); |
| 191 | + object.add("value", remremGenerateFailures); |
| 192 | + customData.add(object); |
| 193 | + } |
| 194 | + return inputJson; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Gets the customData array from an Eiffel message |
| 199 | + * @param Eiffel message |
| 200 | + * @return customData array |
| 201 | + */ |
| 202 | + public JsonArray getCustomData(JsonObject json) { |
| 203 | + if (json.isJsonObject() && json.getAsJsonObject().has(DATA) |
| 204 | + && json.getAsJsonObject().getAsJsonObject(DATA).has(CUSTOM_DATA)) { |
| 205 | + return json.getAsJsonObject().getAsJsonObject(DATA).get(CUSTOM_DATA).getAsJsonArray(); |
| 206 | + } |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
91 | 210 | /**
|
92 | 211 | *
|
93 | 212 | * @param report json validation report
|
94 | 213 | * @return error message
|
95 | 214 | */
|
96 |
| - private String getErrorsList(ProcessingReport report) { |
97 |
| - Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
98 |
| - JsonParser parser = new JsonParser(); |
99 |
| - String message = ""; |
100 |
| - for (ProcessingMessage processingMessage : report) { |
101 |
| - if(LogLevel.ERROR.equals(processingMessage.getLogLevel())) { |
102 |
| - JsonElement element=parser.parse(processingMessage.asJson().toString()); |
103 |
| - element.getAsJsonObject().remove("schema"); |
104 |
| - element.getAsJsonObject().remove("level"); |
105 |
| - message = gson.toJson(element); |
106 |
| - log.debug(message); |
| 215 | + private String getErrorsList(final ProcessingReport report) { |
| 216 | + List<String> messages = new ArrayList<String>(); |
| 217 | + for (final ProcessingMessage processingMessage : report) { |
| 218 | + if (LogLevel.ERROR.equals(processingMessage.getLogLevel())) { |
| 219 | + messages.add(processingMessage.getMessage().toString()); |
107 | 220 | }
|
108 | 221 | }
|
109 |
| - return message; |
| 222 | + return messages.toString(); |
110 | 223 | }
|
| 224 | + |
111 | 225 | /**
|
112 | 226 | * This method is used to validate links in an event
|
113 | 227 | * @param JsonArray of links in an event
|
|
0 commit comments