1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.conversation;
20
21 import org.apache.myfaces.custom.redirectTracker.RedirectTrackerManager;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25 import java.io.IOException;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class UIEnsureConversation extends AbstractConversationComponent
44 {
45 public static final String COMPONENT_TYPE = "org.apache.myfaces.EnsureConversation";
46
47 private String redirectTo;
48 private Boolean preCheck;
49
50 public void encodeBegin(FacesContext context) throws IOException
51 {
52 super.encodeBegin(context);
53
54 checkConversation(context, getName());
55 }
56
57 public void decode(FacesContext context)
58 {
59 super.decode(context);
60
61 try
62 {
63 checkConversation(context, getName());
64 }
65 catch (IOException e)
66 {
67 throw new RuntimeException(e);
68 }
69 }
70
71 public void restoreState(FacesContext context, Object state)
72 {
73 Object[] states = (Object[]) state;
74
75 super.restoreState(context, states[0]);
76 redirectTo = (String) states[1];
77 preCheck = (Boolean) states[2];
78 }
79
80 public Object saveState(FacesContext context)
81 {
82 return new Object[]
83 {
84 super.saveState(context),
85 redirectTo,
86 preCheck
87 };
88 }
89
90 protected void checkConversation(FacesContext context, String name) throws IOException
91 {
92 ConversationManager conversationManager = ConversationManager.getInstance();
93
94 if (Boolean.TRUE.equals(preCheck))
95 {
96 String actionResult = (String) getAction().invoke(context, null);
97 if (actionResult == null)
98 {
99
100 return;
101 }
102
103 conversationManager.getMessager().setConversationNotActive(context, getName());
104 return;
105 }
106 else if (!conversationManager.hasConversation(name))
107 {
108 if (getAction() != null)
109 {
110 String actionResult = (String) getAction().invoke(context, null);
111 if (actionResult == null)
112 {
113
114 return;
115 }
116 conversationManager.getMessager().setConversationNotActive(context, getName());
117
118
119 context.getApplication().getNavigationHandler().handleNavigation(context, null, actionResult);
120 }
121 else
122 {
123 conversationManager.getMessager().setConversationNotActive(context, getName());
124
125 String actionUrl = context.getApplication().getViewHandler().getActionURL(
126 context, getRedirectTo());
127 String encodedActionUrl = context.getExternalContext().encodeActionURL(actionUrl);
128
129
130 RedirectTrackerManager manager = RedirectTrackerManager.getInstance(context);
131 if (manager != null)
132 {
133 encodedActionUrl = manager.trackRedirect(context, encodedActionUrl);
134 }
135
136
137 context.getExternalContext().redirect(encodedActionUrl);
138 }
139
140 return;
141 }
142 }
143
144
145
146
147
148
149
150 public String getRedirectTo()
151 {
152 if (redirectTo != null)
153 {
154 return redirectTo;
155 }
156 ValueBinding vb = getValueBinding("redirectTo");
157 if (vb == null)
158 {
159 return null;
160 }
161 return (String) vb.getValue(getFacesContext());
162 }
163
164 public void setRedirectTo(String redirectTo)
165 {
166 this.redirectTo = redirectTo;
167 }
168
169
170
171
172
173
174
175
176 public Boolean getPreCheck()
177 {
178 if (preCheck != null)
179 {
180 return preCheck;
181 }
182 ValueBinding vb = getValueBinding("preCheck");
183 if (vb == null)
184 {
185 return null;
186 }
187 return (Boolean) vb.getValue(getFacesContext());
188 }
189
190 public void setPreCheck(Boolean preCheck)
191 {
192 this.preCheck = preCheck;
193 }
194
195 }