Search in sources :

Example 1 with ValidationFailure

use of org.apache.sling.validation.ValidationFailure in project sling by apache.

the class ModifyUserServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    ValueMap requestParameters = request.adaptTo(ValueMap.class);
    String[] resourceTypeValues = requestParameters.get("sling:resourceType", String[].class);
    String resourceType = null;
    if (resourceTypeValues != null && resourceTypeValues.length > 0) {
        resourceType = resourceTypeValues[0];
    }
    if (resourceType != null && !"".equals(resourceType)) {
        String resourcePath = request.getRequestPathInfo().getResourcePath();
        ValidationModel vm = validationService.getValidationModel(resourceType, resourcePath, false);
        if (vm != null) {
            ValidationResult vr = validationService.validate(requestParameters, vm);
            if (vr.isValid()) {
                RequestDispatcherOptions options = new RequestDispatcherOptions();
                options.setReplaceSelectors(" ");
                request.getRequestDispatcher(request.getResource(), options).forward(request, response);
            } else {
                response.setContentType("application/json");
                JSONWriter writer = new JSONWriter(response.getWriter());
                writer.object();
                writer.key("success").value(false);
                writer.key("failures").array();
                for (ValidationFailure failure : vr.getFailures()) {
                    writer.object();
                    writer.key("message").value(failure.getMessage(request.getResourceBundle(Locale.US)));
                    writer.key("location").value(failure.getLocation());
                    writer.endObject();
                }
                writer.endArray();
                writer.endObject();
                response.setStatus(400);
            }
        }
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ValidationModel(org.apache.sling.validation.model.ValidationModel) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) ValueMap(org.apache.sling.api.resource.ValueMap) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 2 with ValidationFailure

use of org.apache.sling.validation.ValidationFailure in project sling by apache.

the class ValidationPostResponse method doSend.

@Override
protected void doSend(HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter printWriter = response.getWriter();
    JSONWriter writer = new JSONWriter(printWriter);
    writer.object();
    boolean validationError = false;
    if (validationResult != null) {
        try {
            writer.key("valid").value(validationResult.isValid());
            writer.key("failures").array();
            for (ValidationFailure failure : validationResult.getFailures()) {
                writer.object();
                writer.key("message").value(failure.getMessage(resourceBundle));
                writer.key("location").value(failure.getLocation());
                writer.key("severity").value(failure.getSeverity());
                writer.endObject();
            }
            writer.endArray();
        } catch (IOException e) {
            LOG.error("JSON error during response send operation.", e);
        }
    } else {
        validationError = true;
    }
    writer.endObject();
    if (validationError) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 3 with ValidationFailure

use of org.apache.sling.validation.ValidationFailure in project sling by apache.

the class InvalidResourceException method getMessage.

public String getMessage() {
    StringBuilder builder = new StringBuilder("Validation errors for ");
    builder.append("'" + path + "':");
    for (ValidationFailure failure : result.getFailures()) {
        builder.append("\n" + failure.getLocation() + ":" + failure.getMessage(null) + "\n\t");
    }
    return builder.toString();
}
Also used : ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 4 with ValidationFailure

use of org.apache.sling.validation.ValidationFailure in project sling by apache.

the class InvalidResourcePostProcessorException method getMessage.

public String getMessage() {
    StringBuilder builder = new StringBuilder();
    boolean isFirst = true;
    for (ValidationFailure failure : result.getFailures()) {
        if (isFirst) {
            isFirst = false;
        } else {
            builder.append(", ");
        }
        if (StringUtils.isNotEmpty(failure.getLocation())) {
            builder.append(failure.getLocation() + " : ");
        }
        builder.append(failure.getMessage(resourceBundle));
    }
    return MessageFormat.format(resourceBundle.getString(KEY_MESSAGE), builder.toString());
}
Also used : ValidationFailure(org.apache.sling.validation.ValidationFailure)

Aggregations

ValidationFailure (org.apache.sling.validation.ValidationFailure)4 JSONWriter (org.apache.felix.utils.json.JSONWriter)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ValidationResult (org.apache.sling.validation.ValidationResult)1 ValidationModel (org.apache.sling.validation.model.ValidationModel)1