View Javadoc

1   /*
2    * $Id: FacesInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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                      // Abort the chain as the result is done
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 }