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