Search in sources :

Example 1 with Violation

use of org.webpieces.http.exception.Violation in project webpieces by deanhiller.

the class BeanValidator method validate.

public List<Violation> validate(Object controller, Method m, List<Object> args) {
    Object[] params = args.toArray();
    List<Violation> all = new ArrayList<>();
    Set<ConstraintViolation<Object>> violations = execValidator.validateParameters(controller, m, params);
    addAll(null, all, violations);
    Parameter[] parameters = m.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        Object arg = args.get(i);
        if (arg != null) {
            Parameter param = parameters[i];
            String paramName = param.getName();
            Set<ConstraintViolation<Object>> violations2 = validator.validate(arg);
            addAll(paramName + ".", all, violations2);
        }
    }
    return all;
}
Also used : Violation(org.webpieces.http.exception.Violation) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter)

Example 2 with Violation

use of org.webpieces.http.exception.Violation in project webpieces by deanhiller.

the class BeanValidator method addAll.

private void addAll(String prefix, List<Violation> all, Set<ConstraintViolation<Object>> violations) {
    for (ConstraintViolation<Object> violation : violations) {
        String path = getPathFun(prefix, violation);
        all.add(new Violation(path, violation.getMessage()));
    }
}
Also used : Violation(org.webpieces.http.exception.Violation) ConstraintViolation(javax.validation.ConstraintViolation)

Example 3 with Violation

use of org.webpieces.http.exception.Violation in project webpieces by deanhiller.

the class SvcProxyForHtml method validate.

/**
 * This validation covers THREE cases I know of
 *  controller method public Action postHibernateEntity(CustomerDbo customer) //hibernate plugin validation AFTER looking up in DB
 *  controller method public Action postDto(@Dto SomeDto dto) //dto plugin validation AFTER looking up from remote service
 *  controller method public Action postSomething(@NotBlank String username) //simple validation
 *
 *  JSON or other content validation is done in SvcProxyForContent.java and throws BadClientRequestException so that translators
 *  can translate to generic error message sent to clients
 */
private List<Object> validate(Object controller, Method m, RequestContext requestContext, List<Object> args) {
    if (requestContext.getRequest().method != HttpMethod.POST)
        // ONLY validate on post requests
        return args;
    Validation validation = requestContext.getValidation();
    List<Violation> violations = validator.validate(controller, m, args);
    // Since this is web, just add errors to Validation object
    for (Violation violation : violations) {
        // add error to list of errors for form to display
        validation.addError(violation.getPath(), violation.getMessage());
    }
    return args;
}
Also used : Validation(org.webpieces.ctx.api.Validation) Violation(org.webpieces.http.exception.Violation)

Example 4 with Violation

use of org.webpieces.http.exception.Violation in project webpieces by deanhiller.

the class JacksonCatchAllFilter method translateViolations.

protected String translateViolations(BadRequestException t, String defaultMessage) {
    if (t.getViolations() == null || t.getViolations().size() == 0) {
        return defaultMessage;
    }
    String failures = "Your request is bad. ";
    int counter = 1;
    for (Violation violation : t.getViolations()) {
        failures += "Violation #" + counter + ":'" + violation.getMessage() + "' path=" + violation.getPath();
        if (counter < t.getViolations().size()) {
            failures += "****";
        }
    }
    return failures;
}
Also used : Violation(org.webpieces.http.exception.Violation)

Aggregations

Violation (org.webpieces.http.exception.Violation)4 ConstraintViolation (javax.validation.ConstraintViolation)2 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 Validation (org.webpieces.ctx.api.Validation)1