Save This Page
Home » cocoon-2.1.11-src » org.apache » cocoon » acting » [javadoc | source]
org.apache.cocoon.acting
abstract public class: AbstractValidatorAction [javadoc | source]
java.lang.Object
   org.apache.avalon.framework.logger.AbstractLogEnabled
      org.apache.cocoon.acting.AbstractAction
         org.apache.cocoon.acting.AbstractConfigurableAction
            org.apache.cocoon.acting.ConfigurableServiceableAction
               org.apache.cocoon.acting.AbstractComplementaryConfigurableAction
                  org.apache.cocoon.acting.AbstractValidatorAction

All Implemented Interfaces:
    org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.configuration.Configurable, Action

Direct Known Subclasses:
    SessionValidatorAction, FormValidatorAction, CookieValidatorAction, SessionFormAction

Abstract implementation of action that needs to perform validation of parameters (from session, from request, etc.). All `validator' actions share the same description xml file. In such file every parameter is described via its name, type and its constraints. One large description file can be used among all validator actions, because each action should explicitely specify which parameters to validate - through a sitemap parameter.

Variant 1

<map:act type="validator">
<parameter name="descriptor" value="context://descriptor.xml">
<parameter name="validate" value="username,password">
</map:act>

The list of parameters to be validated is specified as a comma separated list of their names. descriptor.xml can therefore be used among many various actions. If the list contains only of *, all parameters in the file will be validated.

Variant 2

<map:act type="validator">
<parameter name="descriptor" value="context://descriptor.xml">
<parameter name="constraint-set" value="is-logged-in">
</map:act>

The parameter "constraint-set" tells to take a given "constraint-set" from description file and test all parameters against given criteria. This variant is more powerful, more aspect oriented and more flexibile than the previous one, because it allows comparsion constructs, etc. See AbstractValidatorAction documentation.

For even more powerful validation, constraints can be grouped and used independently of the parameter name. If a validate element has a rule attribute, it uses the parameter with that name as a rule template and validates the parameter from the name attribute with that rule.

This action returns null when validation fails, otherwise it provides all validated parameters to the sitemap via {name} expression.

In addition a request attribute org.apache.cocoon.acting.FormValidatorAction.results contains the validation results in both cases and make it available to XSPs. The special parameter "*" contains either the validation result "OK", if all parameters were validated successfully, or "ERROR" otherwise. Mind you that redirections create new request objects and thus the result is not available for the target page.

<root>
<parameter name="username" type="string" nullable="no"/>
<parameter name="role" type="string" nullable="no"/>
<parameter name="oldpassword" type="string" nullable="no"/>
<parameter name="newpassword" type="string" nullable="no"/>
<parameter name="renewpassword" type="string" nullable="no"/>
<parameter name="id" type="long" nullable="no"/>
<parameter name="sallary" type="double" nullable="no"/>
<parameter name="theme" type="string" nullable="yes" default="dflt"/>
<constraint-set name="is-logged-in">
<validate name="username"/>
<validate name="role"/>
</constraint-set>

<constraint-set name="is-in-admin-role">
<validate name="username"/>
<validate name="role" equals-to="admin"/>
</constraint-set>

<constraint-set name="new-passwords-match">
<validate name="oldpassword"/>
<validate name="newpassword"/>
<validate name="renewpassword"
equals-to-param="newpass"/>
</constraint-set>

<constraint-set name="all">
<include name="is-logged-in"/>
<include name="is-in-admin-role"/>
<include name="new-passwords-match"/>
</constraint-set>
</root>

The types recognized by validator and their attributes

stringnullable="yes|no" default="str"
longnullable="yes|no" default="123123"
doublenullable="yes|no" default="0.5"

Default value takes place only when specified parameter is nullable and really is null or empty. Long numbers may be specified in decimal, hex or octal values as accepted by java.Lang.decode (String s).

Constraints

matches-regexPOSIX regular expression
min-lenpositive integer
max-lenpositive integer
minDouble / Long
maxDouble / Long

Constraints can be defined globally for a parameter and can be overridden by redefinition in a constraint-set. Thus if e.g. a database field can take at maximum 200 character, this property can be set globally.

Values in parameter arrays are validated individually and the worst error is reported back.

The attributes recognized in "constraint-set"

equals-to-paramparameter name
equals-tostring constant
Fields inherited from org.apache.cocoon.acting.ConfigurableServiceableAction:
manager
Fields inherited from org.apache.cocoon.acting.AbstractConfigurableAction:
settings
Fields inherited from org.apache.cocoon.acting.AbstractAction:
EMPTY_MAP
Method from org.apache.cocoon.acting.AbstractValidatorAction Summary:
act,   createMapOfParameters,   getDescriptor,   getSetOfParameterNamesFromSitemap,   indexConfiguration,   isDescriptorReloadable,   isStringEncoded,   resolveConstraints,   setResult,   validateParameter,   validateParameter,   validateSetOfParameters,   validateValue
Methods from org.apache.cocoon.acting.AbstractComplementaryConfigurableAction:
getConfiguration,   getConfiguration,   getConfiguration
Methods from org.apache.cocoon.acting.ConfigurableServiceableAction:
service
Methods from org.apache.cocoon.acting.AbstractConfigurableAction:
configure
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.cocoon.acting.AbstractValidatorAction Detail:
 public Map act(Redirector redirector,
    SourceResolver resolver,
    Map objectModel,
    String src,
    Parameters parameters) throws Exception 
 abstract protected HashMap createMapOfParameters(Map objectModel,
    Collection set)
    Reads parameter values for all parameters that are contained in the active constraint list. If a parameter has multiple values, all are stored in the resulting map.
 protected Configuration getDescriptor(SourceResolver resolver,
    Map objectModel,
    Parameters parameters) 
    Load the descriptor containing the constraints.
 protected Collection getSetOfParameterNamesFromSitemap(String valstr,
    Map desc) 
    Get list of params to be validated from sitemap parameter and isolates the parameter names from the comma separated list.
 protected Map indexConfiguration(Configuration[] descriptor) 
    Create an index map to an array of configurations by their name attribute. An empty array results in an empty map.
 protected boolean isDescriptorReloadable() 
    Checks the default setting for reloading the descriptor file.
 abstract boolean isStringEncoded()
    Are parameters encoded as strings?
 protected Collection resolveConstraints(String valsetstr,
    Map consets) 
    Recursively resolve constraint sets that may "include" other constraint sets and return a collection of all parameters to validate.
 protected Map setResult(Map objectModel,
    Map actionMap,
    Map resultMap,
    boolean allOK) 
    Add success indicator to resulting maps and clear actionMap if unsuccessful. Results are stored as request attributes.
 public ValidatorActionHelper validateParameter(String name,
    Configuration constraints,
    Map conf,
    Map params,
    boolean isString) 
    Try to validate given parameter.
 public ValidatorActionHelper validateParameter(String name,
    String rule,
    Configuration constraints,
    Map conf,
    Map params,
    boolean isString) 
    Try to validate given parameter.
 protected boolean validateSetOfParameters(Map desc,
    Map actionMap,
    Map resultMap,
    Collection set,
    Map params,
    boolean isString) 
    Validate all parameters in the set with the constraints contained in desc and the values from params. Validation details are in resultMap and successful validated parameters in resultMap.
 protected ValidatorActionHelper validateValue(String name,
    Configuration constraints,
    Configuration conf,
    Map params,
    boolean isString,
    String type) 
    Validate a single parameter value.