View Javadoc

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