Class AnnotationValidationInterceptor

java.lang.Object
org.apache.struts2.interceptor.AbstractInterceptor
com.opensymphony.xwork2.interceptor.AbstractInterceptor
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
name.matthewgreet.strutscommons.interceptor.AnnotationValidationInterceptor
All Implemented Interfaces:
com.opensymphony.xwork2.interceptor.ConditionalInterceptor, com.opensymphony.xwork2.interceptor.Interceptor, Serializable, org.apache.struts2.interceptor.ConditionalInterceptor, org.apache.struts2.interceptor.Interceptor

public class AnnotationValidationInterceptor extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor

Struts 2 Interceptor for parsing annotated form fields from strings to another data type and writes error messages where data type conversion fails. If the Action implements ModelDriven, the model is recognised as the form, not the action. Struts Actions are expected to implement ValidationAware and, if message keys are used, implement TextProvider.

This differs from the standard ConversionErrorInterceptor by allowing writing to the general error list, not just field errors, and, if a message key is used, using the Action's text provider directly, not going through the Value Stack.

Interceptor parameters:

  • None

Extending the interceptor:

Various points of the core algorithm can be overridden to customize behaviour. See javadoc of protected methods.

Example code:

 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="annotationValidation"/>
     <interceptor-ref name="validation"/>
     <result name="success">good_result.ftl</result>
 </action>
 
 
