View Javadoc

    1   /*
    2    * $Id: FacesInterceptor.java 471756 2006-11-06 15:01:43Z husted $
    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  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                      // Abort the chain as the result is done
    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 }