@Test public void customValidation() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); instance.setValidator(new Validator() { @Override public boolean supports(Class<?> clazz) { return String.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { String value = (String) target; if ("failure".equals(value)) { errors.reject("not a valid value"); } } }); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "payloadValidation", String.class); thrown.expect(MethodArgumentNotValidException.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build()); }
@Autowired public SendController(final MessageSenderService messageService, final @Qualifier("mvcValidator") Validator validator, final SecureTransferConfiguration config) { this.messageService = messageService; this.validator = validator; this.config = config; }
/** * Registreer de specifieke validators. * @param validators validators */ public void setValidators(final Validator[] validators) { for (final Validator validator : validators) { if (!(validator instanceof GlobalValidator)) { this.validators.add(validator); } } }
@Override public boolean supports(final Class<?> clazz) { for (final Validator validator : validators) { if (validator.supports(clazz)) { return true; } } return false; }
@Override public void validate(final Object target, final Errors errors) { for (final Validator validator : validators) { if (validator.supports(target.getClass())) { validator.validate(target, errors); } } }
public void validate(Object target, Errors errors) { Set<ConstraintViolation<Object>> constraintViolations = beanValidator.validate(target); for (ConstraintViolation<Object> constraintViolation : constraintViolations) { String propertyPath = constraintViolation.getPropertyPath().toString(); String message = constraintViolation.getMessage(); errors.rejectValue(propertyPath, "", message); } for(Validator validator: springValidators) { validator.validate(target, errors); } }
@Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource); return validator; }
@Override public Validator getValidator() { LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); localValidatorFactoryBean.setProviderClass(org.hibernate.validator.HibernateValidator.class); localValidatorFactoryBean.setValidationMessageSource(messageSource()); return localValidatorFactoryBean; }
private Validator[] supportedValidatorsFor(Class<?> clazz) { final Set<Validator> supportedValidators = new HashSet<>(); for (Validator validator : validators) { if (validator.supports(clazz)) { supportedValidators.add(validator); } } return supportedValidators.toArray(new Validator[supportedValidators.size()]); }
@Test public void simpValidatorCustom() { final Validator validator = mock(Validator.class); AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() { @Override public Validator getValidator() { return validator; } }; assertSame(validator, config.simpValidator()); }
@Test public void simpValidatorInjected() { SimpAnnotationMethodMessageHandler messageHandler = this.simpleBrokerContext.getBean(SimpAnnotationMethodMessageHandler.class); assertThat(messageHandler.getValidator(), Matchers.notNullValue(Validator.class)); }
private Validator testValidator() { return new Validator() { @Override public boolean supports(Class<?> clazz) { return String.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { String value = (String) target; if ("invalidValue".equals(value)) { errors.reject("invalid value"); } } }; }
/** * Bind request parameters onto the given command bean * @param request request from which parameters will be bound * @param command command object, that must be a JavaBean * @throws Exception in case of invalid state or arguments */ protected void bind(HttpServletRequest request, Object command) throws Exception { logger.debug("Binding request parameters onto MultiActionController command"); ServletRequestDataBinder binder = createBinder(request, command); binder.bind(request); if (this.validators != null) { for (Validator validator : this.validators) { if (validator.supports(command.getClass())) { ValidationUtils.invokeValidator(validator, command, binder.getBindingResult()); } } } binder.closeNoCatch(); }
@Override public Validator getValidator() { List<Validator> candidates = new ArrayList<Validator>(); for (WebMvcConfigurer configurer : this.delegates) { Validator validator = configurer.getValidator(); if (validator != null) { candidates.add(validator); } } return selectSingleInstance(candidates, Validator.class); }
@Override public Validator getValidator() { return new Validator() { @Override public void validate(Object target, Errors errors) { errors.reject("invalid"); } @Override public boolean supports(Class<?> clazz) { return true; } }; }
@Test public void requestMappingHandlerAdapter() throws Exception { ApplicationContext context = initContext(WebConfig.class); RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class); List<HttpMessageConverter<?>> converters = adapter.getMessageConverters(); assertEquals(9, converters.size()); for(HttpMessageConverter<?> converter : converters) { if (converter instanceof AbstractJackson2HttpMessageConverter) { ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper(); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); if (converter instanceof MappingJackson2XmlHttpMessageConverter) { assertEquals(XmlMapper.class, objectMapper.getClass()); } } } ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer(); assertNotNull(initializer); ConversionService conversionService = initializer.getConversionService(); assertNotNull(conversionService); assertTrue(conversionService instanceof FormattingConversionService); Validator validator = initializer.getValidator(); assertNotNull(validator); assertTrue(validator instanceof LocalValidatorFactoryBean); DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter); @SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice"); assertEquals(2, bodyAdvice.size()); assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass()); assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass()); }
private Validator testValidator(final String invalidValue) { return new Validator() { @Override public boolean supports(Class<?> clazz) { return String.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { String value = (String) target; if (invalidValue.equals(value)) { errors.reject("not a valid value"); } } }; }
@Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource()); return validator; }
@Override public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) { Validator validator = validator(); //bean validation always before save and create validatingListener.addValidator("beforeCreate", validator); validatingListener.addValidator("beforeSave", validator); }
private void freeLocalValidator() { try { Validator validator = this.localValidator; this.localValidator = null; if (validator != null) { ((DisposableBean) validator).destroy(); } } catch (Exception ex) { throw new IllegalStateException(ex); } }
private Validator determineValidator(Object bean) { Validator validator = getValidator(); boolean supportsBean = (validator != null && validator.supports(bean.getClass())); if (ClassUtils.isAssignable(Validator.class, bean.getClass())) { if (supportsBean) { return new ChainingValidator(validator, (Validator) bean); } return (Validator) bean; } return (supportsBean ? validator : null); }
private Validator getValidator() { if (this.validator != null) { return this.validator; } if (this.localValidator == null && isJsr303Present()) { this.localValidator = new LocalValidatorFactory() .run(this.applicationContext); } return this.localValidator; }
@Override public boolean supports(Class<?> clazz) { for (Validator validator : this.validators) { if (validator.supports(clazz)) { return true; } } return false; }
@Override public void validate(Object target, Errors errors) { for (Validator validator : this.validators) { if (validator.supports(target.getClass())) { validator.validate(target, errors); } } }
/** * Use a validator on a list of objects, correctly handling changes to the nestedPath * * @param path * @param validator * @param list * @param errors */ public static void invokeValidatorOnList(String path, Validator validator, List<?> list, Errors errors) { int i = 0; for (Object target : list) { try { errors.pushNestedPath(path + "[" + i + "]"); ValidationUtils.invokeValidator(validator, target, errors); } finally { errors.popNestedPath(); i++; } } }
@Override public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener eventListener) { Stream<Validator> stdValidators = Stream.of( analysisValidator, assayValidator, assayDataValidator, egaDacValidator, egaDacPolicyValidator, egaDatasetValidator, projectValidator, protocolValidator, sampleValidator, sampleGroupValidator, studyValidator, submissionValidator ); stdValidators.forEach(validator -> { eventListener.addValidator(BEFORE_CREATE, validator); eventListener.addValidator(BEFORE_SAVE, validator); }); eventListener.addValidator(BEFORE_SAVE, submissionStatusValidator); eventListener.addValidator(BEFORE_DELETE, submissionDeleteValidator); eventListener.addValidator(BEFORE_DELETE, submittableDeleteValidator); }
/** * Default constructor * * @param cs * @param validator */ public DataBinderMappingJackson2HttpMessageConverter(ConversionService cs, Validator validator) { super(); this.conversionService = cs; this.validator = validator; }
/** * Default constructor. */ @Autowired public Jackson2RequestMappingHandlerAdapter( ConversionService conversionService, Validator validator) { super(); this.conversionService = conversionService; this.validator = validator; }
/** * Register custom serialization and deserialization modules. * * @param conversionService * @param validator */ @Autowired public ConversionServiceObjectMapper( final ConversionService conversionService, final Validator validator) { // Register a module to add provider for custom bean deserializer registerModule(new ConversionServiceModule(conversionService)); // Register a module to add provider for custom bean serializer registerModule(new DataBinderModule()); }
private void checkForValidator() { PluginConfigurationValidator validatorAnnotation = this.configuration.getClass() .getAnnotation(PluginConfigurationValidator.class); if (validatorAnnotation != null) { Validator validator; try { validator = validatorAnnotation.validatorClass().getConstructor().newInstance(); } catch (Exception e) { throw new IllegalArgumentException("could not create validator for configuration", e); } this.dataBinder.setValidator(validator); this.validator = validator; } }
@Override public void afterPropertiesSet() throws Exception { if (this.propertySources == null) { this.propertySources = deducePropertySources(); } if (this.validator == null) { this.validator = getOptionalBean(VALIDATOR_BEAN_NAME, Validator.class); } if (this.conversionService == null) { this.conversionService = getOptionalBean( ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME, ConversionService.class); } }
/** * Set the Validator to apply after each binding step. */ public final void setValidator(Validator validator) { this.validator = validator; }
/** * Return the Validator to apply after each binding step, if any. */ public final Validator getValidator() { return this.validator; }
@Override public Validator getValidator() { return null; }