1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.jsf;
23
24 import javax.faces.context.FacesContext;
25
26 import com.opensymphony.xwork2.ActionContext;
27 import com.opensymphony.xwork2.ActionInvocation;
28 import com.opensymphony.xwork2.interceptor.Interceptor;
29
30 /***
31 * Translates JSF phases into individual interceptors, and adapts their expected
32 * workflow to Action 2
33 */
34 public class FacesInterceptor extends FacesSupport implements Interceptor {
35
36 private static final long serialVersionUID = -5418255964277566516L;
37
38 /***
39 * Not used
40 */
41 public void init() {
42 }
43
44 /***
45 * Adapts the phase workflow to Action 2
46 *
47 * @param invocation
48 * The action invocation
49 * @return The string result code
50 */
51 public String intercept(ActionInvocation invocation) throws Exception {
52
53 if (isFacesEnabled(invocation.getInvocationContext())) {
54 FacesContext context = FacesContext.getCurrentInstance();
55
56 if (context.getRenderResponse()) {
57 return invocation.invoke();
58 } else {
59
60 String viewId = invocation.getProxy().getNamespace() + '/'
61 + invocation.getProxy().getActionName();
62 executePhase(viewId, context);
63
64 if (context.getResponseComplete()) {
65
66 return null;
67 } else {
68 if (invocation.getResultCode() != null) {
69 return invocation.getResultCode();
70 } else {
71 return invocation.invoke();
72 }
73 }
74 }
75 } else {
76 return invocation.invoke();
77 }
78 }
79
80 /***
81 * Executes the specific phase. The phase id is constructed as a composite
82 * of the namespace and action name.
83 *
84 * @param viewId
85 * The view id
86 * @param facesContext
87 * The current faces context
88 * @return True if the next phases should be skipped
89 */
90 protected boolean executePhase(String viewId, FacesContext facesContext) {
91 return false;
92 }
93
94 /***
95 * Not used
96 */
97 public void destroy() {
98 }
99
100 /***
101 * Determines whether to process this request with the JSF phases
102 *
103 * @param ctx The current action context
104 * @return True if it is a faces-enabled request
105 */
106 protected boolean isFacesEnabled(ActionContext ctx) {
107 return ctx.get(FACES_ENABLED) != null;
108 }
109
110 }