Java Code Examples for javax.validation.ValidatorFactory#getMessageInterpolator()

The following examples show how to use javax.validation.ValidatorFactory#getMessageInterpolator() . 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: BeanValidation.java    From open-Autoscaler with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: BeanValidation.java    From open-Autoscaler with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: DefaultValidatorTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
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 5
Source File: BeanValidation.java    From open-Autoscaler with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: BeanValidation.java    From open-Autoscaler with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: BeanValidationProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void postConstruct() {
	ValidatorFactory validatorFactory = null;

	Iterator<ValidatorFactory> iterator = validatorFactoryInstance.iterator();

	if (iterator.hasNext()) {
		validatorFactory = iterator.next();
	}

	if (validatorFactory == null) {

		if (LOG.isWarnEnabled()) {
			LOG.warn("ValidatorFactory was not injected -- if using Hibernate " + "Validator, please include the " +
				"hibernate-validator-cdi dependency.");
		}

		try {
			validatorFactory = Validation.buildDefaultValidatorFactory();
		}
		catch (NoProviderFoundException npfe) {
			LOG.error(npfe.getMessage(), npfe);
		}
	}

	if (validatorFactory != null) {
		messageInterpolator = validatorFactory.getMessageInterpolator();

		if ((messageInterpolator == null) && LOG.isWarnEnabled()) {
			LOG.warn("Bean validation MessageInterpolator not available");
		}

		validator = validatorFactory.getValidator();

		if ((validator == null) && LOG.isWarnEnabled()) {
			LOG.warn("Bean validation validator not available");
		}
	}
}