/** * Post-process the given message code, built by this resolver. * <p> * The default implementation applies the specified prefix, if any. * @param code the message code as built by this resolver * @return the final message code to be returned * @see #setPrefix */ protected String postProcessMessageCode(String code) { return getPrefix() + code; }
/** * An alternative constructor that creates an instance of {@link BeanValidationHintResolver}. */ public ValidationHelper(Object model, RequestContext requestContext, String eventId, String modelName, ExpressionParser expressionParser, MessageCodesResolver messageCodesResolver, MappingResults mappingResults) { this(model, requestContext, eventId, modelName, expressionParser, messageCodesResolver, mappingResults, new BeanValidationHintResolver()); }
/** * Invoke the validators available by convention. */ public void validate() { if (this.validator != null) { invokeValidatorDefaultValidateMethod(this.validator); } invokeModelValidationMethod(model); Object modelValidator = getModelValidator(); if (modelValidator != null) { invokeModelValidator(modelValidator); } }
private void validate(Object model, TransitionDefinition transition) { if (logger.isDebugEnabled()) { logger.debug("Validating model"); } ValidationHelper helper = new ValidationHelper(model, requestContext, eventId, getModelExpression() .getExpressionString(), expressionParser, messageCodesResolver, mappingResults, validationHintResolver); helper.setValidator(this.validator); helper.validate(); }
private void invokeModelValidationMethod(Object model) { invokeValidateMethodForCurrentState(model); invokeDefaultValidateMethod(model); }
private void invokeModelValidator(Object validator) { invokeValidatorValidateMethodForCurrentState(validator); invokeValidatorDefaultValidateMethod(validator); }
private boolean invokeValidatorValidateMethodForCurrentState(Object validator) { String methodName = "validate" + StringUtils.capitalize(requestContext.getCurrentState().getId()); Method validateMethod = findValidationMethod(model, validator, methodName, ValidationContext.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { new DefaultValidationContext(requestContext, eventId, mappingResults)); return true; validateMethod = findValidationMethod(model, validator, methodName, Errors.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { validateMethod = findValidationMethod(model, validator, methodName, MessageContext.class); if (validateMethod != null) { ReflectionUtils.invokeMethod(validateMethod, validator,
hint = "javax.validation.groups.Default"; Class<?> resolvedHint = toClass(hint); if ((resolvedHint == null) && (model != null)) { resolvedHint = findInnerClass(model.getClass(), StringUtils.capitalize(hint)); resolvedHint = handleUnresolvedHint(model, flowId, stateId, hint);
/** * Build the code list for the given code and field: an object/field-specific code, a field-specific code, a plain * error code. * <p> * Arrays, Lists and Maps are resolved both for specific elements and the whole collection. * <p> * See the {@link DefaultMessageCodesResolver class level Javadoc} for details on the generated codes. * @return the list of codes */ public String[] resolveMessageCodes(String errorCode, String objectName, String field, Class<?> fieldType) { List<String> codeList = new ArrayList<>(); List<String> fieldList = new ArrayList<>(); buildFieldList(field, fieldList); for (String fieldInList : fieldList) { codeList.add(postProcessMessageCode(objectName + CODE_SEPARATOR + fieldInList + CODE_SEPARATOR + errorCode)); } int dotIndex = field.lastIndexOf('.'); if (dotIndex != -1) { buildFieldList(field.substring(dotIndex + 1), fieldList); } for (String fieldInList : fieldList) { codeList.add(postProcessMessageCode(fieldInList + CODE_SEPARATOR + errorCode)); } if (fieldType != null) { codeList.add(postProcessMessageCode(fieldType.getName() + CODE_SEPARATOR + errorCode)); } codeList.add(postProcessMessageCode(errorCode)); return StringUtils.toStringArray(codeList); }
private boolean invokeDefaultValidateMethod(Object model) { // preferred Method validateMethod = ReflectionUtils.findMethod(model.getClass(), "validate", ValidationContext.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { logger.debug("Invoking default model validation method 'validate(ValidationContext)'"); } ReflectionUtils.invokeMethod(validateMethod, model, new DefaultValidationContext(requestContext, eventId, mappingResults)); return true; } // mvc 2 compatibility only validateMethod = ReflectionUtils.findMethod(model.getClass(), "validate", Errors.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { logger.debug("Invoking default model validation method 'validate(Errors)'"); } MessageContextErrors errors = new MessageContextErrors(requestContext.getMessageContext(), modelName, model, expressionParser, messageCodesResolver, mappingResults); ReflectionUtils.invokeMethod(validateMethod, model, errors); return true; } return false; }
private MappingResult getMappingResult(String property) { if (mappingResults != null) { List<MappingResult> results = mappingResults.getResults(new PropertyMappingResult(property)); if (!results.isEmpty()) { return results.get(0); } } return null; }
public void testResolveFieldMessageCodesWithPrefix() { messageCodesResolver.setPrefix("validation."); String[] codes = messageCodesResolver.resolveMessageCodes("required", "testBean", "foo", String.class); assertEquals(4, codes.length); assertEquals("validation.testBean.foo.required", codes[0]); assertEquals("validation.foo.required", codes[1]); assertEquals("validation.java.lang.String.required", codes[2]); assertEquals("validation.required", codes[3]); }
public String[] resolveMessageCodes(String errorCode, String objectName) { return new String[] { postProcessMessageCode(objectName + CODE_SEPARATOR + errorCode), postProcessMessageCode(errorCode) }; }
private Class<?> findInnerClass(Class<?> targetClass, String hint) { try { return Class.forName(targetClass.getName() + "$" + hint); } catch (ClassNotFoundException e) { Class<?> superClass = targetClass.getSuperclass(); if (superClass != null) { return findInnerClass(superClass, hint); } } return null; }
public Object getUserValue(String property) { MappingResult result = getMappingResult(property); return result != null ? result.getOriginalValue() : null; }
this.messageCodesResolver = messageCodesResolver; this.mappingResults = mappingResults; this.validationHints = initValidationHints(model, requestContext, eventId, hintResolver);
public void testResolveFieldMessageCodesKeyedField() { String[] codes = messageCodesResolver.resolveMessageCodes("required", "testBean", "foo[0]", String.class); assertEquals(6, codes.length); assertEquals("testBean.foo[0].required", codes[0]); assertEquals("testBean.foo.required", codes[1]); assertEquals("foo[0].required", codes[2]); assertEquals("foo.required", codes[3]); assertEquals("java.lang.String.required", codes[4]); assertEquals("required", codes[5]); }
if (hintsValue instanceof String) { String[] hints = StringUtils.commaDelimitedListToStringArray((String) hintsValue); return hintResolver.resolveValidationHints(model, flowId, stateId, hints);
Method validateMethod = findValidationMethod(model, validator, "validate", ValidationContext.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { new DefaultValidationContext(requestContext, eventId, mappingResults)); return true; validateMethod = findValidationMethod(model, validator, "validate", Errors.class); if (validateMethod != null) { if (logger.isDebugEnabled()) {
private boolean invokeValidateMethodForCurrentState(Object model) { String methodName = "validate" + StringUtils.capitalize(requestContext.getCurrentState().getId()); // preferred Method validateMethod = ReflectionUtils.findMethod(model.getClass(), methodName, ValidationContext.class); if (validateMethod != null) { if (logger.isDebugEnabled()) { logger.debug("Invoking current state model validation method '" + methodName + "(ValidationContext)'"); } ReflectionUtils.invokeMethod(validateMethod, model, new DefaultValidationContext(requestContext, eventId, mappingResults)); return true; } // web flow 2.0.3 or < compatibility only validateMethod = ReflectionUtils.findMethod(model.getClass(), methodName, MessageContext.class); if (validateMethod != null) { ReflectionUtils.invokeMethod(validateMethod, model, requestContext.getMessageContext()); return true; } // mvc 2 compatibility only validateMethod = ReflectionUtils.findMethod(model.getClass(), methodName, Errors.class); if (validateMethod != null) { MessageContextErrors errors = new MessageContextErrors(requestContext.getMessageContext(), modelName, model, expressionParser, messageCodesResolver, mappingResults); if (logger.isDebugEnabled()) { logger.debug("Invoking current state model validation method '" + methodName + "(Errors)'"); } ReflectionUtils.invokeMethod(validateMethod, model, errors); return true; } return false; }