View Javadoc

1   /*
2    * $Id: StrutsNavigationHandler.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 java.util.Map;
25  
26  import javax.faces.application.NavigationHandler;
27  import javax.faces.context.FacesContext;
28  
29  import com.opensymphony.xwork2.ActionContext;
30  import com.opensymphony.xwork2.config.entities.ActionConfig;
31  import com.opensymphony.xwork2.config.entities.ResultConfig;
32  
33  /***
34   * Overrides the JFS navigation by delegating the result to handling by the core
35   * result code lookup and execution.  If a result cannot be found, the previous
36   * NavigationHandler is called.
37   */
38  public class StrutsNavigationHandler extends NavigationHandler {
39  
40      private NavigationHandler parent;
41  
42      /***
43       * Creates the handler
44       *
45       * @param handler The old NavigationHandler to possibly delegate to
46       */
47      public StrutsNavigationHandler(NavigationHandler handler) {
48          this.parent = handler;
49      }
50  
51      /***
52       * Stores any outcomes as the result code, failing over to the old
53       * NavigationHandler
54       *
55       * @param facesContext The faces context
56       * @param fromAction The action we are coming from
57       * @param outcome The String return code
58       */
59      @Override
60      public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
61          ActionContext ctx = ActionContext.getContext();
62          if (outcome != null) {
63              if (ctx == null && ctx.getActionInvocation() == null) {
64                  delegateToParentNavigation(facesContext, fromAction, outcome);
65              } else {
66                  ActionConfig config = ctx.getActionInvocation().getProxy().getConfig();
67                  Map results = config.getResults();
68      
69                  ResultConfig resultConfig = null;
70      
71                  synchronized (config) {
72                      try {
73                          resultConfig = (ResultConfig) results.get(outcome);
74                      } catch (NullPointerException e) {
75                      }
76                      if (resultConfig == null) {
77                          // If no result is found for the given resultCode, try to get a wildcard '*' match.
78                          resultConfig = (ResultConfig) results.get("*");
79                      }
80                  }
81                  if (resultConfig != null) {
82                      ctx.getActionInvocation().setResultCode(outcome);
83                  } else {
84                      delegateToParentNavigation(facesContext, fromAction, outcome);
85                  }
86              }
87          }
88      }
89  
90      private void delegateToParentNavigation(FacesContext facesContext, String fromAction, String outcome) {
91          // Failing over to parent handler
92          parent.handleNavigation(facesContext, fromAction, outcome);
93      }
94  
95  }