1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.util;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import java.io.Writer;
25
26 import java.lang.reflect.Method;
27
28 import javax.faces.context.ExternalContext;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.http.HttpServletRequest;
33
34 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
35
36
37
38
39
40
41
42
43 public final class ExternalContextUtils
44 {
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public static final boolean isRequestTypeAvailable(RequestType type)
59 {
60 switch (type)
61 {
62 case SERVLET:
63 return true;
64
65 case ACTION:
66 case RENDER:
67 return _PORTLET_CONTEXT_CLASS != null;
68
69 case RESOURCE:
70 case EVENT:
71 return _PORTLET_RENDER_REQUEST_CLASS != null;
72
73 default:
74 return false;
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static final boolean isRequestTypeSupported(RequestType type)
94 {
95 switch (type)
96 {
97 case SERVLET:
98 return true;
99
100 case ACTION:
101 case RENDER:
102 return _PORTLET_10_SUPPORTED;
103
104 case RESOURCE:
105 case EVENT:
106 return _PORTLET_20_SUPPORTED;
107
108 default:
109 return false;
110 }
111 }
112
113
114
115
116
117
118
119
120
121 public static final RequestType getRequestType(ExternalContext ec)
122 {
123
124
125
126
127 if(_PORTLET_10_SUPPORTED)
128 {
129 if (_PORTLET_CONTEXT_CLASS.isInstance(ec.getContext()))
130 {
131
132 Object request = ec.getRequest();
133
134 if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
135 {
136 return RequestType.RENDER;
137 }
138
139 if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
140 {
141 if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
142 {
143 return RequestType.ACTION;
144 }
145
146
147 if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
148 {
149 return RequestType.RESOURCE;
150 }
151
152 return RequestType.EVENT;
153 }
154
155 return RequestType.ACTION;
156 }
157 }
158
159 return RequestType.SERVLET;
160 }
161
162
163
164
165
166
167
168
169
170 public static String getSessionId(ExternalContext ec)
171 {
172 return getSessionId(ec, true);
173 }
174
175
176
177
178
179
180
181
182
183 public static String getSessionId(ExternalContext ec, boolean create)
184 {
185 Object session = ec.getSession(create);
186 return (null!=session)?(String)_runMethod(session, "getId"):null;
187 }
188
189
190
191
192
193
194
195 public static String getRequestedSessionId(ExternalContext ec)
196 {
197 return (String) _runMethod(ec.getRequest(), "getRequestedSessionId");
198 }
199
200
201
202
203
204
205
206
207 public static boolean isRequestedSessionIdValid(ExternalContext ec)
208 {
209 return (Boolean) _runMethod(ec.getRequest(), "isRequestedSessionIdValid");
210 }
211
212
213
214
215
216
217
218 public static String getServletContextPath(ExternalContext ec)
219 {
220 if(!isPortlet(ec))
221 {
222 return ((ServletContext) ec.getContext()).getContextPath();
223 }
224 else
225 {
226 return null;
227 }
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241 @Deprecated
242 public static String getRequestContextPath(ExternalContext ec)
243 {
244 if(!isPortlet(ec))
245 {
246 return ec.getRequestContextPath();
247 }
248 else
249 {
250 return null;
251 }
252 }
253
254
255
256
257
258
259
260
261 public static String getRequestURI(ExternalContext ec)
262 {
263 if (!isPortlet(ec))
264 {
265 return ((HttpServletRequest) ec.getRequest()).getRequestURI();
266 }
267 else
268 {
269 return null;
270 }
271 }
272
273
274
275
276
277
278
279
280
281 @Deprecated
282 public static String getRequestScheme(ExternalContext ec)
283 {
284 return ec.getRequestScheme();
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298 @Deprecated
299 public static Writer getResponseWriter(ExternalContext ec)
300 throws IOException
301 {
302 if (isResponseWritable(ec))
303 {
304 return ec.getResponseOutputWriter();
305 }
306
307 return null;
308 }
309
310
311
312
313
314
315
316
317
318
319 @Deprecated
320 public static String getCharacterEncoding(ExternalContext ec)
321 {
322 return ec.getRequestCharacterEncoding();
323 }
324
325
326
327
328
329
330
331
332 public static String getContextName(ExternalContext ec)
333 {
334 try
335 {
336 if (isPortlet(ec))
337 {
338 return (String) _runMethod(ec.getContext(), "getPortletContextName");
339 }
340 else
341 {
342 return ((ServletContext) ec.getContext()).getServletContextName();
343 }
344 }
345 catch (final ClassCastException e)
346 {
347 _LOG.severe(e);
348 }
349 return null;
350 }
351
352
353
354
355
356
357
358
359 public static String getServerInfo(ExternalContext ec)
360 {
361 try
362 {
363 if (isPortlet(ec))
364 {
365 return (String) _runMethod(ec.getContext(), "getServerInfo");
366 }
367 else
368 {
369 return ((ServletContext) ec.getContext()).getServerInfo();
370 }
371 }
372 catch (final ClassCastException e)
373 {
374 _LOG.severe(e);
375 }
376 return null;
377 }
378
379
380
381
382
383
384
385 public static int getContentLength(ExternalContext ec)
386 {
387 if(isRequestFromClient(ec))
388 {
389 return (Integer) _runMethod(ec.getRequest(), "getContentLength");
390 }
391
392 return -1;
393 }
394
395
396
397
398
399
400
401
402
403
404
405 @Deprecated
406 public static String getContentType(ExternalContext ec)
407 {
408 return ec.getRequestContentType();
409 }
410
411
412
413
414
415
416
417
418 public static InputStream getRequestInputStream(ExternalContext ec)
419 throws IOException
420 {
421 RequestType type = getRequestType(ec);
422 if(type.isRequestFromClient())
423 {
424 Object req = ec.getRequest();
425 if(type.isPortlet())
426 {
427 return (InputStream)_runMethod(req, "getPortletInputStream");
428 }
429 else
430 {
431 return ((ServletRequest) ec.getRequest()).getInputStream();
432 }
433 }
434
435 return null;
436 }
437
438
439
440
441
442
443
444
445
446
447
448
449
450 @Deprecated
451 public static boolean isAction(ExternalContext ec)
452 {
453 return isRequestFromClient(ec);
454 }
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470 public static boolean isPortlet(ExternalContext ec)
471 {
472 return getRequestType(ec).isPortlet();
473 }
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490 public static final boolean isResponseWritable(ExternalContext ec)
491 {
492 return getRequestType(ec).isResponseWritable();
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510 public static final boolean isRequestFromClient(ExternalContext ec)
511 {
512 return getRequestType(ec).isRequestFromClient();
513 }
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 public static boolean isHttpServletRequest(ExternalContext ec)
529 {
530 return (!isPortlet(ec) && (ec.getRequest() instanceof HttpServletRequest));
531 }
532
533
534
535
536
537
538 public static boolean isSecure(
539 ExternalContext ec)
540 {
541 Object req = ec.getRequest();
542 if (isPortlet(ec))
543 {
544 return (Boolean)_runMethod(req, "isSecure");
545 }
546 else
547 {
548 return ((ServletRequest)req).isSecure();
549 }
550 }
551
552
553
554
555
556
557
558
559 private static Object _runMethod(Object obj, String methodName)
560 {
561 try
562 {
563 Method sessionIdMethod = obj.getClass().getMethod(methodName);
564 return sessionIdMethod.invoke(obj);
565 }
566 catch (Exception e)
567 {
568 return null;
569 }
570
571 }
572
573
574 private ExternalContextUtils()
575 {}
576
577 private static final TrinidadLogger _LOG = TrinidadLogger
578 .createTrinidadLogger(ExternalContextUtils.class);
579
580
581
582 private static final Class<?> _PORTLET_ACTION_REQUEST_CLASS;
583 private static final Class<?> _PORTLET_RENDER_REQUEST_CLASS;
584 private static final Class<?> _PORTLET_RESOURCE_REQUEST_CLASS;
585 private static final Class<?> _PORTLET_CONTEXT_CLASS;
586 private static final boolean _PORTLET_10_SUPPORTED;
587 private static final boolean _PORTLET_20_SUPPORTED;
588
589 static
590 {
591 Class<?> context;
592 Class<?> actionRequest;
593 Class<?> renderRequest;
594 Class<?> resourceRequest;
595 boolean portlet20Supported = false;
596 boolean portlet10Supported = false;
597
598 try
599 {
600 context = ClassLoaderUtils.loadClass("javax.portlet.PortletContext");
601 actionRequest = ClassLoaderUtils.loadClass("javax.portlet.ActionRequest");
602 renderRequest = ClassLoaderUtils.loadClass("javax.portlet.RenderRequest");
603
604 try
605 {
606 resourceRequest = ClassLoaderUtils.loadClass("javax.portlet.ResourceRequest");
607 }
608 catch (ClassNotFoundException e)
609 {
610 _LOG.fine("Portlet 2.0 API is not available on classpath. Portlet 2.0 functionality is disabled");
611 resourceRequest = null;
612 }
613 }
614 catch (final ClassNotFoundException e)
615 {
616 _LOG
617 .fine("Portlet API is not available on the classpath. Portlet configurations are disabled.");
618 context = null;
619 actionRequest = null;
620 renderRequest = null;
621 resourceRequest = null;
622 }
623
624
625 if(context != null)
626 {
627 try
628 {
629 Class<?> bridge = ClassLoaderUtils.loadClass("javax.portlet.faces.Bridge");
630
631 if(bridge != null)
632 {
633 portlet10Supported = true;
634
635
636
637 String specName = bridge.getPackage().getSpecificationTitle();
638 _LOG.fine("Found Bridge: " + specName);
639 if(specName != null && specName.startsWith("Portlet 2"))
640 {
641 portlet20Supported = true;
642 }
643
644 if(_LOG.isInfo())
645 {
646 String ver = (portlet20Supported)?"2.0":"1.0";
647 _LOG.info("Portlet Environment Detected: " + ver);
648 }
649 }
650 }
651 catch (ClassNotFoundException e)
652 {
653 _LOG.fine("Portlet API is present but bridge is not. Portlet configurations are disabled.");
654 }
655 }
656
657 _PORTLET_CONTEXT_CLASS = context;
658 _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
659 _PORTLET_RENDER_REQUEST_CLASS = renderRequest;
660 _PORTLET_RESOURCE_REQUEST_CLASS = resourceRequest;
661 _PORTLET_10_SUPPORTED = portlet10Supported;
662 _PORTLET_20_SUPPORTED = portlet20Supported;
663 }
664 }