/** * create validator * * @param constraintValidatorFactory * @param messageInterpolator * @param traversableResolver * @param parameterNameProvider * @param failFast * @param validatedValueHandlers * @return */ protected Validator createValidator(ConstraintValidatorFactory constraintValidatorFactory, MessageInterpolator messageInterpolator, TraversableResolver traversableResolver, ParameterNameProvider parameterNameProvider, boolean failFast, List<ValidatedValueUnwrapper<?>> validatedValueHandlers) { BeanMetaDataManager513 beanMetaDataManager; if (!beanMetaDataManagerMap.containsKey(parameterNameProvider)) { beanMetaDataManager = new BeanMetaDataManager513(constraintHelper, executableHelper, parameterNameProvider, buildDataProviders(parameterNameProvider)); beanMetaDataManagerMap.put(parameterNameProvider, beanMetaDataManager); } else { beanMetaDataManager = beanMetaDataManagerMap.get(parameterNameProvider); } return new ValidatorImpl(constraintValidatorFactory, messageInterpolator, traversableResolver, beanMetaDataManager, parameterNameProvider, typeResolutionHelper, validatedValueHandlers, constraintValidatorManager, failFast); }
/** * Returns a list with {@link ValidatedValueUnwrapper}s configured via the * {@link HibernateValidatorConfiguration#VALIDATED_VALUE_HANDLERS} * property. * * @param properties * the properties used to bootstrap the factory * * @return a list with property-configured {@link ValidatedValueUnwrapper}s; * May be empty but never {@code null} */ private List<ValidatedValueUnwrapper<?>> getPropertyConfiguredValidatedValueHandlers(Map<String, String> properties) { String propertyValue = properties.get(HibernateValidatorConfiguration.VALIDATED_VALUE_HANDLERS); if (propertyValue == null || propertyValue.isEmpty()) { return Collections.emptyList(); } String[] handlerNames = propertyValue.split(","); List<ValidatedValueUnwrapper<?>> handlers = newArrayList(handlerNames.length); for (String handlerName : handlerNames) { @SuppressWarnings("unchecked") Class<? extends ValidatedValueUnwrapper<?>> handlerType = (Class<? extends ValidatedValueUnwrapper<?>>) run(LoadClass .action(handlerName, ValidatorFactoryImpl513.class)); handlers.add(run(NewInstance.action(handlerType, "validated value handler class"))); } return handlers; }
public ValidatorContextImpl513(ValidatorFactoryImpl513 validatorFactory) { this.validatorFactory = validatorFactory; this.messageInterpolator = validatorFactory.getMessageInterpolator(); this.traversableResolver = validatorFactory.getTraversableResolver(); this.constraintValidatorFactory = validatorFactory.getConstraintValidatorFactory(); this.parameterNameProvider = validatorFactory.getParameterNameProvider(); this.failFast = validatorFactory.isFailFast(); this.validatedValueHandlers = new ArrayList<ValidatedValueUnwrapper<?>>( validatorFactory.getValidatedValueHandlers()); }
@Test public void verifyConfigurable() throws Exception { ObjectMapper mapper = Jackson.newObjectMapper(); // dropwizard 0.9.1 changed the validation wiring a bit.. Class<ValidatedValueUnwrapper> optValidatorClazz = (Class<ValidatedValueUnwrapper>) Class .forName("io.dropwizard.validation.valuehandling.OptionalValidatedValueUnwrapper"); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); if (optValidatorClazz != null) { validator = Validation.byProvider(HibernateValidator.class).configure() .addValidatedValueHandler(optValidatorClazz.newInstance()) .buildValidatorFactory().getValidator(); } ConfigurationFactory<CloudWatchReporterFactory> configFactory = new ConfigurationFactory<>(CloudWatchReporterFactory.class, validator, mapper, "dw"); CloudWatchReporterFactory f = configFactory.build(new File(Resources.getResource("cw.yml").getFile())); assertEquals("[env=default]", f.getGlobalDimensions().toString()); assertEquals("us-east-1", f.getAwsRegion()); assertEquals("a.b", f.getNamespace()); assertEquals("XXXXX", f.getAwsSecretKey()); assertEquals("11111", f.getAwsAccessKeyId()); assertEquals("p.neustar.biz", f.getAwsClientConfiguration().getProxyHost()); assertNull(f.getAwsClientConfiguration().getProxyUsername()); }
public ValidatorFactoryImpl513(ConfigurationState configurationState) { this.messageInterpolator = configurationState.getMessageInterpolator(); this.traversableResolver = configurationState.getTraversableResolver(); this.parameterNameProvider = configurationState.getParameterNameProvider(); this.beanMetaDataManagerMap = Collections .synchronizedMap(new IdentityHashMap<ParameterNameProvider, BeanMetaDataManager513>()); this.constraintHelper = new ConstraintHelper(); this.typeResolutionHelper = new TypeResolutionHelper(); this.executableHelper = new ExecutableHelper(typeResolutionHelper); // HV-302; don't load XmlMappingParser if not necessary if (configurationState.getMappingStreams().isEmpty()) { this.xmlMetaDataProvider = null; } else { this.xmlMetaDataProvider = new XmlMetaDataProvider(constraintHelper, parameterNameProvider, configurationState.getMappingStreams()); } Map<String, String> properties = configurationState.getProperties(); boolean tmpFailFast = false; List<ValidatedValueUnwrapper<?>> tmpValidatedValueHandlers = newArrayList(5); Set<DefaultConstraintMapping> tmpConstraintMappings = newHashSet(); if (configurationState instanceof ConfigurationImpl) { ConfigurationImpl hibernateSpecificConfig = (ConfigurationImpl) configurationState; if (hibernateSpecificConfig.getProgrammaticMappings().size() > 0) { tmpConstraintMappings.addAll(hibernateSpecificConfig.getProgrammaticMappings()); } // check whether fail fast is programmatically enabled tmpFailFast = hibernateSpecificConfig.getFailFast(); tmpValidatedValueHandlers.addAll(hibernateSpecificConfig.getValidatedValueHandlers()); } this.constraintMappings = Collections.unmodifiableSet(tmpConstraintMappings); tmpFailFast = checkPropertiesForFailFast(properties, tmpFailFast); this.failFast = tmpFailFast; tmpValidatedValueHandlers.addAll(getPropertyConfiguredValidatedValueHandlers(properties)); this.validatedValueHandlers = Collections.unmodifiableList(tmpValidatedValueHandlers); this.constraintValidatorManager = new ConstraintValidatorManager( configurationState.getConstraintValidatorFactory()); }
public List<ValidatedValueUnwrapper<?>> getValidatedValueHandlers() { return validatedValueHandlers; }
@Override public HibernateValidatorContext addValidationValueHandler(ValidatedValueUnwrapper<?> handler) { this.validatedValueHandlers.add(handler); return this; }
public TypeResolverBasedValueUnwrapper(TypeResolutionHelper typeResolutionHelper) { this.typeResolver = typeResolutionHelper.getTypeResolver(); clazz = resolveSingleTypeParameter( typeResolver, this.getClass(), ValidatedValueUnwrapper.class ); }