use of javax.validation.metadata.BeanDescriptor in project dropwizard by dropwizard.
the class DropwizardConfiguredValidator method validateResourceAndInputParams.
@Override
public void validateResourceAndInputParams(Object resource, final Invocable invocable, Object[] objects) throws ConstraintViolationException {
final Class<?>[] groups = getGroup(invocable);
final Set<ConstraintViolation<Object>> violations = new HashSet<>();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
if (beanDescriptor.isBeanConstrained()) {
violations.addAll(validate(resource, groups));
}
violations.addAll(forExecutables().validateParameters(resource, invocable.getHandlingMethod(), objects, groups));
if (!violations.isEmpty()) {
throw new JerseyViolationException(violations, invocable);
}
}
use of javax.validation.metadata.BeanDescriptor in project Payara by payara.
the class ConnectorJavaBeanValidator method validateJavaBean.
public void validateJavaBean(Object bean, String rarName) {
if (bean != null) {
Validator validator = getBeanValidator(rarName);
if (validator != null) {
BeanDescriptor bd = validator.getConstraintsForClass(bean.getClass());
bd.getConstraintDescriptors();
Class[] array = new Class[] {};
Set constraintViolations = validator.validate(bean, array);
if (constraintViolations != null && constraintViolations.size() > 0) {
ConstraintViolationException cve = new ConstraintViolationException(constraintViolations);
StringBuilder msg = new StringBuilder();
Iterator it = constraintViolations.iterator();
while (it.hasNext()) {
ConstraintViolation cv = (ConstraintViolation) it.next();
msg.append("\n Bean Class : ").append(cv.getRootBeanClass());
msg.append("\n Bean : ").append(cv.getRootBean());
msg.append("\n Property path : ").append(cv.getPropertyPath());
msg.append("\n Violation Message : ").append(cv.getMessage());
}
Object[] args = new Object[] { bean.getClass(), rarName, msg.toString() };
_logger.log(Level.SEVERE, "validation.constraints.violation", args);
throw cve;
}
} else {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "No Bean Validator is available for RAR [ " + rarName + " ]");
}
}
}
}
use of javax.validation.metadata.BeanDescriptor in project jersey by jersey.
the class DefaultConfiguredValidator method onValidate.
// Invoked as the last validation interceptor method in the chain.
@Override
public void onValidate(final ValidationInterceptorContext ctx) {
final Object resource = ctx.getResource();
final Invocable resourceMethod = ctx.getInvocable();
final Object[] args = ctx.getArgs();
final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
// Resource validation.
if (beanDescriptor.isBeanConstrained()) {
constraintViolations.addAll(validate(resource));
}
if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
final Method handlingMethod = resourceMethod.getHandlingMethod();
// Resource method validation - input parameters.
final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters()) {
constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args));
}
}
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
}
use of javax.validation.metadata.BeanDescriptor in project cuba by cuba-platform.
the class DesktopAbstractField method initBeanValidator.
protected void initBeanValidator() {
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
Class enclosingJavaClass = propertyEnclosingMetaClass.getJavaClass();
if (enclosingJavaClass != KeyValueEntity.class && !DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
BeanValidation beanValidation = AppBeans.get(BeanValidation.NAME);
javax.validation.Validator validator = beanValidation.getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(enclosingJavaClass);
if (beanDescriptor.isBeanConstrained()) {
addValidator(new BeanValidator(enclosingJavaClass, metaProperty.getName()));
}
}
}
use of javax.validation.metadata.BeanDescriptor in project kie-wb-common by kiegroup.
the class ContextModelConstraintsExtractorImpl method readModelConstraints.
@Override
public void readModelConstraints(MapModelRenderingContext clientRenderingContext, ClassLoader classLoader) {
if (clientRenderingContext == null) {
throw new IllegalArgumentException("Context cannot be null");
}
if (classLoader == null) {
throw new IllegalArgumentException("ClassLoader cannot be null");
}
clientRenderingContext.getAvailableForms().values().forEach(formDefinition -> {
if (formDefinition.getModel() instanceof JavaFormModel) {
JavaFormModel javaModel = (JavaFormModel) formDefinition.getModel();
if (clientRenderingContext.getModelConstraints().containsKey(javaModel)) {
return;
}
Class clazz = null;
try {
clazz = classLoader.loadClass(javaModel.getType());
if (clazz == null) {
clazz = getClass().forName(javaModel.getType());
}
} catch (ClassNotFoundException e) {
// maybe Class is not on the project ClassLoader, let's try on the main ClassLoader
try {
clazz = getClass().forName(javaModel.getType());
} catch (ClassNotFoundException e1) {
// ops! class not available on the main classLoader
}
}
if (clazz == null) {
logger.warn("Unable to find class for type {} on any classLoader. Skipping annotation processing", javaModel.getType());
} else {
BeanDescriptor descriptor = Validation.buildDefaultValidatorFactory().getValidator().getConstraintsForClass(clazz);
Set<PropertyDescriptor> properties = descriptor.getConstrainedProperties();
DynamicModelConstraints constraints = new DynamicModelConstraints(javaModel.getType());
clientRenderingContext.getModelConstraints().put(javaModel.getType(), constraints);
properties.forEach(property -> {
property.getConstraintDescriptors().forEach(constraintDescriptor -> {
Map<String, Object> attributes = new HashMap<>();
constraintDescriptor.getAttributes().forEach((key, value) -> {
if (key.equals("payload") || key.equals("groups")) {
return;
}
Object portableValue;
if (EnvUtil.isPortableType(value.getClass())) {
portableValue = value;
} else {
portableValue = value.toString();
}
attributes.put(key, portableValue);
});
constraints.addConstraintForField(property.getPropertyName(), new FieldConstraint(constraintDescriptor.getAnnotation().annotationType().getName(), attributes));
});
});
}
}
});
}
Aggregations