1 /*
2 * Copyright (c) 2002-2007 by OpenSymphony
3 * All rights reserved.
4 */
5 package com.opensymphony.xwork2.interceptor;
6
7 import com.opensymphony.xwork2.Action;
8 import com.opensymphony.xwork2.ActionInvocation;
9 import com.opensymphony.xwork2.ValidationAware;
10 import com.opensymphony.xwork2.util.logging.Logger;
11 import com.opensymphony.xwork2.util.logging.LoggerFactory;
12
13 /**
14 * <!-- START SNIPPET: description -->
15 *
16 * An interceptor that makes sure there are not validation errors before allowing the interceptor chain to continue.
17 * <b>This interceptor does not perform any validation</b>.
18 *
19 * <p/>This interceptor does nothing if the name of the method being invoked is specified in the <b>excludeMethods</b>
20 * parameter. <b>excludeMethods</b> accepts a comma-delimited list of method names. For example, requests to
21 * <b>foo!input.action</b> and <b>foo!back.action</b> will be skipped by this interceptor if you set the
22 * <b>excludeMethods</b> parameter to "input, back".
23 *
24 * <b>Note:</b> As this method extends off MethodFilterInterceptor, it is capable of
25 * deciding if it is applicable only to selective methods in the action class. This is done by adding param tags
26 * for the interceptor element, naming either a list of excluded method names and/or a list of included method
27 * names, whereby includeMethods overrides excludedMethods. A single * sign is interpreted as wildcard matching
28 * all methods for both parameters.
29 * See {@link MethodFilterInterceptor} for more info.
30 *
31 * <!-- END SNIPPET: description -->
32 *
33 * <p/> <u>Interceptor parameters:</u>
34 *
35 * <!-- START SNIPPET: parameters -->
36 *
37 * <ul>
38 *
39 * <li>inputResultName - Default to "input". Determine the result name to be returned when
40 * an action / field error is found.</li>
41 *
42 * </ul>
43 *
44 * <!-- END SNIPPET: parameters -->
45 *
46 * <p/> <u>Extending the interceptor:</u>
47 *
48 * <p/>
49 *
50 * <!-- START SNIPPET: extending -->
51 *
52 * There are no known extension points for this interceptor.
53 *
54 * <!-- END SNIPPET: extending -->
55 *
56 * <p/> <u>Example code:</u>
57 *
58 * <pre>
59 * <!-- START SNIPPET: example -->
60 *
61 * <action name="someAction" class="com.examples.SomeAction">
62 * <interceptor-ref name="params"/>
63 * <interceptor-ref name="validation"/>
64 * <interceptor-ref name="workflow"/>
65 * <result name="success">good_result.ftl</result>
66 * </action>
67 *
68 * <-- In this case myMethod as well as mySecondMethod of the action class
69 * will not pass through the workflow process -->
70 * <action name="someAction" class="com.examples.SomeAction">
71 * <interceptor-ref name="params"/>
72 * <interceptor-ref name="validation"/>
73 * <interceptor-ref name="workflow">
74 * <param name="excludeMethods">myMethod,mySecondMethod</param>
75 * </interceptor-ref name="workflow">
76 * <result name="success">good_result.ftl</result>
77 * </action>
78 *
79 * <-- In this case, the result named "error" will be used when
80 * an action / field error is found -->
81 * <-- The Interceptor will only be applied for myWorkflowMethod method of action
82 * classes, since this is the only included method while any others are excluded -->
83 * <action name="someAction" class="com.examples.SomeAction">
84 * <interceptor-ref name="params"/>
85 * <interceptor-ref name="validation"/>
86 * <interceptor-ref name="workflow">
87 * <param name="inputResultName">error</param>
88 * <param name="excludeMethods">*</param>
89 * <param name="includeMethods">myWorkflowMethod</param>
90 * </interceptor-ref>
91 * <result name="success">good_result.ftl</result>
92 * </action>
93 *
94 * <!-- END SNIPPET: example -->
95 * </pre>
96 *
97 * @author Jason Carreira
98 * @author Rainer Hermanns
99 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
100 * @author Philip Luppens
101 * @author tm_jee
102 */
103 public class DefaultWorkflowInterceptor extends MethodFilterInterceptor {
104
105 private static final long serialVersionUID = 7563014655616490865L;
106
107 private static final Logger LOG = LoggerFactory.getLogger(DefaultWorkflowInterceptor.class);
108
109 private String inputResultName = Action.INPUT;
110
111 /**
112 * Set the <code>inputResultName</code> (result name to be returned when
113 * a action / field error is found registered). Default to {@link Action#INPUT}
114 *
115 * @param inputResultName what result name to use when there was validation error(s).
116 */
117 public void setInputResultName(String inputResultName) {
118 this.inputResultName = inputResultName;
119 }
120
121 /**
122 * Intercept {@link ActionInvocation} and returns a <code>inputResultName</code>
123 * when action / field errors is found registered.
124 *
125 * @return String result name
126 */
127 protected String doIntercept(ActionInvocation invocation) throws Exception {
128 Object action = invocation.getAction();
129
130 if (action instanceof ValidationAware) {
131 ValidationAware validationAwareAction = (ValidationAware) action;
132
133 if (validationAwareAction.hasErrors()) {
134 if (LOG.isDebugEnabled()) {
135 LOG.debug("Errors on action "+validationAwareAction+", returning result name 'input'");
136 }
137 return inputResultName;
138 }
139 }
140
141 return invocation.invoke();
142 }
143
144 }