/**
* Main invocation routine.
*/
public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src,
Parameters parameters) throws Exception {
Request req = ObjectModelHelper.getRequest(objectModel);
/* check request validity */
if (req == null) {
getLogger().debug ("No request object");
return null;
}
// read global parameter settings
boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
if (this.settings.containsKey("reloadable")) {
reloadable = Boolean.getBoolean((String) this.settings.get("reloadable"));
}
// read local settings
try {
Configuration conf = this.getConfiguration (
parameters.getParameter ("descriptor", (String) this.settings.get("descriptor")), resolver,
parameters.getParameterAsBoolean("reloadable", reloadable));
String valstr = parameters.getParameter ("validate", (String) settings.get("validate",""));
String valsetstr = parameters.getParameter ("validate-set", (String) settings.get("validate-set",""));
Configuration[] desc = conf.getChildren ("parameter");
Configuration[] csets = conf.getChildren ("constraint-set");
HashMap actionMap = new HashMap ();
HashMap resultMap = new HashMap ();
boolean allOK = true;
/*
* old obsoleted method
*/
if (!"".equals (valstr.trim ())) {
getLogger().debug ("Validating parameters "
+ "as specified via 'validate' parameter");
/* get list of params to be validated */
String[] rparams = Tokenizer.tokenize (valstr, ",", false);
/* perform actuall validation */
ValidatorActionHelper result = null;
String name = null;
HashMap params = new HashMap (rparams.length);
/* put required params into hash */
for (int i = 0; i < rparams.length; i ++) {
name = rparams[i];
if (name == null || "".equals (name.trim ())) {
getLogger().debug ("Wrong syntax of the 'validate' parameter");
return null;
}
name = name.trim ();
params.put (name, req.getParameter (name));
}
for (int i = 0; i < rparams.length; i ++) {
name = rparams[i].trim ();
result = validateParameter (name, null, desc,
params, true);
if (!result.isOK()) {
getLogger().debug ("Validation failed for parameter " + name);
allOK = false;
}
actionMap.put (name, result.getObject());
resultMap.put (name, result.getResult());
}
}
/*
* new set-based method
*/
if (!"".equals (valsetstr.trim ())) {
getLogger().debug ("Validating parameters "
+ "from given constraint-set " + valsetstr);
Configuration cset = null;
String setname = null;
int j = 0;
boolean found = false;
for (j = 0; j < csets.length; j ++) {
setname = csets[j].getAttribute ("name", "");
if (valsetstr.trim().equals (setname.trim ())) {
found = true;
break;
}
}
if (!found) {
getLogger().debug ("Given set "
+ valsetstr
+ " does not exist in a description file");
return null;
}
cset = csets[j];
/* get the list of params to be validated */
Configuration[] set = cset.getChildren ("validate");
/* perform actuall validation */
ValidatorActionHelper result = null;
String name = null;
HashMap params = new HashMap (set.length);
getLogger().debug ("Given set "
+ valsetstr
+ " contains " + set.length + " rules");
/* put required params into hash */
for (int i = 0; i < set.length; i ++) {
name = set[i].getAttribute ("name", "");
if ("".equals (name.trim ())) {
getLogger().debug ("Wrong syntax "
+ " of 'validate' children nr. " + i);
return null;
}
name = name.trim ();
params.put (name, req.getParameter (name));
}
for (int i = 0; i < set.length; i ++) {
name = set[i].getAttribute ("name", null);
result = validateParameter (name, set[i],
desc, params, true);
if (!result.isOK()) {
getLogger().debug ("Validation failed for parameter " + name);
allOK = false;
}
actionMap.put (name, result.getObject());
resultMap.put (name, result.getResult());
}
}
if (!allOK) {
// if any validation failed return an empty map
actionMap = null;
getLogger().debug ("All form params validated. An error occurred.");
} else {
getLogger().debug ("All form params successfully validated");
}
// store validation results in request attribute
req.setAttribute(Constants.XSP_FORMVALIDATOR_PATH, resultMap);
//return Collections.unmodifiableMap (actionMap);
return actionMap;
} catch (Exception e) {
getLogger().debug ("exception: ", e);
}