1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
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
102
103
104
105
106
107
108
109
110
111
112 return REQUEST_PARAMETERS;
113 }
114 }