WW-3753 - adheres AnnotationActionValidatorManager to ActionValidatorManager interface's contract

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1293791 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-02-26 09:28:10 +00:00
parent 9abf8dd346
commit f47cfa889a
3 changed files with 46 additions and 12 deletions
@@ -75,7 +75,7 @@ public class AnnotationActionValidatorManager implements ActionValidatorManager
}
public List<Validator> getValidators(Class clazz, String context, String method) {
final String validatorKey = buildValidatorKey(clazz);
final String validatorKey = buildValidatorKey(clazz, context);
final List<ValidatorConfig> cfgs;
if (validatorCache.containsKey(validatorKey)) {
@@ -216,23 +216,36 @@ public class AnnotationActionValidatorManager implements ActionValidatorManager
* @param clazz the action.
* @return a validator key which is the class name plus context.
*/
protected static String buildValidatorKey(Class clazz) {
protected static String buildValidatorKey(Class clazz, String context) {
ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
ActionProxy proxy = invocation.getProxy();
ActionConfig config = proxy.getConfig();
//the key needs to use the name of the action from the config file,
//instead of the url, so wild card actions will have the same validator
//see WW-2996
StringBuilder sb = new StringBuilder(clazz.getName());
sb.append("/");
if (StringUtils.isNotBlank(config.getPackageName())) {
sb.append(config.getPackageName());
sb.append("/");
}
sb.append(config.getName());
sb.append("|");
sb.append(proxy.getMethod());
// the key needs to use the name of the action from the config file,
// instead of the url, so wild card actions will have the same validator
// see WW-2996
// UPDATE:
// WW-3753 Using the config name instead of the context only for
// wild card actions to keep the flexibility provided
// by the original design (such as mapping different contexts
// to the same action and method if desired)
String configName = config.getName();
if (configName.contains(ActionConfig.WILDCARD)) {
sb.append(configName);
sb.append("|");
sb.append(proxy.getMethod());
} else {
sb.append(context);
}
return sb.toString();
}
@@ -19,7 +19,6 @@ import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil;
import com.opensymphony.xwork2.util.logging.Logger;
@@ -206,7 +205,8 @@ public class ValidationInterceptor extends MethodFilterInterceptor {
//the action name has to be from the url, otherwise validators that use aliases, like
//MyActio-someaction-validator.xml will not be found, see WW-3194
String context = proxy.getActionName();
//UPDATE: see WW-3753
String context = this.getValidationContext(proxy);
String method = proxy.getMethod();
if (log.isDebugEnabled()) {
@@ -264,5 +264,26 @@ public class ValidationInterceptor extends MethodFilterInterceptor {
return invocation.invoke();
}
/**
* Returns the context that will be used by the
* {@link ActionValidatorManager} to associate the action invocation with
* the appropriate {@link ValidatorConfig ValidatorConfigs}.
* <p>
* The context returned is used in the pattern
* <i>ActionClass-context-validation.xml</i>
* <p>
* The default context is the action name from the URL, but the method can
* be overridden to implement custom contexts.
* <p>
* This can be useful in cases in which a single action and a single model
* require vastly different validation based on some condition.
*
* @return the Context
*/
protected String getValidationContext(ActionProxy proxy) {
// This method created for WW-3753
return proxy.getActionName();
}
}
@@ -70,8 +70,8 @@ public class AnnotationActionValidatorManagerTest extends XWorkTestCase {
}
public void testBuildValidatorKey() {
String validatorKey = AnnotationActionValidatorManager.buildValidatorKey(SimpleAnnotationAction.class);
assertEquals(SimpleAnnotationAction.class.getName() + "/packageName/name|execute", validatorKey);
String validatorKey = AnnotationActionValidatorManager.buildValidatorKey(SimpleAnnotationAction.class, "name");
assertEquals(SimpleAnnotationAction.class.getName() + "/packageName/name", validatorKey);
}
public void testBuildsValidatorsForAlias() {