@Override
  @SuppressWarnings("unchecked")
  public void afterPropertiesSet() {
    @SuppressWarnings("rawtypes")
    Configuration configuration =
        (this.providerClass != null
            ? Validation.byProvider(this.providerClass).configure()
            : Validation.byDefaultProvider().configure());

    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
      targetInterpolator = configuration.getDefaultMessageInterpolator();
    }
    configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));

    if (this.traversableResolver != null) {
      configuration.traversableResolver(this.traversableResolver);
    }

    ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
    if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
      targetConstraintValidatorFactory =
          new SpringConstraintValidatorFactory(
              this.applicationContext.getAutowireCapableBeanFactory());
    }
    if (targetConstraintValidatorFactory != null) {
      configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
    }

    configureParameterNameProviderIfPossible(configuration);

    if (this.mappingLocations != null) {
      for (Resource location : this.mappingLocations) {
        try {
          configuration.addMapping(location.getInputStream());
        } catch (IOException ex) {
          throw new IllegalStateException("Cannot read mapping resource: " + location);
        }
      }
    }

    for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) {
      configuration.addProperty(entry.getKey(), entry.getValue());
    }

    // Allow for custom post-processing before we actually build the ValidatorFactory.
    postProcessConfiguration(configuration);

    this.validatorFactory = configuration.buildValidatorFactory();
    setTargetValidator(this.validatorFactory.getValidator());
  }
  private ValidatorFactory() {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    final MessageInterpolator defaultMessageInterpolator = config.getDefaultMessageInterpolator();

    config.messageInterpolator(
        new MessageInterpolator() {
          @Override
          public String interpolate(String messageTemplate, Context context) {
            String message;
            if (context.getConstraintDescriptor().getPayload() != null
                && context.getConstraintDescriptor().getPayload().contains(Localized.class)) {
              // Retrieve message from message library
              message = MessageUtility.getLocalizedMessage(messageTemplate).getMessage();
            } else {
              message = defaultMessageInterpolator.interpolate(messageTemplate, context);
            }
            return message;
          }

          @Override
          public String interpolate(String messageTemplate, Context context, Locale locale) {
            String message;
            if (context.getConstraintDescriptor().getPayload() != null
                && context.getConstraintDescriptor().getPayload().contains(Localized.class)) {
              // Retrieve message from message library
              message = MessageUtility.getLocalizedMessage(messageTemplate, locale).getMessage();
            } else {
              message = defaultMessageInterpolator.interpolate(messageTemplate, context);
            }
            return message;
          }
        });

    // Stop validation if Entity action is DELETE
    config.traversableResolver(
        new TraversableResolver() {

          @Override
          public boolean isReachable(
              Object traversableObject,
              Node traversableProperty,
              Class<?> rootBeanType,
              Path pathToTraversableObject,
              ElementType elementType) {
            if (traversableObject != null
                && AbstractBindingBean.class.isAssignableFrom(traversableObject.getClass())) {
              AbstractBindingBean bean = (AbstractBindingBean) traversableObject;
              if (bean.getEntityAction() != null && bean.getEntityAction() == EntityAction.DELETE) {
                return false;
              }
            }
            return true;
          }

          @Override
          public boolean isCascadable(
              Object traversableObject,
              Node traversableProperty,
              Class<?> rootBeanType,
              Path pathToTraversableObject,
              ElementType elementType) {
            return true;
          }
        });

    javax.validation.ValidatorFactory factory = config.buildValidatorFactory();
    validator = factory.getValidator();
  }
示例#3
0
 @Override
 public MessageInterpolator getDefaultMessageInterpolator() {
   return delegate.getDefaultMessageInterpolator();
 }
 @Override
 public MessageInterpolator getDefaultMessageInterpolator() {
   return configuration.getDefaultMessageInterpolator();
 }