javax.validation.Configuration Java Examples

The following examples show how to use javax.validation.Configuration. 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: ImageCatalogV4BaseTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
    Configuration<?> cfg = Validation.byDefaultProvider().configure();
    cfg.messageInterpolator(new ParameterMessageInterpolator());
    validator = cfg.buildValidatorFactory().getValidator();

    for (Entry<String, Object> entry : Map.of("HTTP_CONTENT_SIZE_VALIDATOR", httpContentSizeValidator, "HTTP_HELPER", httpHelper).entrySet()) {
        Field field = ReflectionUtils.findField(ImageCatalogValidator.class, entry.getKey());
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, entry.getValue());
    }

    when(httpContentSizeValidator.isValid(anyString(), any(ConstraintValidatorContext.class))).thenReturn(true);
    when(statusType.getFamily()).thenReturn(Family.SUCCESSFUL);
}
 
Example #2
Source File: ValidationManager.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
public ValidatorFactory createValidatorFactory(Consumer<Configuration> customizer) {
    boolean skipAutoconfig = "false".equalsIgnoreCase(System.getProperty(SEEDSTACK_VALIDATION_AUTOCONFIG, "true"));
    boolean hasXmlConfiguration = findMostCompleteClassLoader(ValidationManager.class)
            .getResource(VALIDATION_XML_FILE) != null;
    if (validationLevel == ValidationLevel.NONE) {
        throw SeedException.createNew(CoreErrorCode.UNABLE_TO_CREATE_VALIDATOR_FACTORY);
    } else {
        try {
            Configuration<?> configuration = Validation.byDefaultProvider().configure();
            if (!hasXmlConfiguration && !skipAutoconfig) {
                Classes.optional("org.seedstack.seed.core.internal.validation.ReflectionParameterNameProvider")
                        .map(Classes::instantiateDefault)
                        .ifPresent(c -> configuration.parameterNameProvider((ParameterNameProvider) c));
                Classes.optional("org.seedstack.seed.core.internal.validation.HibernateMessageInterpolator")
                        .map(Classes::instantiateDefault)
                        .ifPresent(c -> configuration.messageInterpolator((MessageInterpolator) c));
            }
            if (customizer != null) {
                customizer.accept(configuration);
            }
            return configuration.buildValidatorFactory();
        } catch (Exception e) {
            throw SeedException.wrap(e, CoreErrorCode.UNABLE_TO_CREATE_VALIDATOR_FACTORY);
        }
    }
}
 
Example #3
Source File: ValidationExtension.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ValidationExtension
 *
 * @param mapper the Mapper instance to use
 */
public ValidationExtension(final Mapper mapper) {
    final Configuration<?> configuration = Validation.byDefaultProvider().configure();
    validationFactory = configuration.buildValidatorFactory();

    mapper.addInterceptor(this);
}
 
Example #4
Source File: DefaultValidationContext.java    From sejda with GNU Affero General Public License v3.0 5 votes vote down vote up
private DefaultValidationContext() {
    Configuration<?> validationConfig = Validation.byDefaultProvider().configure();
    if (new DefaultSejdaContext().isIgnoreXmlConfiguration()) {
        validationConfig.ignoreXmlConfiguration();
    }
    ValidatorFactory factory = validationConfig.buildValidatorFactory();
    validator = factory.getValidator();
}
 
Example #5
Source File: ValidatorBuilder.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static ValidatorFactory buildFactory(final ValidationInfo config, final ClassLoader classLoader) {
    ValidatorFactory factory = null;
    final Thread thread = Thread.currentThread();
    final ClassLoader oldContextLoader = thread.getContextClassLoader();
    try {
        thread.setContextClassLoader(classLoader);
        if (config == null) {
            factory = Validation.buildDefaultValidatorFactory();
        } else {
            final Configuration<?> configuration = getConfig(config);
            try {
                factory = configuration.buildValidatorFactory();
            } catch (final ValidationException ve) {
                thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
                factory = Validation.buildDefaultValidatorFactory();
                thread.setContextClassLoader(classLoader);

                logger.warning("Unable create validator factory with config " + config
                    + " (" + ve.getMessage() + ")."
                    + " Default factory will be used.");
            }
        }
    } finally {
        thread.setContextClassLoader(oldContextLoader);
    }
    return factory;
}
 
Example #6
Source File: ValidationConfigurationContextResolver.java    From jaxrs-beanvalidation-javaee7 with Apache License 2.0 5 votes vote down vote up
/**
 * Get a context of type {@code GeneralValidator} that is applicable to the supplied type.
 *
 * @param type the class of object for which a context is desired
 * @return a context for the supplied type or {@code null} if a context for the supplied type is not available from
 *         this provider.
 */
