1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.lifecycle; |
20 | |
|
21 | |
import java.io.BufferedReader; |
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.io.InputStreamReader; |
25 | |
import java.io.OutputStream; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Map; |
28 | |
import javax.faces.FacesException; |
29 | |
import javax.faces.application.Resource; |
30 | |
import javax.faces.context.ExternalContext; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
import javax.faces.lifecycle.ClientWindow; |
33 | |
import javax.faces.render.ResponseStateManager; |
34 | |
import javax.servlet.http.Cookie; |
35 | |
import javax.servlet.http.HttpServletResponse; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class CODIClientSideWindow extends ClientWindow |
42 | |
{ |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private static final String AUTOMATED_ENTRY_POINT_PARAMETER_KEY = "automatedEntryPoint"; |
54 | |
|
55 | |
private static final long serialVersionUID = 5293942986187078113L; |
56 | |
|
57 | |
private static final String WINDOW_ID_COOKIE_PREFIX = "jfwid-"; |
58 | |
private static final String CODI_REQUEST_TOKEN = "mfRid"; |
59 | |
|
60 | |
private static final String UNINITIALIZED_WINDOW_ID_VALUE = "uninitializedWindowId"; |
61 | |
private static final String WINDOW_ID_REPLACE_PATTERN = "$$windowIdValue$$"; |
62 | |
private static final String NOSCRIPT_URL_REPLACE_PATTERN = "$$noscriptUrl$$"; |
63 | |
private static final String NOSCRIPT_PARAMETER = "mfDirect"; |
64 | |
|
65 | |
private final ClientConfig clientConfig; |
66 | |
|
67 | |
private final WindowContextConfig windowContextConfig; |
68 | |
|
69 | |
private final TokenGenerator clientWindowTokenGenerator; |
70 | |
|
71 | |
private String windowId; |
72 | |
|
73 | 0 | private String unparsedWindowHandlerHtml = null; |
74 | |
|
75 | |
private Map<String,String> queryParamsMap; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
protected CODIClientSideWindow(TokenGenerator clientWindowTokenGenerator, |
84 | |
WindowContextConfig windowContextConfig, |
85 | |
ClientConfig clientConfig) |
86 | 0 | { |
87 | 0 | this.windowContextConfig = windowContextConfig; |
88 | 0 | this.clientConfig = clientConfig; |
89 | 0 | this.clientWindowTokenGenerator = clientWindowTokenGenerator; |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void decode(FacesContext facesContext) |
120 | |
{ |
121 | 0 | if (facesContext.isPostback()) |
122 | |
{ |
123 | |
|
124 | 0 | if (getId() == null) |
125 | |
{ |
126 | 0 | setId(calculateWindowIdFromPost(facesContext)); |
127 | |
} |
128 | |
} |
129 | |
|
130 | 0 | if (!isClientSideWindowHandlerRequest(facesContext)) |
131 | |
{ |
132 | 0 | return; |
133 | |
} |
134 | |
|
135 | 0 | ExternalContext externalContext = facesContext.getExternalContext(); |
136 | |
|
137 | 0 | if (isNoscriptRequest(externalContext)) |
138 | |
{ |
139 | |
|
140 | 0 | clientConfig.setJavaScriptEnabled(false); |
141 | 0 | return; |
142 | |
} |
143 | |
|
144 | 0 | String windowId = getWindowIdFromCookie(externalContext); |
145 | 0 | if (windowId == null) |
146 | |
{ |
147 | |
|
148 | 0 | sendWindowHandlerHtml(facesContext, null); |
149 | 0 | facesContext.responseComplete(); |
150 | |
} |
151 | |
else |
152 | |
{ |
153 | 0 | if (AUTOMATED_ENTRY_POINT_PARAMETER_KEY.equals(windowId) || |
154 | |
(!windowContextConfig.isUnknownWindowIdsAllowed() |
155 | |
)) |
156 | |
{ |
157 | |
|
158 | |
|
159 | |
|
160 | 0 | windowId = createWindowId(facesContext); |
161 | |
|
162 | |
|
163 | 0 | sendWindowHandlerHtml(facesContext, windowId); |
164 | 0 | facesContext.responseComplete(); |
165 | |
} |
166 | |
else |
167 | |
{ |
168 | |
|
169 | |
|
170 | |
|
171 | 0 | setId(windowId); |
172 | |
} |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
public String calculateWindowIdFromPost(FacesContext context) |
177 | |
{ |
178 | |
|
179 | |
|
180 | 0 | String windowId = context.getExternalContext().getRequestParameterMap().get( |
181 | |
ResponseStateManager.CLIENT_WINDOW_PARAM); |
182 | 0 | if (windowId != null) |
183 | |
{ |
184 | 0 | return windowId; |
185 | |
} |
186 | 0 | return null; |
187 | |
} |
188 | |
|
189 | |
private boolean isClientSideWindowHandlerRequest(FacesContext facesContext) |
190 | |
{ |
191 | |
|
192 | |
|
193 | 0 | return !facesContext.isPostback() && clientConfig.isClientSideWindowHandlerRequest(facesContext); |
194 | |
} |
195 | |
|
196 | |
private boolean isNoscriptRequest(ExternalContext externalContext) |
197 | |
{ |
198 | 0 | String noscript = externalContext.getRequestParameterMap().get(NOSCRIPT_PARAMETER); |
199 | |
|
200 | 0 | return (noscript != null && "true".equals(noscript)); |
201 | |
} |
202 | |
|
203 | |
private void sendWindowHandlerHtml(FacesContext facesContext, String windowId) |
204 | |
{ |
205 | 0 | HttpServletResponse httpResponse = (HttpServletResponse) facesContext.getExternalContext().getResponse(); |
206 | |
|
207 | |
try |
208 | |
{ |
209 | 0 | httpResponse.setStatus(HttpServletResponse.SC_OK); |
210 | 0 | httpResponse.setContentType("text/html"); |
211 | |
|
212 | 0 | if (unparsedWindowHandlerHtml == null) |
213 | |
{ |
214 | 0 | Resource resource = facesContext.getApplication().getResourceHandler().createResource( |
215 | |
"windowhandler.html", "org.apache.myfaces.windowId"); |
216 | |
|
217 | 0 | unparsedWindowHandlerHtml = convertStreamToString(resource.getInputStream()); |
218 | |
} |
219 | |
|
220 | 0 | String windowHandlerHtml = unparsedWindowHandlerHtml; |
221 | |
|
222 | 0 | if (windowId == null) |
223 | |
{ |
224 | 0 | windowId = UNINITIALIZED_WINDOW_ID_VALUE; |
225 | |
} |
226 | |
|
227 | |
|
228 | 0 | windowHandlerHtml = windowHandlerHtml.replace(WINDOW_ID_REPLACE_PATTERN, windowId); |
229 | |
|
230 | |
|
231 | 0 | windowHandlerHtml = windowHandlerHtml.replace( |
232 | |
NOSCRIPT_URL_REPLACE_PATTERN, getNoscriptUrl(facesContext.getExternalContext())); |
233 | |
|
234 | 0 | OutputStream os = httpResponse.getOutputStream(); |
235 | |
try |
236 | |
{ |
237 | 0 | os.write(windowHandlerHtml.getBytes()); |
238 | |
} |
239 | |
finally |
240 | |
{ |
241 | 0 | os.close(); |
242 | 0 | } |
243 | |
} |
244 | 0 | catch (IOException ioe) |
245 | |
{ |
246 | 0 | throw new FacesException(ioe); |
247 | 0 | } |
248 | 0 | } |
249 | |
|
250 | |
private static String convertStreamToString(InputStream is) |
251 | |
{ |
252 | 0 | StringBuilder sb = new StringBuilder(); |
253 | |
try |
254 | |
{ |
255 | 0 | BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
256 | 0 | String line = null; |
257 | 0 | while ((line = reader.readLine()) != null) |
258 | |
{ |
259 | 0 | sb.append(line + "\n"); |
260 | |
} |
261 | |
} |
262 | 0 | catch (IOException e) |
263 | |
{ |
264 | 0 | throw new FacesException(e); |
265 | |
} |
266 | |
finally |
267 | |
{ |
268 | 0 | if (is != null) |
269 | |
{ |
270 | |
try |
271 | |
{ |
272 | 0 | is.close(); |
273 | |
} |
274 | 0 | catch (IOException e) |
275 | |
{ |
276 | |
|
277 | 0 | } |
278 | |
} |
279 | |
} |
280 | 0 | return sb.toString(); |
281 | |
} |
282 | |
|
283 | |
private String getNoscriptUrl(ExternalContext externalContext) |
284 | |
{ |
285 | 0 | String url = externalContext.getRequestPathInfo(); |
286 | 0 | if (url == null) |
287 | |
{ |
288 | 0 | url = ""; |
289 | |
} |
290 | |
|
291 | |
|
292 | 0 | int lastSlash = url.lastIndexOf('/'); |
293 | 0 | if (lastSlash != -1) |
294 | |
{ |
295 | 0 | url = url.substring(lastSlash + 1); |
296 | |
} |
297 | |
|
298 | |
|
299 | 0 | url = addParameters(externalContext, url, true, true, true); |
300 | |
|
301 | |
|
302 | 0 | if (url.contains("?")) |
303 | |
{ |
304 | 0 | url = url + "&"; |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | 0 | url = url + "?"; |
309 | |
} |
310 | 0 | url = url + NOSCRIPT_PARAMETER + "=true"; |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | 0 | url = url.replace("\"", ""); |
316 | 0 | url = url.replace("\'", ""); |
317 | |
|
318 | 0 | return url; |
319 | |
} |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public static String addParameters(ExternalContext externalContext, String url, |
331 | |
boolean addRequestParameter, boolean addPageParameter, boolean encodeValues) |
332 | |
{ |
333 | 0 | StringBuilder finalUrl = new StringBuilder(url); |
334 | 0 | boolean existingParameters = url.contains("?"); |
335 | 0 | boolean urlContainsWindowId = url.contains(ResponseStateManager.CLIENT_WINDOW_URL_PARAM + "="); |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | 0 | return finalUrl.toString(); |
379 | |
} |
380 | |
|
381 | |
protected String createWindowId(FacesContext context) |
382 | |
{ |
383 | 0 | String windowId = clientWindowTokenGenerator._getNextToken(); |
384 | 0 | setId(windowId); |
385 | 0 | return windowId; |
386 | |
} |
387 | |
|
388 | |
private String getWindowIdFromCookie(ExternalContext externalContext) |
389 | |
{ |
390 | 0 | String cookieName = WINDOW_ID_COOKIE_PREFIX + getRequestToken(externalContext); |
391 | 0 | Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(cookieName); |
392 | |
|
393 | 0 | if (cookie != null) |
394 | |
{ |
395 | |
|
396 | |
|
397 | |
|
398 | 0 | cookie.setMaxAge(0); |
399 | |
|
400 | 0 | return cookie.getValue(); |
401 | |
} |
402 | |
|
403 | 0 | return null; |
404 | |
} |
405 | |
|
406 | |
private String getRequestToken(ExternalContext externalContext) |
407 | |
{ |
408 | 0 | String requestToken = externalContext.getRequestParameterMap().get(CODI_REQUEST_TOKEN); |
409 | 0 | if (requestToken != null) |
410 | |
{ |
411 | 0 | return requestToken; |
412 | |
} |
413 | |
|
414 | 0 | return ""; |
415 | |
} |
416 | |
|
417 | |
@Override |
418 | |
public String getId() |
419 | |
{ |
420 | 0 | return windowId; |
421 | |
} |
422 | |
|
423 | |
public void setId(String id) |
424 | |
{ |
425 | 0 | windowId = id; |
426 | 0 | queryParamsMap = null; |
427 | 0 | } |
428 | |
|
429 | |
@Override |
430 | |
public Map<String, String> getQueryURLParameters(FacesContext context) |
431 | |
{ |
432 | 0 | if (queryParamsMap == null) |
433 | |
{ |
434 | 0 | String id = context.getExternalContext().getClientWindow().getId(); |
435 | 0 | if (id != null) |
436 | |
{ |
437 | 0 | queryParamsMap = new HashMap<String, String>(2,1); |
438 | 0 | queryParamsMap.put(ResponseStateManager.CLIENT_WINDOW_URL_PARAM, id); |
439 | |
} |
440 | |
} |
441 | 0 | return queryParamsMap; |
442 | |
} |
443 | |
} |