1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.urlParamNav;
20
21 import javax.faces.application.NavigationHandler;
22 import javax.faces.context.ExternalContext;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25 import java.io.IOException;
26
27 /***
28 * This navigation handler replaces EL expressions (value bindings) within the to-view-id
29 * property of a navigation rule.
30 * <p>
31 * Since we do not know quite how this feature may interact with other JSF frameworks that
32 * also provide navigation handlers, this feature is not enabled by default.
33 * <p>
34 * To enable this feature:
35 * <ul>
36 * <li>add this navigation handler to your faces-config.xml</li>
37 * <li>add the {@link UrlParameterViewHandler} to your faces-config.xml</li>
38 * <li>configure the navigation case to use <redirect/> flag.<li/>
39 * </ul>
40 * Note that redirect is required for the rule; this is only natural as without redirect
41 * there is no bookmarkable URL for the user.
42 */
43 public class UrlParameterNavigationHandler extends NavigationHandler
44 {
45 private final NavigationHandler original;
46
47 public UrlParameterNavigationHandler(final NavigationHandler original)
48 {
49 this.original = original;
50 }
51
52 public void handleNavigation(final FacesContext context, String fromAction, String outcome)
53 {
54 original.handleNavigation(new FacesContextWrapper(context)
55 {
56 public ExternalContext getExternalContext()
57 {
58 return new ExternalContextWrapper(super.getExternalContext())
59 {
60 public void redirect(String url) throws IOException
61 {
62 super.redirect(interceptRedirect(context, url));
63 }
64 };
65 }
66 }, fromAction, outcome);
67 }
68
69
70
71 protected String interceptRedirect(FacesContext context, String url)
72 {
73 int pos = url.indexOf("#{");
74 if (pos > -1 && url.indexOf("}", pos) > -1)
75 {
76 ValueBinding vb = context.getApplication().createValueBinding(url);
77 return (String) vb.getValue(context);
78 }
79
80 return url;
81 }
82 }