See Also:
  • Constructor Details

    • AnnotationValidationInterceptor

      public AnnotationValidationInterceptor()
  • Method Details

    • getProperties

      public static Collection<Field> getProperties(Class<?> type)
      Returns all member fields of a class, whether directly declared by the class or inherited.
    • adjust

      protected String adjust(Object form, Field formField, Annotation annotation, Adjuster<?> adjuster, String fieldValue) throws Exception
      Throws:
      IllegalAccessException
      IllegalArgumentException
      Exception
    • checkCollectionConversionMessage

      protected <T> void checkCollectionConversionMessage(Field unconvertedField, Annotation annotation, CollectionConverter<?,T> collectionConverter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider)
      Checks if conversion results means a message must be written and writes appropriate type if needed.
    • checkCollectionConversionRecipientDataType

      protected boolean checkCollectionConversionRecipientDataType(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass)
      Returns whether converting validator can convert form field value to the recipient field's type.
    • checkCollectionPostConversionMessage

      protected <T> void checkCollectionPostConversionMessage(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?,T> collectionPostConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider)
      Checks if conversion results means a message must be written and writes appropriate type if needed.
    • checkCollectionPostConversionRecipientDataType

      protected boolean checkCollectionPostConversionRecipientDataType(Annotation annotation, Field recipientField, Class<?> recipientClass)
      Returns whether post conversion validator can use the recipient field's type.
    • checkConversionMessage

      protected <T> void checkConversionMessage(Field unconvertedField, Annotation annotation, Converter<?,T> converter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider)
      Checks if conversion results means a message must be written and writes appropriate type if needed.
    • checkConversionRecipientDataType

      protected boolean checkConversionRecipientDataType(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass)
      Returns whether converting validator can convert form field value to the recipient field's type.
    • checkNonConversionMessage

      protected void checkNonConversionMessage(Field unconvertedField, Annotation annotation, NonConversionValidator<?> validator, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider)
      Checks if non-conversion validation results means a message must be written and writes appropriate type if needed.
    • checkPostConversionRecipientDataType

      protected boolean checkPostConversionRecipientDataType(Annotation annotation, Field recipientField, Class<?> recipientClass)
      Returns whether post conversion validator can use the recipient field's type.
    • checkPostConversionMessage

      protected <T> void checkPostConversionMessage(Field unconvertedField, Annotation annotation, PostConversionValidator<?,T> postConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider)
      Checks if conversion results means a message must be written and writes appropriate type if needed.
    • collectionConvert

      protected <T> AnnotationValidationInterceptor.ConversionFieldResult<T> collectionConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> collectionConversionAnnotationEntry, boolean modelDriven)
      Converts form field value and returns result, or null if recipient field missing or compatibility checks fail.
    • collectionPostConvert

      protected <T> ValidationResult collectionPostConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, ConversionResult<T> conversionResult, Collection<T> parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) throws Exception
      Return validation result of converted form field value, or null if compatibility checks fail.
      Throws:
      Exception
    • convert

      protected <T> AnnotationValidationInterceptor.ConversionFieldResult<T> convert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> conversionAnnotationEntry, boolean modelDriven)
      Converts form field value and returns result, or null if recipient field missing or compatibility checks fail.
    • doIntercept

      protected String doIntercept(com.opensymphony.xwork2.ActionInvocation invocation) throws Exception
      Specified by:
      doIntercept in class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
      Throws:
      Exception
    • getAnnotationUsage

      protected <T> InterceptorCommonLibrary.ConfiguredPolicy<T> getAnnotationUsage(Field unconvertedField, Annotation annotation) throws Exception
      Returns validator (and formatter) that processes an annotated form field, or result of NA type if not recognised.
      Throws:
      Exception
    • getCollectionPostConversionShortCircuit

      protected <T> boolean getCollectionPostConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, Collection<T> parsedValue)
      Returns whether a rejection by the non-conversion validator should stop further validation.
    • getDefaultRecipientName

      protected String getDefaultRecipientName(Field unconvertedField, String fieldName)
      Returns the default field name to receive a converted form field.
    • getMessage

      protected String getMessage(com.opensymphony.xwork2.TextProvider textProvider, String messageKey, String message)
      Returns message to use, extracted from text provider if required. textProvider can be null if Action does not implement it.
    • getNonConversionShortCircuit

      protected boolean getNonConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String formValue)
      Returns whether a rejection by the non-conversion validator should stop further validation.
    • getPostConversionShortCircuit

      protected <T> boolean getPostConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, T parsedValue)
      Returns whether a rejection by the non-conversion validator should stop further validation.
    • getRecipientField

      protected Field getRecipientField(Field unconvertedField, Annotation annotation, Collection<Field> fields, String recipientFieldName)
      Finds field to receive converted value from fields found in the form, or null if not found.
    • makeCollectionForRecipient

      protected <T> Collection<T> makeCollectionForRecipient(Class<?> recipientFieldClass)
      Returns an empty collection to set the recipient field and will accept converted values.
    • postConvert

      protected <T> ValidationResult postConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, ConversionResult<T> conversionResult, T parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) throws Exception
      Return validation result of converted form field value, or null if compatibility checks fail.
      Throws:
      Exception
    • validateCollectionConversion

      protected <T> ConversionResult<T> validateCollectionConversion(Field unconvertedField, Annotation annotation, CollectionConverter<?,T> collectionConverter, String formValue, Field recipientField, Class<T> recipientClass) throws Exception
      Calls validator to convert string form field value to recipient field of expected data type.
      Throws:
      Exception
    • validateCollectionPostConversion

      protected <T> ValidationResult validateCollectionPostConversion(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?,T> collectionPostConversionValidator, Collection<T> convertedValue, Field recipientField, Class<?> recipientClass) throws Exception
      Calls validator to check a converted form field.
      Throws:
      Exception
    • validateConversion

      protected <T> ConversionResult<T> validateConversion(Field unconvertedField, Annotation annotation, Converter<?,T> converter, String fieldValue, Field recipientField, Class<T> recipientClass) throws Exception
      Calls validator to convert string form field value to recipient field of expected data type.
      Throws:
      Exception
    • validateNonConversion

      protected ValidationResult validateNonConversion(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String fieldValue) throws Exception
      Calls validator to check a form field before any conversion.
      Throws:
      Exception
    • validatePostConversion

      protected <T> ValidationResult validatePostConversion(Field unconvertedField, Annotation annotation, PostConversionValidator<?,T> postConversionValidator, T convertedValue, Field recipientField, Class<T> recipientClass) throws Exception
      Calls validator to check a converted form field.
      Throws:
      Exception
    • writeFieldError

      protected void writeFieldError(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String fieldName, String message)
      Writes message to Action's field errors for field. ValidationAware can be null if Action does not implement it.
    • writeGeneralError

      protected void writeGeneralError(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message)
      Writes message to Action's action errors. ValidationAware can be null if Action does not implement it.
    • writeInfoMessage

      protected void writeInfoMessage(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message)
      Writes message to Action's action messages. ValidationAware can be null if Action does not implement it.
    • writeWarningMessage

      protected void writeWarningMessage(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message)
      Writes message to Action's action warnings. ValidationAware can be null if Action does not implement it.