Search in sources :

Example 1 with ConstraintViolation

use of org.qi4j.api.constraint.ConstraintViolation in project qi4j-sdk by Qi4j.

the class Qi4jPropertyAccessor method setProperty.

@Override
@SuppressWarnings("unchecked")
public final void setProperty(Map aContext, Object aTarget, Object aPropertyName, Object aPropertyValue) throws OgnlException {
    String fieldName = aPropertyName.toString();
    Object qi4jField = getQi4jField(aContext, aTarget, fieldName);
    if (qi4jField != null) {
        Class<?> memberClass = qi4jField.getClass();
        if (Property.class.isAssignableFrom(memberClass)) {
            Property property = (Property) qi4jField;
            OgnlContext ognlContext = (OgnlContext) aContext;
            Class<?> propertyType = (Class) api.propertyDescriptorFor(property).type();
            Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, propertyType);
            try {
                property.set(convertedValue);
            } catch (ConstraintViolationException e) {
                Collection<ConstraintViolation> violations = e.constraintViolations();
                handleConstraintViolation(aContext, aTarget, fieldName, convertedValue, violations);
            }
            return;
        } else if (Association.class.isAssignableFrom(memberClass)) {
            Association association = (Association) qi4jField;
            OgnlContext ognlContext = (OgnlContext) aContext;
            Class<?> associationType = (Class) api.associationDescriptorFor(association).type();
            Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, associationType);
            if (convertedValue == OgnlRuntime.NoConversionPossible) {
                throw new OgnlException("Could not convert value to association type");
            }
            try {
                association.set(convertedValue);
            } catch (ConstraintViolationException e) {
                Collection<ConstraintViolation> violations = e.constraintViolations();
                handleConstraintViolation(aContext, aTarget, fieldName, aPropertyValue, violations);
            }
            return;
        } else if (ManyAssociation.class.isAssignableFrom(memberClass)) {
            throw new OgnlException("Setting many association [" + fieldName + "] is impossible.");
        } else if (NamedAssociation.class.isAssignableFrom(memberClass)) {
            throw new OgnlException("Setting named association [" + fieldName + "] is impossible.");
        }
    }
    super.setProperty(aContext, aTarget, aPropertyName, aPropertyValue);
}
Also used : NamedAssociation(org.qi4j.api.association.NamedAssociation) OgnlContext(ognl.OgnlContext) OgnlException(ognl.OgnlException) ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) NamedAssociation(org.qi4j.api.association.NamedAssociation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Collection(java.util.Collection) Property(org.qi4j.api.property.Property)

Example 2 with ConstraintViolation

use of org.qi4j.api.constraint.ConstraintViolation in project qi4j-sdk by Qi4j.

the class ValueConstraintsInstance method checkConstraints.

@SuppressWarnings({ "raw", "unchecked" })
public List<ConstraintViolation> checkConstraints(Object value) {
    List<ConstraintViolation> violations = null;
    // Check optional first - this avoids NPE's in constraints
    if (optional) {
        if (value == null) {
            violations = Collections.emptyList();
        }
    } else {
        if (value == null) {
            violations = new ArrayList<>();
            violations.add(new ConstraintViolation(name, OPTIONAL, null));
        }
    }
    if (violations == null && value != null) {
        for (ConstraintInstance constraint : constraints) {
            boolean valid;
            try {
                valid = constraint.isValid(value);
            } catch (NullPointerException e) {
                // A NPE is the same as a failing constraint
                valid = false;
            }
            if (!valid) {
                if (violations == null) {
                    violations = new ArrayList<>();
                }
                ConstraintViolation violation = new ConstraintViolation(name, constraint.annotation(), value);
                violations.add(violation);
            }
        }
    }
    if (violations == null) {
        violations = Collections.emptyList();
    }
    return violations;
}
Also used : ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation)

Example 3 with ConstraintViolation

use of org.qi4j.api.constraint.ConstraintViolation in project qi4j-sdk by Qi4j.

the class ContextResource method handleException.

private void handleException(Response response, Throwable ex) {
    while (ex instanceof InvocationTargetException) {
        ex = ex.getCause();
    }
    try {
        throw ex;
    } catch (ResourceException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(e.getStatus());
    } catch (ConstraintViolationException e) {
        try {
            ConstraintViolationMessages cvm = new ConstraintViolationMessages();
            // CVE are considered client faults
            String messages = "";
            Locale locale = ObjectSelection.type(Locale.class);
            for (ConstraintViolation constraintViolation : e.constraintViolations()) {
                if (!messages.isEmpty()) {
                    messages += "\n";
                }
                messages += cvm.getMessage(constraintViolation, locale);
            }
            response.setEntity(new StringRepresentation(messages));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        } catch (Exception e1) {
            response.setEntity(new StringRepresentation(e.getMessage()));
            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
        }
    } catch (IllegalArgumentException e) {
        // IAE (or subclasses) are considered client faults
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (RuntimeException e) {
        // RuntimeExceptions are considered server faults
        LoggerFactory.getLogger(getClass()).warn("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    } catch (Exception e) {
        // Checked exceptions are considered client faults
        String s = e.getMessage();
        if (s == null) {
            s = e.getClass().getSimpleName();
        }
        response.setEntity(new StringRepresentation(s));
        response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
    } catch (Throwable e) {
        // Anything else are considered server faults
        LoggerFactory.getLogger(getClass()).error("Exception thrown during processing", e);
        response.setEntity(new StringRepresentation(e.getMessage()));
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
    }
}
Also used : Locale(java.util.Locale) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) ConstraintViolationMessages(org.qi4j.library.rest.server.restlet.ConstraintViolationMessages) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException)

Example 4 with ConstraintViolation

use of org.qi4j.api.constraint.ConstraintViolation in project qi4j-sdk by Qi4j.

the class ConstraintViolationInterceptor method addConstraintViolationFieldErrors.

private void addConstraintViolationFieldErrors(ValueStack stack, ValidationAware va, String fieldName, FieldConstraintViolations violations) {
    for (ConstraintViolation constraintViolation : violations.constraintViolations()) {
        Object target = violations.target();
        String message = message(target, violations.propertyName(), constraintViolation, stack);
        va.addFieldError(fieldName, message);
    }
}
Also used : ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation)

Aggregations

ConstraintViolation (org.qi4j.api.constraint.ConstraintViolation)4 ConstraintViolationException (org.qi4j.api.constraint.ConstraintViolationException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 Locale (java.util.Locale)1 OgnlContext (ognl.OgnlContext)1 OgnlException (ognl.OgnlException)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1 NamedAssociation (org.qi4j.api.association.NamedAssociation)1 Property (org.qi4j.api.property.Property)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 ConstraintViolationMessages (org.qi4j.library.rest.server.restlet.ConstraintViolationMessages)1 StringRepresentation (org.restlet.representation.StringRepresentation)1 ResourceException (org.restlet.resource.ResourceException)1