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