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);
}
}
}
}
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);
}
}
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();
}
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());
}
Aggregations