View Javadoc

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  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 &lt;redirect/&gt; 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  	// TODO: this only supports one EL expression at the moment; it would be nice
70  	// to support multiple.
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  }