@Override
public GeneralValidator getContext(Class<?> type) {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    BootstrapConfiguration bootstrapConfiguration = config.getBootstrapConfiguration();

    config.messageInterpolator(new LocaleSpecificMessageInterpolator(Validation.byDefaultProvider().configure()
            .getDefaultMessageInterpolator()));
    config.parameterNameProvider(new CustomParameterNameProvider());

    return new GeneralValidatorImpl(config.buildValidatorFactory(),
            bootstrapConfiguration.isExecutableValidationEnabled(),
            bootstrapConfiguration.getDefaultValidatedExecutableTypes());
}
 
Example #7
Source File: BeanValidationImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a validator factory
 * @return The factory
 */
static ValidatorFactory createValidatorFactory()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new IronJacamarTraversableResolver());

   return conf.buildValidatorFactory();
}
 
Example #8
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration cfg) {
    if (cfg != null) {
        factoryCfg.parameterNameProvider(cfg.getParameterNameProvider());
        factoryCfg.messageInterpolator(cfg.getMessageInterpolator());
        factoryCfg.traversableResolver(cfg.getTraversableResolver());
        factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory());
        for (Map.Entry<String, String> entry : cfg.getProperties().entrySet()) {
            factoryCfg.addProperty(entry.getKey(), entry.getValue());
        }
    }
}
 
Example #9
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public <T extends Configuration<T>, U extends ValidationProvider<T>> BeanValidationProvider(
    ValidationProviderResolver resolver,
    Class<U> providerType,
    ValidationConfiguration cfg) {
    try {
        Configuration<?> factoryCfg = providerType != null
            ? Validation.byProvider(providerType).providerResolver(resolver).configure()
            : Validation.byDefaultProvider().providerResolver(resolver).configure();
        initFactoryConfig(factoryCfg, cfg);
        factory = factoryCfg.buildValidatorFactory();
    } catch (final ValidationException ex) {
        LOG.severe("Bean Validation provider can not be found, no validation will be performed");
        throw ex;
    }
}
 
Example #10
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public BeanValidationProvider(ValidationConfiguration cfg) {
    try {
        Configuration<?> factoryCfg = Validation.byDefaultProvider().configure();
        initFactoryConfig(factoryCfg, cfg);
        factory = factoryCfg.buildValidatorFactory();
    } catch (final ValidationException ex) {
        LOG.severe("Bean Validation provider can not be found, no validation will be performed");
        throw ex;
    }
}
 
Example #11
Source File: ValidationBundle.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static Validator buildValidator ()
{
    final GenericBootstrap bootstrap = Validation.byDefaultProvider ();
    bootstrap.providerResolver ( tracker );

    final Configuration<?> cfg = bootstrap.configure ();
    final OsgiMessageInterpolator mi = getMessageInterpolator ();
    mi.setFallback ( cfg.getDefaultMessageInterpolator () );
    cfg.messageInterpolator ( mi );

    final ValidatorFactory factory = cfg.buildValidatorFactory ();
    return factory.getValidator ();
}
 
Example #12
Source File: App.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void configure() {
    logger.info("init app, name={}, language={}, dir={}", name(), language(), dir());
    binder = new ModuleBinder();

    binder.bind(App.class).toInstance(this);
    binder.bind(MessageBundle.class).toInstance(message());

    binder.bind(Configuration.class).toInstance(Validation.byDefaultProvider().configure()
        .messageInterpolator(new MessageInterpolatorImpl())
        .addProperty("hibernate.validator.fail_fast", "true"));
    register(ValidationFeature.class);

    JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
    jacksonProvider.setMapper(JSON.OBJECT_MAPPER);
    register(jacksonProvider);
    register(JacksonFeature.class);

    register(binder.raw());
    register(new DefaultContainerLifecycleListener(this));

    register(new AppEventListener());
    register(DefaultExceptionMapper.class);

    validate();

    for (String moduleName : orderedModules()) {
        ModuleNode moduleNode = moduleNode(moduleName).orElseThrow();
        if (moduleNode.isOverrided()) {
            moduleNode.status = ModuleStatus.CONFIGURED;
        } else {
            try {
                Stopwatch w = Stopwatch.createStarted();
                configure(moduleNode.module);
                moduleNode.status = ModuleStatus.CONFIGURED;
            } catch (Exception e) {
                throw new ApplicationException("failed to install module {}, type={}", moduleName, moduleNode.module.getClass().getCanonicalName(), e);
            }
        }
    }
}
 
Example #13
Source File: ValidatorContextResolver.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public GeneralValidator getContext(final Class<?> type) {
    final ResourceBundleLocator resourceBundleLocator = new PlatformResourceBundleLocator("messages");
    final MessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator(resourceBundleLocator);
    final Configuration<?> config = Validation.byDefaultProvider().configure()
        .messageInterpolator(messageInterpolator);
    final BootstrapConfiguration bootstrapConfiguration = config.getBootstrapConfiguration();
    final boolean isExecutableValidationEnabled = bootstrapConfiguration.isExecutableValidationEnabled();
    final Set<ExecutableType> defaultValidatedExecutableTypes = bootstrapConfiguration
        .getDefaultValidatedExecutableTypes();

    return new GeneralValidatorImpl(validatorFactory, isExecutableValidationEnabled,
        defaultValidatedExecutableTypes);
}
 
