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