Search in sources :

Example 1 with ListProcessingReport

use of com.github.fge.jsonschema.core.report.ListProcessingReport in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceImpl method validatePolicyConfiguration.

private String validatePolicyConfiguration(String policyName, String configuration) {
    if (policyName != null && configuration != null) {
        String schema = getSchema(policyName);
        try {
            // At least, validate json.
            String safePolicyConfiguration = clearNullValues(configuration);
            JsonNode jsonConfiguration = JsonLoader.fromString(safePolicyConfiguration);
            if (schema != null && !schema.equals("")) {
                // Validate json against schema when defined.
                JsonNode jsonSchema = JsonLoader.fromString(schema);
                ListProcessingReport report = (ListProcessingReport) jsonSchemaFactory.getValidator().validate(jsonSchema, jsonConfiguration, true);
                if (!report.isSuccess()) {
                    boolean hasDefaultValue = false;
                    String msg = "";
                    if (report.iterator().hasNext()) {
                        msg = " : " + report.iterator().next().getMessage();
                        Pattern pattern = Pattern.compile("\\(\\[\\\"(.*?)\\\"\\]\\)");
                        Matcher matcher = pattern.matcher(msg);
                        if (matcher.find()) {
                            String field = matcher.group(1);
                            JsonNode properties = jsonSchema.get("properties");
                            hasDefaultValue = properties != null && properties.get(field) != null && properties.get(field).get("default") != null;
                        }
                    }
                    if (!hasDefaultValue) {
                        throw new InvalidDataException("Invalid policy configuration" + msg);
                    }
                }
            }
            return safePolicyConfiguration;
        } catch (IOException | ProcessingException e) {
            throw new InvalidDataException("Unable to validate policy configuration", e);
        }
    }
    return configuration;
}
Also used : ListProcessingReport(com.github.fge.jsonschema.core.report.ListProcessingReport) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ListProcessingReport (com.github.fge.jsonschema.core.report.ListProcessingReport)1 InvalidDataException (io.gravitee.rest.api.service.exceptions.InvalidDataException)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1