Java Code Examples for org.hibernate.validator.HibernateValidatorConfiguration#buildValidatorFactory()

The following examples show how to use org.hibernate.validator.HibernateValidatorConfiguration#buildValidatorFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateValidation.java    From validator-web with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ValidationContext context) {
    context.setValidatorDelegate(this);
    
    HibernateValidatorConfiguration configure = javax.validation.Validation.byProvider(HibernateValidator.class).configure();
    if (context instanceof ValidationContext.Accessor) {
        configure.failFast(((ValidationContext.Accessor) context).isFailFast());
    }
    ValidatorFactory factory = configure.buildValidatorFactory();
    validator = factory.getValidator();
}
 
Example 2
Source File: BeanValidation.java    From open-Autoscaler with Apache License 2.0 5 votes vote down vote up
public static JsonNode parsePolicy(String jsonString, Map<String, String> service_info, HttpServletRequest httpServletRequest) throws JsonParseException, JsonMappingException, IOException{
	 List<String> violation_message = new ArrayList<String>();
	 ObjectNode result = new_mapper.createObjectNode();
	 result.put("valid", false);
	 logger.info("received policy : " + jsonString); //debug
	 new_mapper.readValue(jsonString, Policy.class); //for syntax error check
	 String transfered_json = TransferedPolicy.packServiceInfo(jsonString, service_info);
	 logger.info("transfered policy after update with service_information : " + transfered_json); //debug
	 TransferedPolicy policy = new_mapper.readValue(transfered_json, TransferedPolicy.class);
	 logger.info("we get policy as " + (Obj2Map(policy)).toString());//debug
	 //additional data manipulation and check again
	 policy = policy.setMaxInstCount();
	 HibernateValidatorConfiguration config = BeanValidation.getPolicyRange();
	 ValidatorFactory vf = config.buildValidatorFactory();
	 Locale locale = LocaleUtil.getLocale(httpServletRequest);
	 MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(vf.getMessageInterpolator(), locale);
	 Validator validator = vf.usingContext().messageInterpolator(interpolator).getValidator();
	 Set<ConstraintViolation<TransferedPolicy>> set = validator.validate(policy);
	 if (set.size() > 0 ){
		 for (ConstraintViolation<TransferedPolicy> constraintViolation : set) {
			 violation_message.add(constraintViolation.getMessage());
		 }
		 result.set("violation_message", new_mapper.valueToTree(violation_message));
		 return result;
	 }

	 String new_json = policy.transformInput();
	 logger.info("policy before trigger back : " + new_json); //debug
	 new_json = TransferedPolicy.unpackServiceInfo(new_json, service_info);
	 result.put("valid", true);
	 logger.info("send out policy: " + new_json); //debug
	 result.put("new_json", new_json);
	 return result;
}
 
Example 3
Source File: BeanValidationImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options) {
    checkNotNullArgument(options);

    if (constraintMapping == null
            && options.getFailFast() == null
            && options.getLocale() != null) {
        return getValidatorWithDefaultFactory(options.getLocale());
    }

    Locale locale;
    if (options.getLocale() != null) {
        locale = options.getLocale();
    } else {
        locale = getCurrentLocale();
    }

    HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(locale);
    if (options.getFailFast() != null) {
        configuration.failFast(options.getFailFast());
    }
    if (constraintMapping != null) {
        configuration.addMapping(constraintMapping);
    }

    ValidatorFactory factory = configuration.buildValidatorFactory();
    return factory.getValidator();
}
 
Example 4
Source File: BeanValidationImpl.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected Validator getValidatorWithDefaultFactory(Locale locale) {
    HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(locale);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    return factory.getValidator();
}
 
Example 5
Source File: HibernateValidatorService.java    From wisdom with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes the validator, and registers it as an OSGi service (if the bundle context is set).
 *
 * @return the validator.
 */
@Validate
public Validator initialize() {
    // configure and build an instance of ValidatorFactory
    ProviderSpecificBootstrap<HibernateValidatorConfiguration> validationBootStrap = javax.validation.Validation
            .byProvider(HibernateValidator.class);

    // bootstrap to properly resolve in an OSGi environment
    validationBootStrap.providerResolver(new HibernateValidationProviderResolver());

    HibernateValidatorConfiguration configure = validationBootStrap.configure().messageInterpolator(interpolator);
    interpolator.setDefaultInterpolator(configure.getDefaultMessageInterpolator());

    // now that we've done configuring the ValidatorFactory, let's build it
    ValidatorFactory validatorFactory = configure.buildValidatorFactory();

    // retrieve a unique validator.
    Validator validator = validatorFactory.getValidator();

    // Register the validator.
    if (context != null) {
        registration = context.registerService(Validator.class, new WrappedValidator(validator), null);
        factoryRegistration = context.registerService(ValidatorFactory.class, validatorFactory, null);
    }

    return validator;
}