@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRole.class) .property("roleName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .constraint(new PatternDef().regexp("^(?!root$).+$").message( "may not be root")) .property("roleCode", ElementType.FIELD) .constraint(new NotNullDef()).constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRoute.class) .property("routeId", ElementType.FIELD) .constraint(new NotNullDef()) .property("name", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("url", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysMenu.class) .property("menuName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .property("menuPath", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")) .property("parentId", ElementType.FIELD) .constraint(new NotNullDef()) .property("icon", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysUser.class) .property("userId", ElementType.FIELD) .constraint(new NotNullDef()) .property("username", ElementType.FIELD) .constraint(new SizeDef().max(16).min(3)) .constraint(new PatternDef().regexp("[a-zA-Z][0-9a-zA-Z_]*") .message("{msg.error.validation.username.pattern}")) .property("password", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .property("fullName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("email", ElementType.FIELD).constraint(new EmailDef()) .constraint(new SizeDef().max(64)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(SysRole.class).property("roleId", ElementType.FIELD) .constraint(new NotNullDef()) .property("roleName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new PatternDef().regexp("^(?!root$).+$").message("may not be root")) .property("roleCode", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysUser.class) .property("username", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(16).min(3)) .constraint(new PatternDef().regexp("[a-zA-Z][0-9a-zA-Z_]*") .message("{msg.error.validation.username.pattern}")) .property("password", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("fullName", ElementType.FIELD) .constraint(new NotNullDef()).constraint(new SizeDef().max(32)) .property("email", ElementType.FIELD) .constraint(new SizeDef().max(64)).constraint(new EmailDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(ChangePasswordVo.class) .property("oldpassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("newpassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()) .property("retypepassword", ElementType.FIELD) .constraint(new SizeDef().max(16).min(6)) .constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysMenu.class) .property("menuId", ElementType.FIELD) .constraint(new NotNullDef()) .property("menuName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("menuPath", ElementType.FIELD) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")) .property("icon", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysRoute.class) .property("name", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .property("url", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(128)) .constraint(new PatternDef() .regexp("(/:?[0-9a-zA-Z_]+)*") .message("{msg.error.validation.routeUrl.pattern}")); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping.type(SysJob.class).property("jobId", ElementType.FIELD) .constraint(new NotNullDef()) .property("clazzName", ElementType.FIELD).constraint(new NullDef()) .property("jobName", ElementType.FIELD) .constraint(new SizeDef().max(32)) .property("cron", ElementType.FIELD) .constraint(new SizeDef().max(32)); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysJob.class) .property("clazzName", ElementType.FIELD) .constraint(new NotNullDef()) .constraint(new SizeDef().max(64)) .constraint(new PatternDef().regexp("[a-zA-Z.]+") .message("{msg.error.validation.className.pattern}")) .property("jobName", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()) .property("cron", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override public Validator createValidator() { HibernateValidatorConfiguration configuration = Validation.byProvider( HibernateValidator.class).configure(); ConstraintMapping constraintMapping = configuration.createConstraintMapping(); constraintMapping .type(SysDict.class) .property("dictCode", ElementType.FIELD) .constraint(new SizeDef().max(32)) .constraint(new NotNullDef()) .constraint(new PatternDef().regexp("[0-9a-zA-Z_]+").message( "{msg.error.validation.dictCode.pattern}")) .property("dictName", ElementType.FIELD) .constraint(new SizeDef().max(32)).constraint(new NotNullDef()); return configuration.addMapping(constraintMapping).buildValidatorFactory() .getValidator(); }
@Override protected void postProcessConfiguration(Configuration<?> configuration) { super.postProcessConfiguration(configuration); HibernateValidatorConfiguration hibernateConfiguration = (HibernateValidatorConfiguration) configuration; ConstraintMapping constraintMapping = hibernateConfiguration.createConstraintMapping(); validators.forEach(validator -> addConstraint(constraintMapping, validator)); hibernateConfiguration.addMapping(constraintMapping); hibernateValidatorConfigurationStrategy.accept(hibernateConfiguration); }
private void addConstraint(ConstraintMapping mapping, ConstraintValidator validator) { Stream.of(validator.getClass().getGenericInterfaces()) .filter(genericInterface -> genericInterface instanceof ParameterizedType) .map(genericInterface -> (ParameterizedType) genericInterface) .filter(genericInterface -> isConstraintValidator(genericInterface.getRawType())) .forEach(constraintValidatorInterface -> { @SuppressWarnings("unchecked") Class<? extends Annotation> a = (Class<? extends Annotation>) constraintValidatorInterface.getActualTypeArguments()[0]; mapping.constraintDefinition(a).validatedBy(getValidatorClass(validator)); }); }
public static HibernateValidatorConfiguration getPolicyTriggerRange(HibernateValidatorConfiguration config) { Map<String, Map<String, Map<String, Map<String, String>>>> mulit_range = Constants.getTriggerRangeByTriggerType(); logger.debug("mulit trigger range: " + mulit_range.toString()); for (String class_type : mulit_range.keySet()){ Map<String, Map<String, Map<String, String>>> range = mulit_range.get(class_type); Class<?> class_obj=null; if (class_type.equals("trigger_Memory")) //only one metricType supported now class_obj = PolicyTrigger.class; for (String key : range.keySet()) { Map<String, Map<String, String>> value = range.get(key); for(String value_key : value.keySet()){ Map<String, String> value_item = value.get(value_key); ConstraintMapping mapping = config.createConstraintMapping(); if (value_key.endsWith("Min")){ mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new MinDef().value(Integer.parseInt(value_item.get("value"))).message(value_item.get("message"))); } else if(value_key.endsWith("Max")){ mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new MaxDef().value(Integer.parseInt(value_item.get("value"))).message(value_item.get("message"))); } else if(value_key.endsWith("NotNull")){ mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new NotNullDef().message(value_item.get("message"))); } config.addMapping( mapping ); } } } return config; }
@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(); }
/** * initialize constraints of bean descriptor * * @param descriptor */ @SuppressWarnings("rawtypes") protected void initialize(IBeanDescriptor descriptor) { if (logger.isDebugEnabled()) { logger.debug("Initialize bean descriptor [" + descriptor + "]."); } ConstraintMapping mapping = getConfiguration().createConstraintMapping(); TypeConstraintMappingContext context = mapping.type(descriptor.getBeanClass()); this.generateTypeLevelConstraints(descriptor, context); this.generatePropertyLevelConstraints(descriptor, context); getConfiguration().addMapping(mapping); }
@Override public Validator getValidator(ConstraintMapping constraintMapping) { checkNotNullArgument(constraintMapping); return getValidator(constraintMapping, null); }
@Override public Validator getValidator(ConstraintMapping constraintMapping) { return new ValidatorStub(); }
@Override public Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options) { return new ValidatorStub(); }
@Override @Context public void run(OdsConfig configuration, Environment environment) { Injector injector = Guice.createInjector( new MonitoringModule(environment.metrics()), new ConfigModule(configuration), new ProcessorModule(), new DbModule(configuration.getCouchDb()), new NotificationsModule(), new DataModule(), new AuthModule(configuration.getAuth())); // start data grabbing environment.lifecycle().manage(injector.getInstance(DataSourceManager.class)); environment.jersey().getResourceConfig().register(MultiPartFeature.class); environment.jersey().getResourceConfig().register(injector.getInstance(AuthBinder.class)); environment.jersey().register(injector.getInstance(DataSourceApi.class)); environment.jersey().register(injector.getInstance(DataApi.class)); environment.jersey().register(injector.getInstance(ProcessorChainApi.class)); environment.jersey().register(injector.getInstance(DataViewApi.class)); environment.jersey().register(injector.getInstance(NotificationApi.class)); environment.jersey().register(injector.getInstance(PluginApi.class)); environment.jersey().register(injector.getInstance(ProcessorSpecificationApi.class)); environment.jersey().register(injector.getInstance(VersionApi.class)); environment.jersey().register(injector.getInstance(UserApi.class)); environment.jersey().register(PropertyFilteringMessageBodyWriter.class); environment.jersey().register(new DbExceptionMapper()); environment.jersey().register(new JsonExceptionMapper()); environment.jersey().register(new UnauthorizedExceptionMapper()); environment.jersey().register(new NotFoundExceptionMapper()); // setup users setupDefaultUsers(injector.getInstance(UserManager.class), configuration.getAuth().getUsers()); // setup health checks environment.healthChecks().register(DbHealthCheck.class.getSimpleName(), injector.getInstance(DbHealthCheck.class)); environment.healthChecks().register(PegelOnlineHealthCheck.class.getSimpleName(), injector.getInstance(PegelOnlineHealthCheck.class)); environment.healthChecks().register(DataSourceHealthCheck.class.getSimpleName(), injector.getInstance(DataSourceHealthCheck.class)); environment.healthChecks().register(FilterChainHealthCheck.class.getSimpleName(), injector.getInstance(FilterChainHealthCheck.class)); environment.healthChecks().register(DataHealthCheck.class.getSimpleName(), injector.getInstance(DataHealthCheck.class)); environment.healthChecks().register(CepsClientHealthCheck.class.getSimpleName(), injector.getInstance(CepsClientHealthCheck.class)); // configure administration DropwizardResourceConfig jerseyConfig = new DropwizardResourceConfig(environment.metrics()); JerseyContainerHolder jerseyContainerHolder = new JerseyContainerHolder(new ServletContainer(jerseyConfig)); jerseyConfig.register(injector.getInstance(AdminFilterChainApi.class)); environment.admin().addServlet("admin resources", jerseyContainerHolder.getContainer()).addMapping("/admin/*"); // setup validation for external classes HibernateValidatorConfiguration validatorContext = Validation.byProvider(HibernateValidator.class).configure(); ConstraintMapping mapping = validatorContext.createConstraintMapping(); mapping.type(ProcessorReferenceChainDescription.class).constraint(new GenericConstraintDef<>(ValidChainReference.class)); // setup Guice DI for hibernate validator environment.setValidator(validatorContext.addMapping(mapping) .constraintValidatorFactory(new GuiceConstraintValidatorFactory(injector)) .buildValidatorFactory() .getValidator()); }
/** * Get validator with custom constraint mapping and current locale if there is current UserSession or with default locale. * * @param constraintMapping constraint mapping * @return validator */ Validator getValidator(ConstraintMapping constraintMapping);
/** * Get validator with custom constraint mapping and additional validation options. * * @param constraintMapping constraint mapping * @param options options * @return validator */ Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options);