@Override public void afterPropertiesSet() { if (this.validatorFactory == null) { this.validatorFactory = Validation.buildDefaultValidatorFactory(); } ValidatorContext validatorContext = this.validatorFactory.usingContext(); MessageInterpolator targetInterpolator = this.messageInterpolator; if (targetInterpolator == null) { targetInterpolator = this.validatorFactory.getMessageInterpolator(); } validatorContext.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator)); if (this.traversableResolver != null) { validatorContext.traversableResolver(this.traversableResolver); } setTargetValidator(validatorContext.getValidator()); }
public static JsonNode parsePolicyEnable(String jsonString, HttpServletRequest httpServletRequest) throws JsonParseException, JsonMappingException, IOException{ List<String> violation_message = new ArrayList<String>(); ObjectNode result = new_mapper.createObjectNode(); result.put("valid", false); PolicyEnbale policyEnable = new_mapper.readValue(jsonString, PolicyEnbale.class); ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); Locale locale = LocaleUtil.getLocale(httpServletRequest); MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(vf.getMessageInterpolator(), locale); Validator validator = vf.usingContext().messageInterpolator(interpolator).getValidator(); Set<ConstraintViolation<PolicyEnbale>> set = validator.validate(policyEnable); if (set.size() > 0 ){ for (ConstraintViolation<PolicyEnbale> constraintViolation : set) { violation_message.add(constraintViolation.getMessage()); } result.set("violation_message", new_mapper.valueToTree(violation_message)); return result; } //additional data manipulation String new_json = policyEnable.transformInput(); result.put("valid", true); result.put("new_json", new_json); return result; }
public static JsonNode parseScalingHistory(String jsonString, HttpServletRequest httpServletRequest) throws JsonParseException, JsonMappingException, IOException{ List<String> violation_message = new ArrayList<String>(); ObjectNode result = new_mapper.createObjectNode(); result.put("valid", false); JavaType javaType = getCollectionType(ArrayList.class, ArrayList.class, HistoryData.class); new_mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); List<HistoryData> scalinghistory = (List<HistoryData>)new_mapper.readValue(jsonString, javaType); ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); Locale locale = LocaleUtil.getLocale(httpServletRequest); MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(vf.getMessageInterpolator(), locale); Validator validator = vf.usingContext().messageInterpolator(interpolator).getValidator(); Set<ConstraintViolation<List<HistoryData>>> set = validator.validate(scalinghistory); if (set.size() > 0 ){ for (ConstraintViolation<List<HistoryData>> constraintViolation : set) { violation_message.add(constraintViolation.getMessage()); } result.set("violation_message", new_mapper.valueToTree(violation_message)); return result; } //additional data manipulation String new_json = transformHistory(scalinghistory); result.put("valid", true); result.put("new_json", new_json); return result; }
/** * JAVADOC Method Level Comments */ @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testInterpolateStringContext() { Context context = mock(Context.class); ConstraintDescriptor cd = mock(ConstraintDescriptor.class); Map<String, Object> atts = new HashMap<String, Object>(); when(cd.getAttributes()).thenReturn(atts); when(context.getConstraintDescriptor()).thenReturn(cd); Foo foo = new Foo(); when(context.getValidatedValue()).thenReturn(foo); MessageInterpolator delegate = mock(MessageInterpolator.class); when(delegate.interpolate("message", context)).thenReturn("MEssAGE"); BeanMessageInterpolator interpolator = new BeanMessageInterpolator(delegate); interpolator.interpolate("message", context); assertEquals(foo, atts.get("0")); }
protected String resolve ( final String key, final Context context, final Locale locale ) { for ( final Resolver resolver : this.tracker.getTracked ().values () ) { final String result = resolver.resolve ( key, context, locale ); if ( result != null ) { return result; } } final MessageInterpolator fallback = this.fallback; if ( fallback == null ) { return null; } return fallback.interpolate ( String.format ( "{%s}", key ), context, locale ); }
private <T, V, A extends Annotation> ConstraintViolation<T> createConstraintViolation( final GwtValidationContext<T> context, final G object, final V value, final ConstraintDescriptorImpl<A> constraintDescriptor, final MessageAndPath messageAndPath) { final MessageInterpolator messageInterpolator = context.getMessageInterpolator(); final de.knightsoftnet.validators.client.impl.MessageInterpolatorContextImpl messageContext = new MessageInterpolatorContextImpl(constraintDescriptor, value); final String message = messageInterpolator.interpolate(messageAndPath.getMessage(), messageContext); return ConstraintViolationImpl.<T>builder() // .setConstraintDescriptor(constraintDescriptor) // .setInvalidValue(value) // .setLeafBean(object) // .setMessage(message) // .setMessageTemplate(messageAndPath.getMessage()) // .setPropertyPath(messageAndPath.getPath()) // .setRootBean(context.getRootBean()) // .setRootBeanClass(context.getRootBeanClass()) // .setElementType(constraintDescriptor.getElementType()) // .build(); }
/** * 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); }
public void afterPropertiesSet() { if (this.validatorFactory == null) { this.validatorFactory = Validation.buildDefaultValidatorFactory(); } ValidatorContext validatorContext = this.validatorFactory.usingContext(); MessageInterpolator targetInterpolator = this.messageInterpolator; if (targetInterpolator == null) { targetInterpolator = this.validatorFactory.getMessageInterpolator(); } validatorContext.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator)); if (this.traversableResolver != null) { validatorContext.traversableResolver(this.traversableResolver); } setTargetValidator(validatorContext.getValidator()); }
@Before public void setup() { ResourceBundle bundle = new SafeResourceBundle(ResourceBundle.getBundle("messages")); ValidatorFactory validatorFactory = javax.validation.Validation.buildDefaultValidatorFactory(); javax.validation.Validator bvalidator = validatorFactory.getValidator(); MessageInterpolator interpolator = validatorFactory.getMessageInterpolator(); Proxifier proxifier = new JavassistProxifier(); Messages messages = new Messages(); validator = new DefaultValidator(result, new DefaultValidationViewsFactory(result, proxifier, new DefaultReflectionProvider()), outjector, proxifier, bundle, bvalidator, interpolator, Locale.ENGLISH, messages); when(result.use(LogicResult.class)).thenReturn(logicResult); when(result.use(PageResult.class)).thenReturn(pageResult); when(logicResult.forwardTo(MyComponent.class)).thenReturn(instance); when(pageResult.of(MyComponent.class)).thenReturn(instance); }
@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); }
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; }
public static JsonNode parsePolicyOutput(String jsonString, Map<String, String> supplyment, 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 json: " + jsonString); 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 ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); 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; } //additional data manipulation policy = policy.transformSchedules(); String new_json = policy.transformOutput(supplyment, service_info); result.put("valid", true); result.put("new_json", new_json); return result; }
public static JsonNode parseMetrics(String jsonString, HttpServletRequest httpServletRequest) throws JsonParseException, JsonMappingException, IOException{ List<String> violation_message = new ArrayList<String>(); ObjectNode result = new_mapper.createObjectNode(); result.put("valid", false); //JavaType javaType = getCollectionType(ArrayList.class, HistoryData.class); //new_mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); logger.info("Received metrics: " + jsonString); Metrics metrics = new_mapper.readValue(jsonString, Metrics.class); ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); Locale locale = LocaleUtil.getLocale(httpServletRequest); MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(vf.getMessageInterpolator(), locale); Validator validator = vf.usingContext().messageInterpolator(interpolator).getValidator(); Set<ConstraintViolation<Metrics>> set = validator.validate(metrics); if (set.size() > 0 ){ for (ConstraintViolation<Metrics> constraintViolation : set) { violation_message.add(constraintViolation.getMessage()); } result.set("violation_message", new_mapper.valueToTree(violation_message)); return result; } //additional data manipulation String new_json = metrics.transformOutput(); result.put("valid", true); result.put("new_json", new_json); return result; }
public static ValidatorFactory buildValidatorFactory(boolean osgi, ValidationProviderResolver validationProviderResolver, MessageInterpolator messageInterpolator, TraversableResolver traversableResolver, ConstraintValidatorFactory constraintValidatorFactory) { ValidationProviderResolver resolvedValidationProviderResolver = resolveValidationProviderResolver(osgi, validationProviderResolver); GenericBootstrap bootstrap = Validation.byDefaultProvider(); if (resolvedValidationProviderResolver != null) { bootstrap.providerResolver(resolvedValidationProviderResolver); } Configuration<?> configuration = bootstrap.configure(); if (messageInterpolator != null) { configuration.messageInterpolator(messageInterpolator); } if (traversableResolver != null) { configuration.traversableResolver(traversableResolver); } if (constraintValidatorFactory != null) { configuration.constraintValidatorFactory(constraintValidatorFactory); } return configuration.buildValidatorFactory(); }
/** * JAVADOC Method Level Comments */ @SuppressWarnings("unchecked") @Test public void testInterpolateStringContextLocale() { Context context = mock(Context.class); @SuppressWarnings("rawtypes") ConstraintDescriptor cd = mock(ConstraintDescriptor.class); Map<String, Object> atts = new HashMap<String, Object>(); String[] properties = { "name", "value" }; atts.put("properties", properties); when(cd.getAttributes()).thenReturn(atts); when(context.getConstraintDescriptor()).thenReturn(cd); Foo foo = new Foo(); foo.setName("Name"); foo.setValue(200); when(context.getValidatedValue()).thenReturn(foo); MessageInterpolator delegate = mock(MessageInterpolator.class); when(delegate.interpolate("message", context, Locale.getDefault())).thenReturn("MEssAGE"); BeanMessageInterpolator interpolator = new BeanMessageInterpolator(delegate); interpolator.interpolate("message", context, Locale.getDefault()); assertEquals("Name", atts.get("0")); assertEquals("200", atts.get("1")); }
@Provides @Singleton protected MessageInterpolator messageInterpolator(RiseValidationMessageInterpolator interpolator) { return interpolator; // return new ResourceBundleMessageInterpolator( // resolver::getResourceBundle); }
private GwtValidationContext(final Class<T> rootBeanClass, final T rootBean, final BeanDescriptor beanDescriptor, final MessageInterpolator messageInterpolator, final TraversableResolver traversableResolver, final AbstractGwtValidator validator, final Set<Object> validatedObjects) { this.rootBeanClass = rootBeanClass; this.rootBean = rootBean; this.beanDescriptor = beanDescriptor; this.messageInterpolator = messageInterpolator; this.traversableResolver = traversableResolver; this.validator = validator; this.validatedObjects = new HashSet<>(validatedObjects); }
/** * A helper method that returns "bean level" validation errors, i.e. errors * that are not tied to a specific property/field. * * @return error messages from "bean level validation" */ public Collection<String> getBeanLevelValidationErrors() { Collection<String> errors = new ArrayList<>(); if (getConstraintViolations() != null) { for (final ConstraintViolation<T> constraintViolation : getConstraintViolations()) { final MessageInterpolator.Context context = new MessageInterpolator.Context() { @Override public ConstraintDescriptor<?> getConstraintDescriptor() { return constraintViolation.getConstraintDescriptor(); } @Override public Object getValidatedValue() { return constraintViolation.getInvalidValue(); } @Override public <T> T unwrap(Class<T> type) { throw new ValidationException(); } }; final String msg = getJavaxBeanValidatorFactory().getMessageInterpolator().interpolate( constraintViolation.getMessageTemplate(), context, getLocale()); errors.add(msg); } } if (getBasicConstraintViolations() != null) { for (Validator.InvalidValueException cv : getBasicConstraintViolations()) { errors.add(cv.getMessage()); } } return errors; }
/** * Creates the validator factory bean that Spring uses to * construct a Validator. * * @return a Validator Factory Bean */ @Bean public LocalValidatorFactoryBean validatorFactoryBean() { BeanConstraintValidatorFactory validatorFactory = ctx.getBean(BeanConstraintValidatorFactory.class); MessageInterpolator messageInterpolator = ctx.getBean(MessageInterpolator.class); LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); factoryBean.setMessageInterpolator(messageInterpolator); factoryBean.setConstraintValidatorFactory(validatorFactory); return factoryBean; }
@Inject public CejugValidator(Result result, ValidationViewsFactory factory, Outjector outjector, Proxifier proxifier, ResourceBundle bundle, Validator validator, MessageInterpolator interpolator, Locale locale) { super(result, factory, outjector, proxifier, bundle, validator, interpolator, locale); this.result = result; this.viewsFactory = factory; this.outjector = outjector; this.proxifier = proxifier; }
@Inject public DefaultValidator(Result result, ValidationViewsFactory factory, Outjector outjector, Proxifier proxifier, ResourceBundle bundle, javax.validation.Validator bvalidator, MessageInterpolator interpolator, Locale locale, Messages messages) { this.result = result; this.viewsFactory = factory; this.outjector = outjector; this.proxifier = proxifier; this.bundle = bundle; this.bvalidator = bvalidator; this.interpolator = interpolator; this.locale = locale; this.messages = messages; }
@Override public String interpolate(final String message, final MessageInterpolator.Context context) { Locale locale; if(FrameworkUtils.isFaces()) { locale = XSPContext.getXSPContext(FacesContext.getCurrentInstance()).getLocale(); } else { locale = Locale.getDefault(); } return interpolate(message, context, locale); }
@Bean public MessageInterpolator messageInterpolator() { return new SpringAwareMessageMessageInterpolator(); }
/** * Create a new LocaleContextMessageInterpolator, wrapping the given target interpolator. * @param targetInterpolator the target MessageInterpolator to wrap */ public LocaleContextMessageInterpolator(MessageInterpolator targetInterpolator) { Assert.notNull(targetInterpolator, "Target MessageInterpolator must not be null"); this.targetInterpolator = targetInterpolator; }
/** * Specify a custom MessageInterpolator to use for this Validator. */ public void setMessageInterpolator(MessageInterpolator messageInterpolator) { this.messageInterpolator = messageInterpolator; }
@Override public void afterPropertiesSet() { @SuppressWarnings({"rawtypes", "unchecked"}) Configuration<?> configuration = (this.providerClass != null ? Validation.byProvider(this.providerClass).configure() : Validation.byDefaultProvider().configure()); MessageInterpolator targetInterpolator = this.messageInterpolator; if (targetInterpolator == null) { targetInterpolator = configuration.getDefaultMessageInterpolator(); } configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator)); if (this.traversableResolver != null) { configuration.traversableResolver(this.traversableResolver); } ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory; if (targetConstraintValidatorFactory == null && this.applicationContext != null) { targetConstraintValidatorFactory = new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory()); } if (targetConstraintValidatorFactory != null) { configuration.constraintValidatorFactory(targetConstraintValidatorFactory); } if (this.parameterNameDiscoverer != null) { configureParameterNameProviderIfPossible(configuration); } if (this.mappingLocations != null) { for (Resource location : this.mappingLocations) { try { configuration.addMapping(location.getInputStream()); } catch (IOException ex) { throw new IllegalStateException("Cannot read mapping resource: " + location); } } } for (Map.Entry<String, String> entry : this.validationPropertyMap.entrySet()) { configuration.addProperty(entry.getKey(), entry.getValue()); } // Allow for custom post-processing before we actually build the ValidatorFactory. postProcessConfiguration(configuration); this.validatorFactory = configuration.buildValidatorFactory(); setTargetValidator(this.validatorFactory.getValidator()); }
@Override public MessageInterpolator getMessageInterpolator() { Assert.notNull(this.validatorFactory, "No target ValidatorFactory set"); return this.validatorFactory.getMessageInterpolator(); }
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) { return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource)); }
@Override public MessageInterpolator getMessageInterpolator() { throw new UnsupportedOperationException(); }
@Override public MinijaxValidationProvider messageInterpolator(final MessageInterpolator interpolator) { throw new UnsupportedOperationException(); }
@Override public MessageInterpolator getDefaultMessageInterpolator() { throw new UnsupportedOperationException(); }