View Javadoc

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