1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.myfaces.tomahawk.util;
21
22 import javax.faces.application.NavigationHandler;
23 import javax.faces.application.ViewHandler;
24 import javax.faces.component.UIViewRoot;
25 import javax.faces.context.FacesContext;
26
27 /**
28 * An alternative implementation of the standard JSF NavigationHandler API which
29 * directly using the outcome of an action as the name of the page to navigate to.
30 * <p>
31 * This bypasses the "logical navigation" approach of standard JSF, so that pages
32 * can be wired together by using actual urls in command actions and action-method
33 * return values.
34 * <p>
35 * When this navigation-handler is configured, all navigation-rule entries in
36 * faces-config.xml files are ignored completely.
37 * <p>
38 * This navigation handler does not work well in combination with other custom
39 * navigation handlers, as it never passes any calls down to underlying
40 * implementations. Having another NavigationHandler decorate this one will
41 * work; having this navigation handler decorate another will not as the
42 * decorated handler will never be invoked. Therefore if multiple navigation
43 * handlers are to be used (eg the Tomahawk RedirectTrackerNavigationHandler
44 * in combination with this one), then this should be listed first in the
45 * faces-config.xml file so that the other handlers decorate this one.
46 */
47 public class DirectNavigationHandler extends NavigationHandler
48 {
49 /**
50 * Gives the handleNavigation() method an alternative behaviour. Linking
51 * is now processed directly to the given url (e.g. action="/pages/site.jsp").
52 *
53 * There is no check if the outcome value really points to a valid page.
54 */
55 public void handleNavigation(FacesContext context, String fromAction, String outcome)
56 {
57 if(outcome == null || outcome.length()==0)
58 {
59 return;
60 }
61
62 ViewHandler viewHandler=context.getApplication().getViewHandler();
63 UIViewRoot viewRoot=viewHandler.createView(context,outcome);
64 context.setViewRoot(viewRoot);
65 context.renderResponse();
66 }
67 }