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  
20  package org.apache.myfaces.orchestra.requestParameterProvider;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
25  
26  import java.io.Serializable;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /***
32   * The manager which manage all the attached providers and add their fields to the url.
33   * <p>
34   * This class has an instance per user http session. Code that wishes to add values
35   * to urls generated within pages register a "provider" with this object. When request
36   * params need to be output this manager invokes each provider in turn.
37   * <p>
38   * If the data accessed by a registered "provider" has scope shorter than an http session
39   * then the registered provider should obviously be <i>deregistered</i> when the data
40   * is no longer valid.
41   * <p>
42   * This class works together with the RequestParameterServletFilter and
43   * RequestParameterResponseWrapper so that every call to response.encodeURL(...) gets
44   * forwarded to this class. As encodeURL is used by JSF commandButton, commandLink, etc,
45   * this ensures that whatever the user clicks on the parameters provided by the
46   * registered provider objects are present on the next JSF request.
47   */
48  public class RequestParameterProviderManager implements Serializable
49  {
50  	private final Log LOG = LogFactory.getLog(RequestParameterProviderManager.class);
51  
52  	private static final String PAGE_PARAMETER_SEP = "?";
53  	private static final String PARAMETER_SEP = "&";
54  	private static final String PARAMETER_PROVIDER_MANAGER_KEY = RequestParameterProviderManager.class.getName();
55  
56  	// TODO: investigate why this is transient. At least some callers of register call
57  	// it only once per session, so if the session data is passed to another machine or is
58  	// saved then restored then it seems the registered providers will be lost when they
59  	// should not be...
60  	private transient List providers;
61  
62  	private RequestParameterProviderManager()
63  	{
64  	}
65  
66  	public static RequestParameterProviderManager getInstance()
67  	{
68  		RequestParameterProviderManager manager =
69  			(RequestParameterProviderManager) FrameworkAdapter.getCurrentInstance().getSessionAttribute(PARAMETER_PROVIDER_MANAGER_KEY);
70  
71  		if (manager == null)
72  		{
73  			// TODO: remove this factory code. Not IOC-friendly.
74  			manager = new RequestParameterProviderManager();
75  			FrameworkAdapter.getCurrentInstance().setSessionAttribute(PARAMETER_PROVIDER_MANAGER_KEY, manager);
76  		}
77  
78  		return manager;
79  	}
80  
81  
82  	/***
83  	 * Register the given provider.
84  	 *
85  	 * @param provider the provider to register.
86  	 */
87  
88  	public void register(RequestParameterProvider provider)
89  	{
90  		if (provider == null)
91  		{
92  			LOG.warn("RequestParameterProvider is null -> no registration!");
93  		}
94  		else
95  		{
96  			getProviders().add(provider);
97  		}
98  	}
99  
100 
101 	/***
102 	 * Encode all fields of all providers, and attach the name-value pairs to url.
103 	 *
104 	 * @param url the URL to which the fields should be attached.
105 	 * @return the url after attaching all fields.
106 	 */
107 
108 	public String encodeAndAttachParameters(String url)
109 	{
110 		if (!isResponseIntercepted())
111 		{
112 			throw new IllegalStateException("RequestParameterServletFilter not called. Please configure the filter " + RequestParameterServletFilter.class.getName() + " in your web.xml to cover your faces requests.");
113 		}
114 
115 		StringBuffer sb = new StringBuffer();
116 		if (url == null)
117 		{
118 			LOG.warn("URL is null -> empty string is returned.");
119 			return sb.toString();
120 		}
121 
122 		int nuofParams = -1;
123 		String firstSeparator = url.indexOf(PAGE_PARAMETER_SEP) == -1 ? PAGE_PARAMETER_SEP : PARAMETER_SEP;
124 		sb.append(url);
125 		for (Iterator it = getProviders().iterator(); it.hasNext();)
126 		{
127 			RequestParameterProvider provider = (RequestParameterProvider) it.next();
128 			String[] fields = provider.getFields();
129 			if (fields == null)
130 			{
131 				continue;
132 			}
133 			for (int i = 0; i < fields.length; i++)
134 			{
135 				nuofParams++;
136 
137 				sb.append(nuofParams == 0 ? firstSeparator : PARAMETER_SEP);
138 				sb.append(fields[i]);
139 				sb.append("=");
140 				sb.append(provider.getFieldValue(fields[i]));
141 			}
142 		}
143 		return sb.toString();
144 	}
145 
146 	protected boolean isResponseIntercepted()
147 	{
148 		FrameworkAdapter fa = FrameworkAdapter.getCurrentInstance();
149 
150 		return Boolean.TRUE.equals(fa.getRequestAttribute(RequestParameterServletFilter.REQUEST_PARAM_FILTER_CALLED))
151 			|| Boolean.TRUE.equals(fa.getRequestAttribute(RequestParameterServletFilter.REQUEST_PARAM_RESPONSE_WRAPPED));
152 	}
153 
154 	protected List getProviders()
155 	{
156 		if (providers == null)
157 		{
158 			providers = new ArrayList();
159 		}
160 
161 		return providers;
162 	}
163 }