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.conversation;
21  
22  import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProvider;
23  
24  /***
25   * Adds the required fields (conversationContext) to the request parameters.
26   * <p>
27   * This ensures that every URL in the generated page contains the current
28   * conversation context id as a query parameter. When the request is submitted to the
29   * server this query parameter is then used to select the correct ConversationContext
30   * instance from the user session (ie the set of Conversation objects that are associated
31   * with this particular window).
32   */
33  public class ConversationRequestParameterProvider implements RequestParameterProvider
34  {
35  	private final static String[] REQUEST_PARAMETERS = new String[]
36  		{
37  			ConversationManager.CONVERSATION_CONTEXT_PARAM
38  		};
39  
40  	private final static ThreadLocal inSeparationMode = new ThreadLocal();
41  
42  	/***
43  	 * Update a threadlocal flag indicating whether URLs written to the
44  	 * response page should have the special ConversationContext query
45  	 * parameter added to them or not.
46  	 * <p>
47  	 * Defaults to false (no separation), which means that urls ARE modified. 
48  	 * <p>
49  	 * This can be called by a component before rendering its children in order to
50  	 * skip this url mangling. Any code that calls this method is responsible for
51  	 * restoring the original value at the appropriate time. This is very important,
52  	 * because this is a thread-local value that will be inherited by whatever
53  	 * request this pooled thread is reused for!
54  	 */
55  	public static void setInSeparationMode(boolean separationMode)
56  	{
57  		// TODO: consider using a request-scope variable rather than a
58  		// ThreadLocal; less damage if the flag is not reset..
59  		inSeparationMode.set(separationMode ? Boolean.TRUE : Boolean.FALSE);
60  	}
61  
62  	/***
63  	 * Returns true if URLs should be written out unmodified, false if they should
64  	 * have the conversation context id appended as a query parameter.
65  	 */
66  	public static boolean isInSeparationMode()
67  	{
68  		return Boolean.TRUE.equals(inSeparationMode.get());
69  	}
70  
71  	public String getFieldValue(String field)
72  	{
73  		if (isInSeparationMode())
74  		{
75  			return null;
76  		}
77  
78  		ConversationManager conversationManager = ConversationManager.getInstance();
79  		if (conversationManager == null)
80  		{
81  			throw new IllegalStateException("can find the conversationManager");
82  		}
83  
84  		Long conversationContextId = conversationManager.getConversationContextId();
85  		if (conversationContextId == null)
86  		{
87  			return null;
88  		}
89  
90  		return Long.toString(conversationContextId.longValue(), Character.MAX_RADIX);
91  	}
92  
93  	public String[] getFields()
94  	{
95  		if (isInSeparationMode())
96  		{
97  			return null;
98  		}
99  
100 		/*
101 		ConversationManager conversationManager = ConversationManager.getInstance();
102 		if (conversationManager == null)
103 		{
104 			throw new IllegalStateException("can find the conversationManager");
105 		}
106 		if (!conversationManager.hasConversationContext())
107 		{
108 			return null;
109 		}
110 		*/
111 
112 		return REQUEST_PARAMETERS;
113 	}
114 }