Example #14
Source File: BeanValidation.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public BeanValidation()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

   validatorFactory = conf.buildValidatorFactory();
   validator = validatorFactory.getValidator();
}
 
Example #15
Source File: BeanValidationUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a validator factory
 * @return The factory
 */
public static ValidatorFactory createValidatorFactory()
{
   Configuration configuration = Validation.byDefaultProvider().configure();
   Configuration<?> conf = configuration.traversableResolver(new JCATraversableResolver());

   return conf.buildValidatorFactory();
}
 
Example #16
Source File: ActionValidator.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected Configuration<?> createConfiguration() {
    return newGenericBootstrap().configure();
}
 
Example #17
Source File: TestUtils.java    From sejda with GNU Affero General Public License v3.0 4 votes vote down vote up
private ValidatorHolder() {
    Configuration<?> validationConfig = Validation.byDefaultProvider().configure();
    validationConfig.ignoreXmlConfiguration();
    ValidatorFactory factory = validationConfig.buildValidatorFactory();
    validator = factory.getValidator();
}
 
Example #18
Source File: ValidatorBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
public OpenEjbConfig(final BootstrapConfiguration bootstrapConfig, final Configuration<T> target) {
    bootstrap = bootstrapConfig;
    delegate = target;
}
 
Example #19
Source File: AbstractValidationTest.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
	final Configuration<HibernateValidatorConfiguration> configuration = Validation.byProvider( HibernateValidator.class ).configure();
	validator = configuration.buildValidatorFactory().getValidator();
}
 
Example #20
Source File: AbstractValidationTest.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
	final Configuration<HibernateValidatorConfiguration> configuration = Validation.byProvider( HibernateValidator.class ).configure();
	validator = configuration.buildValidatorFactory().getValidator();
}
 
Example #21
Source File: StackV4RequestCompatibilityTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    Configuration<?> cfg = Validation.byDefaultProvider().configure();
    cfg.messageInterpolator(new ParameterMessageInterpolator());
    validator = cfg.buildValidatorFactory().getValidator();
}
 
Example #22
Source File: BeanValidationProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public <T extends Configuration<T>, U extends ValidationProvider<T>> BeanValidationProvider(
    ValidationProviderResolver resolver,
    Class<U> providerType) {
    this(resolver, providerType, null);
}
 
Example #23
Source File: ActionValidator.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected ValidatorFactory buildValidatorFactory(VaConfigSetupper hibernateConfigSetupper) {
    final Configuration<?> configuration = createConfiguration();
    setupFrameworkConfiguration(configuration);
    setupYourConfiguration(configuration, hibernateConfigSetupper);
    return configuration.buildValidatorFactory();
}
 
Example #24
Source File: SizePropertyValidatorTest.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public void setup() {
    Configuration<?> validationConf = Validation.byDefaultProvider().configure();
    validator = validationConf.buildValidatorFactory().getValidator();
}
 
Example #25
Source File: ActionValidator.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected void setupFrameworkConfiguration(Configuration<?> configuration) {
    configuration.messageInterpolator(newResourceBundleMessageInterpolator());
}
 
Example #26
Source File: ActionValidator.java    From lastaflute with Apache License 2.0 4 votes vote down vote up
protected void setupYourConfiguration(Configuration<?> configuration, VaConfigSetupper hibernateConfigSetupper) {
    hibernateConfigSetupper.setup(configuration);
}
 
Example #27
Source File: LocalValidatorFactoryBean.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Post-process the given Bean Validation configuration,
 * adding to or overriding any of its settings.
 * <p>Invoked right before building the {@link ValidatorFactory}.
 * @param configuration the Configuration object, pre-populated with
 * settings driven by LocalValidatorFactoryBean's properties
 */
protected void postProcessConfiguration(Configuration<?> configuration) {
}
 
Example #28
Source File: VaConfigSetupper.java    From lastaflute with Apache License 2.0 2 votes vote down vote up
/**
 * @param conf The configuration of hibernate validator. (NotNull)
 */
void setup(Configuration<?> conf);
 
Example #29
Source File: LocalValidatorFactoryBean.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Post-process the given Bean Validation configuration,
 * adding to or overriding any of its settings.
 * <p>Invoked right before building the {@link ValidatorFactory}.
 * @param configuration the Configuration object, pre-populated with
 * settings driven by LocalValidatorFactoryBean's properties
 */
protected void postProcessConfiguration(Configuration<?> configuration) {
}
 
Example #30
Source File: LocalValidatorFactoryBean.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Post-process the given Bean Validation configuration,
 * adding to or overriding any of its settings.
 * <p>Invoked right before building the {@link ValidatorFactory}.
 * @param configuration the Configuration object, pre-populated with
 * settings driven by LocalValidatorFactoryBean's properties
 */
protected void postProcessConfiguration(Configuration<?> configuration) {
}