Search in sources :

Example 6 with BeanDescriptor

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);
    }
}
Also used : BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) HashSet(java.util.HashSet)

Example 7 with BeanDescriptor

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 + " ]");
            }
        }
    }
}
Also used : Set(java.util.Set) BeanDescriptor(javax.validation.metadata.BeanDescriptor) Iterator(java.util.Iterator)

Example 8 with BeanDescriptor

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);
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) MethodDescriptor(javax.validation.metadata.MethodDescriptor) HashSet(java.util.HashSet)

Example 9 with BeanDescriptor

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()));
        }
    }
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) BeanValidator(com.haulmont.cuba.gui.components.validators.BeanValidator) MetaClass(com.haulmont.chile.core.model.MetaClass) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) BeanDescriptor(javax.validation.metadata.BeanDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) KeyValueEntity(com.haulmont.cuba.core.entity.KeyValueEntity)

Example 10 with BeanDescriptor

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));
                    });
                });
            }
        }
    });
}
Also used : DynamicModelConstraints(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) HashMap(java.util.HashMap) BeanDescriptor(javax.validation.metadata.BeanDescriptor) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) FieldConstraint(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint)

Aggregations

BeanDescriptor (javax.validation.metadata.BeanDescriptor)11 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)5 HashSet (java.util.HashSet)3 ConstraintViolation (javax.validation.ConstraintViolation)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 KeyValueEntity (com.haulmont.cuba.core.entity.KeyValueEntity)2 BeanValidation (com.haulmont.cuba.core.global.BeanValidation)2 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)2 BeanValidator (com.haulmont.cuba.gui.components.validators.BeanValidator)2 Method (java.lang.reflect.Method)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 MethodDescriptor (javax.validation.metadata.MethodDescriptor)2 MinijaxBeanDescriptor (org.minijax.validator.metadata.MinijaxBeanDescriptor)2 MinijaxPropertyDescriptor (org.minijax.validator.metadata.MinijaxPropertyDescriptor)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Response (javax.ws.rs.core.Response)1 Invocable (org.glassfish.jersey.server.model.Invocable)1 Component (org.hibernate.mapping.Component)1