View Javadoc

1   /*
2    * $Id: FacesInterceptor.java 441572 2006-09-08 16:56:01Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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                      // Abort the chain as the result is done
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 }