Commits

Anonymous committed c958b24

Removed hard coded checks for "input" method. Add interceptor params so the user can specify any methods they would like to exclude.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@640e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits e3c9e34

Comments (0)

Files changed (5)

src/java/com/opensymphony/xwork/interceptor/DefaultWorkflowInterceptor.java

 import com.opensymphony.xwork.ActionInvocation;
 import com.opensymphony.xwork.Validateable;
 import com.opensymphony.xwork.ValidationAware;
+import com.opensymphony.xwork.util.TextParseUtil;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.Collections;
+import java.util.Set;
 
 
 /**
  * <!-- START SNIPPET: description -->
  *
- * An interceptor that does some basic validation workflow before allowing the interceptor chain to continue. This
- * interceptor does nothing if the name of the method being invoked is <b>input</b>. For example, a request to
- * <b>foo!input.action</b> would be skipped by this request. The order of execution in the workflow is:
+ * An interceptor that does some basic validation workflow before allowing the interceptor chain to continue.
+ *
+ * <p/>This interceptor does nothing if the name of the method being invoked
+ * is specified in the <b>excludeMethods</b> parameter. <b>excludeMethods</b>
+ * accepts a comma-delimited list of method names. For example, requests to
+ * <b>foo!input.action</b> and <b>foo!back.action</b> will be skipped by this
+ * interceptor if you set the <b>excludeMethods</b> parameter to "input,
+ * back".
+ *
+ * <p/>The order of execution in the workflow is:
  *
  * <ol>
  *
  */
 public class DefaultWorkflowInterceptor implements Interceptor {
 
-    public void destroy() {
-    }
+    Log log = LogFactory.getLog(this.getClass());
+
+    Set excludeMethods = Collections.EMPTY_SET;
 
-    public void init() {
+    public void setExcludeMethods(String excludeMethods) {
+        this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
     }
 
     public String intercept(ActionInvocation invocation) throws Exception {
-        if (!"input".equals(invocation.getProxy().getMethod())) {
+        if (excludeMethods.contains(invocation.getProxy().getMethod())) {
+            log.debug("Skipping workflow. Method found in exclude list.");
+            return invocation.invoke();
+        }
 
-            Object action = invocation.getAction();
+        Object action = invocation.getAction();
 
-            if (action instanceof Validateable) {
-                Validateable validateable = (Validateable) action;
-                validateable.validate();
-            }
+        if (action instanceof Validateable) {
+            Validateable validateable = (Validateable) action;
+            validateable.validate();
+        }
 
-            if (action instanceof ValidationAware) {
-                ValidationAware validationAwareAction = (ValidationAware) action;
+        if (action instanceof ValidationAware) {
+            ValidationAware validationAwareAction = (ValidationAware) action;
 
-                if (validationAwareAction.hasErrors()) {
-                    return Action.INPUT;
-                }
+            if (validationAwareAction.hasErrors()) {
+                return Action.INPUT;
             }
         }
 
         return invocation.invoke();
     }
+
+    public void destroy() {}
+    public void init() {}
 }

src/java/com/opensymphony/xwork/util/TextParseUtil.java

 
 import com.opensymphony.util.TextUtils;
 
+import java.util.HashSet;
+import java.util.Set;
+
 
 /**
  * Utility class for text parsing.
 
         return XWorkConverter.getInstance().convertValue(stack.getContext(), result, asType);
     }
+
+    public static Set commaDelimitedStringToSet(String s) {
+        Set set = new HashSet();
+        String[] split = s.split(",");
+        for (int i = 0; i < split.length; i++) {
+            String trimmed = split[i].trim();
+            if (trimmed.length() > 0)
+                set.add(trimmed);
+        }
+        return set;
+    }
 }

src/java/com/opensymphony/xwork/validator/ValidationInterceptor.java

 package com.opensymphony.xwork.validator;
 
 import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.util.TextParseUtil;
 import com.opensymphony.xwork.interceptor.AroundInterceptor;
 
+import java.util.Set;
+import java.util.Collections;
+
 
 /**
  * <!-- START SNIPPET: description -->
  * any validation rules (found in files such as <i>ActionClass-validation.xml</i>) and adds field-level and action-level
  * error messages (provided that the action implements {@link com.opensymphony.xwork.ValidationAware}). This interceptor
  * is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have
- * already been set on the action. This interceptor does nothing if the name of the method being invoked is
- * <b>input</b>. For example, a request to <b>foo!input.action</b> would be skipped by this request.
+ * already been set on the action.
+ *
+ * <p/>This interceptor does nothing if the name of the method being invoked
+ * is specified in the <b>excludeMethods</b> parameter. <b>excludeMethods</b>
+ * accepts a comma-delimited list of method names. For example, requests to
+ * <b>foo!input.action</b> and <b>foo!back.action</b> will be skipped by this
+ * interceptor if you set the <b>excludeMethods</b> parameter to "input,
+ * back".
  *
  * <p/>Note that this has nothing to do with the {@link com.opensymphony.xwork.Validateable} interface and simply adds
  * error messages to the action. The workflow of the action request does not change due to this interceptor. Rather,
  */
 public class ValidationInterceptor extends AroundInterceptor {
 
+    Set excludeMethods = Collections.EMPTY_SET;
+
+    public void setExcludeMethods(String excludeMethods) {
+        this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
+    }
+
     protected void after(ActionInvocation dispatcher, String result) throws Exception {
     }
 
      * @throws Exception if an error occurs validating the action.
      */
     protected void before(ActionInvocation invocation) throws Exception {
+        if (excludeMethods.contains(invocation.getProxy().getMethod())) {
+            log.debug("Skipping validation. Method found in exclude list.");
+            return;
+        }
+
         Object action = invocation.getAction();
         String context = invocation.getProxy().getActionName();
 
                     + invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName() + ".");
         }
 
-        if (!"input".equals(invocation.getProxy().getMethod())) {
-            ActionValidatorManager.validate(action, context);
-        }
+        ActionValidatorManager.validate(action, context);
     }
 }

src/test/com/opensymphony/xwork/interceptor/DefaultWorkflowInterceptorTest.java

 
     public void testInvokesActionInvocationIfNoErrors() throws Exception {
         actionMock.expectAndReturn("hasErrors", false);
-
+        actionMock.expect("validate");
         final String result = "testing123";
         invocationMock.expectAndReturn("invoke", result);
+        invocationMock.expectAndReturn("getAction", action);
         assertEquals(result, interceptor.intercept(invocation));
     }
 
     public void testReturnsInputWithoutExecutingIfHasErrors() throws Exception {
         actionMock.expectAndReturn("hasErrors", true);
+        actionMock.expect("validate");
+        invocationMock.expectAndReturn("getAction", action);
         assertEquals(Action.INPUT, interceptor.intercept(invocation));
     }
 
+    public void testExcludesMethod() throws Exception {
+        interceptor.setExcludeMethods("execute");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         actionMock = new Mock(ValidateAction.class);
-        actionMock.expect("validate");
         action = (ValidateAction) actionMock.proxy();
         invocationMock = new Mock(ActionInvocation.class);
-        invocationMock.expectAndReturn("getAction", action);
         ActionProxy proxy = new MockActionProxy();
         proxy.setMethod("execute");
         invocationMock.expectAndReturn("getProxy", proxy);

src/test/com/opensymphony/xwork/util/TextParseUtilTest.java

 import com.opensymphony.xwork.XWorkTestCase;
 
 import java.util.List;
+import java.util.HashSet;
+import java.util.Arrays;
 
 /**
  * User: plightbo
         s = TextParseUtil.translateVariables('$', "count must be between ${123} and ${456}, current value is ${98765}.", stack, Object.class);
         assertEquals("count must be between 123 and 456, current value is 98765.", s);
     }
+
+    public void testCommaDelimitedStringToSet() {
+        assertEquals(0, TextParseUtil.commaDelimitedStringToSet("").size());
+        assertEquals(new HashSet(Arrays.asList(new String[] { "foo", "bar", "tee" })),
+                TextParseUtil.commaDelimitedStringToSet(" foo, bar,tee"));
+    }
 }