/
Faces.java
2908 lines (2693 loc) · 140 KB
/
Faces.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.util;
import static jakarta.faces.FactoryFinder.APPLICATION_FACTORY;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import jakarta.el.ELContext;
import jakarta.el.ELResolver;
import jakarta.el.ExpressionFactory;
import jakarta.el.ValueExpression;
import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.application.Application;
import jakarta.faces.application.ApplicationFactory;
import jakarta.faces.application.NavigationHandler;
import jakarta.faces.application.ProjectStage;
import jakarta.faces.application.Resource;
import jakarta.faces.application.ResourceHandler;
import jakarta.faces.application.ViewHandler;
import jakarta.faces.component.UIViewParameter;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.FacesContextWrapper;
import jakarta.faces.context.Flash;
import jakarta.faces.context.PartialViewContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.ConverterException;
import jakarta.faces.convert.FacesConverter;
import jakarta.faces.event.PhaseId;
import jakarta.faces.lifecycle.Lifecycle;
import jakarta.faces.lifecycle.LifecycleFactory;
import jakarta.faces.render.RenderKit;
import jakarta.faces.validator.FacesValidator;
import jakarta.faces.validator.Validator;
import jakarta.faces.view.ViewDeclarationLanguage;
import jakarta.faces.view.ViewMetadata;
import jakarta.faces.view.facelets.FaceletContext;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.Part;
import org.omnifaces.component.ParamHolder;
import org.omnifaces.component.input.HashParam;
import org.omnifaces.component.input.ScriptParam;
import org.omnifaces.config.FacesConfigXml;
import org.omnifaces.el.FacesELResolver;
import org.omnifaces.facesviews.FacesViews;
import org.omnifaces.filter.MutableRequestFilter;
import org.omnifaces.filter.MutableRequestFilter.MutableRequest;
import org.omnifaces.resourcehandler.ResourceIdentifier;
/**
* <p>
* Collection of utility methods for the Faces API that are mainly shortcuts for obtaining stuff from the thread local
* {@link FacesContext}. In effects, it 'flattens' the hierarchy of nested objects. Do note that using the hierarchy is
* actually a better software design practice, but can lead to verbose code.
* <p>
* Next to those oneliner delegate calls, there are also some helpful methods which eliminates multiline boilerplate
* code, such as {@link #getLocale()} which returns sane fallback values, a more convenient
* {@link #redirect(String, Object...)} which automatically prepends the context path when the path does not start with
* <code>/</code> and offers support for URL encoding of request parameters supplied by varargs argument, and several
* useful {@link #sendFile(File, boolean)} methods which allows you to provide a {@link File}, <code>byte[]</code> or
* {@link InputStream} as a download to the client.
*
* <h2>Usage</h2>
* <p>
* Here are <strong>some</strong> examples:
* <pre>
* // Get a session attribute (no explicit cast necessary!).
* User user = Faces.getSessionAttribute("user");
* </pre>
* <pre>
* // Evaluate EL programmatically (no explicit cast necessary!).
* Item item = Faces.evaluateExpressionGet("#{item}");
* </pre>
* <pre>
* // Get a cookie value.
* String cookieValue = Faces.getRequestCookie("cookieName");
* </pre>
* <pre>
* // Get all supported locales with default locale as first item.
* List<Locale> supportedLocales = Faces.getSupportedLocales();
* </pre>
* <pre>
* // Check in e.g. preRenderView if session has been timed out.
* if (Faces.hasSessionTimedOut()) {
* Messages.addGlobalWarn("Oops, you have been logged out because your session was been timed out!");
* }
* </pre>
* <pre>
* // Get value of <f:metadata><f:attribute name="foo"> of different view without building it.
* String foo = Faces.getMetadataAttribute("/other.xhtml", "foo");
* </pre>
* <pre>
* // Send a redirect with parameters UTF-8 encoded in query string.
* Faces.redirect("product.xhtml?id=%d&name=%s", product.getId(), product.getName());
* </pre>
* <pre>
* // Invalidate the session and send a redirect.
* public void logout() {
* Faces.invalidateSession();
* Faces.redirect("login.xhtml"); // Can by the way also be done by return "login?faces-redirect=true" if in action method.
* }
* </pre>
* <pre>
* // Provide a file as attachment.
* public void download() {
* Faces.sendFile(new File("/path/to/file.ext"), true);
* }
* </pre>
* <pre>
* // Provide a file as attachment via output stream callback.
* public void download() {
* Faces.sendFile("file.txt", true, output -> {
* try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
* writer.println("Hello world");
* }
* });
* }
* </pre>
* <p>
* For a full list, check the <a href="#method.summary">method summary</a>.
*
* <h2>FacesLocal</h2>
* <p>
* Note that there's normally a minor overhead in obtaining the thread local {@link FacesContext}. In case client code
* needs to call methods in this class multiple times it's expected that performance will be slightly better if instead
* the {@link FacesContext} is obtained once and the required methods are called on that, although the difference is
* practically negligible when used in modern server hardware.
* <p>
* In such case, consider using {@link FacesLocal} instead. The difference with {@link Faces} is that no one method of
* {@link FacesLocal} obtains the {@link FacesContext} from the current thread by
* {@link FacesContext#getCurrentInstance()}. This job is up to the caller.
*
* <h2>#{faces} in EL</h2>
* <p>
* Since OmniFaces 2.6,
* all methods of {@link Faces} utility class which start with "get" or "is", and take no parameters, and return
* either <code>String</code> or <code>boolean</code>, and are not related to response nor to session or flash (for
* which already implicit EL objects <code>#{session}</code> and <code>#{flash}</code> exist), will be available as
* properties of the implicit object <code>#{faces}</code>. Examples are:
* <pre>
* #{faces.development}
* #{faces.serverInfo}
* #{faces.ajaxRequest}
* #{faces.requestBaseURL}
* #{faces.requestURLWithQueryString}
* </pre>
*
* @author Arjan Tijms
* @author Bauke Scholtz
* @see FacesLocal
* @see Servlets
* @see FacesELResolver
*/
public final class Faces {
// Constructors ---------------------------------------------------------------------------------------------------
private Faces() {
// Hide constructor.
}
// Faces general ----------------------------------------------------------------------------------------------------
/**
* Returns the current faces context.
* <p>
* <i>Note that whenever you absolutely need this method to perform a general task, you might want to consider to
* submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general
* task.</i>
* @return The current faces context.
* @see FacesContext#getCurrentInstance()
*/
public static FacesContext getContext() {
try {
return FacesContext.getCurrentInstance();
}
catch (NoClassDefFoundError canHappenWhenInvokedWithoutImpl) {
return null;
}
}
/**
* Returns the faces context that's stored in an ELContext.
* <p>
* Note that this only works for an ELContext that is created in the context of Faces.
*
* @param elContext the EL context to obtain the faces context from.
* @return the faces context that's stored in the given ELContext.
* @since 1.2
*/
public static FacesContext getContext(ELContext elContext) {
return (FacesContext) elContext.getContext(FacesContext.class);
}
/**
* Sets the given faces context as current instance. Use this if you have a custom {@link FacesContextWrapper}
* which you'd like to (temporarily) use as the current instance of the faces context.
* @param context The faces context to be set as the current instance.
* @since 1.3
*/
public static void setContext(FacesContext context) {
FacesContextSetter.setCurrentInstance(context);
}
/**
* Inner class so that the protected {@link FacesContext#setCurrentInstance(FacesContext)} method can be invoked.
* @author Bauke Scholtz
*/
private abstract static class FacesContextSetter extends FacesContext {
protected static void setCurrentInstance(FacesContext context) {
FacesContext.setCurrentInstance(context);
}
}
/**
* Returns <code>true</code> when the current faces context is available (i.e. it is not <code>null</code>).
* @return <code>true</code> when the current faces context is available.
* @since 2.0
*/
public static boolean hasContext() {
return getContext() != null;
}
/**
* Returns the current external context.
* <p>
* <i>Note that whenever you absolutely need this method to perform a general task, you might want to consider to
* submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general
* task.</i>
* @return The current external context.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getExternalContext()
*/
public static ExternalContext getExternalContext() {
return getContext().getExternalContext();
}
/**
* Returns the application singleton.
* <p>
* <i>Note that whenever you absolutely need this method to perform a general task, you might want to consider to
* submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general
* task.</i>
* @return The faces application singleton.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getApplication()
*/
public static Application getApplication() {
return getContext().getApplication();
}
/**
* Gets the Faces Application singleton from the FactoryFinder.
* <p>
* This method is an alternative for {@link Faces#getApplication()} for those situations where the
* {@link FacesContext} isn't available.
*
* @return The faces application singleton.
*/
public static Application getApplicationFromFactory() {
return ((ApplicationFactory) FactoryFinder.getFactory(APPLICATION_FACTORY)).getApplication();
}
/**
* Returns the package of the currently loaded Faces implementation.
* <p>
* This is also available in EL as <code>#{faces['package']}</code>.
* @return The package of the currently loaded Faces implementation.
* @since 3.14
*/
public static Package getPackage() {
return FacesLocal.getPackage(getContext());
}
/**
* Returns the implementation information of currently loaded Faces implementation. E.g. "Mojarra 2.1.7-FCS".
* <p>
* This is also available in EL as <code>#{faces.implInfo}</code>.
* @return The implementation information of currently loaded Faces implementation.
* @see Package#getImplementationTitle()
* @see Package#getImplementationVersion()
*/
public static String getImplInfo() {
return FacesLocal.getImplInfo(getContext());
}
/**
* Returns the server information of currently running application server implementation.
* <p>
* This is also available in EL as <code>#{faces.serverInfo}</code>.
* @return The server information of currently running application server implementation.
* @throws NullPointerException When faces context is unavailable.
* @see ServletContext#getServerInfo()
*/
public static String getServerInfo() {
return FacesLocal.getServerInfo(getContext());
}
/**
* Returns the project stage. This will return the <code>jakarta.faces.PROJECT_STAGE</code> context parameter in
* <code>web.xml</code>.
* @return The project stage.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getProjectStage()
* @since 2.6
*/
public static ProjectStage getProjectStage() {
return FacesLocal.getProjectStage(getContext());
}
/**
* Returns whether we're in development stage. This will be the case when the <code>jakarta.faces.PROJECT_STAGE</code>
* context parameter in <code>web.xml</code> is set to <code>Development</code>.
* <p>
* This is also available in EL as <code>#{faces.development}</code>.
* @return <code>true</code> if we're in development stage, otherwise <code>false</code>.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getProjectStage()
*/
public static boolean isDevelopment() {
return FacesLocal.isDevelopment(getContext());
}
/**
* Returns whether we're in system test stage. This will be the case when the <code>jakarta.faces.PROJECT_STAGE</code>
* context parameter in <code>web.xml</code> is set to <code>SystemTest</code>.
* <p>
* This is also available in EL as <code>#{faces.systemTest}</code>.
* @return <code>true</code> if we're in system test stage, otherwise <code>false</code>.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getProjectStage()
* @since 2.6
*/
public static boolean isSystemTest() {
return FacesLocal.isSystemTest(getContext());
}
/**
* Returns whether we're in production stage. This will be the case when the <code>jakarta.faces.PROJECT_STAGE</code>
* context parameter in <code>web.xml</code> is set to <code>Production</code>.
* <p>
* This is also available in EL as <code>#{faces.production}</code>.
* @return <code>true</code> if we're in production stage, otherwise <code>false</code>.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getProjectStage()
* @since 2.6
*/
public static boolean isProduction() {
return FacesLocal.isProduction(getContext());
}
/**
* Determines and returns the faces servlet mapping used in the current request. If Faces is prefix mapped (e.g.
* <code>/faces/*</code>), then this returns the whole path, with a leading slash (e.g. <code>/faces</code>). If Faces
* is suffix mapped (e.g. <code>*.xhtml</code>), then this returns the whole extension (e.g. <code>.xhtml</code>).
* <p>
* This is also available in EL as <code>#{faces.mapping}</code>.
* @return The faces servlet mapping (without the wildcard).
* @throws NullPointerException When faces context is unavailable.
* @throws IllegalStateException When mapping could not be determined.
* @see #getRequestPathInfo()
* @see #getRequestServletPath()
*/
public static String getMapping() {
return FacesLocal.getMapping(getContext());
}
/**
* Returns whether the faces servlet mapping used in the current request is a prefix mapping.
* <p>
* This is also available in EL as <code>#{faces.prefixMapping}</code>.
* @return <code>true</code> if the faces servlet mapping used in the current request is a prefix mapping, otherwise
* <code>false</code>.
* @see #getMapping()
* @see #isPrefixMapping(String)
*/
public static boolean isPrefixMapping() {
return isPrefixMapping(getMapping());
}
/**
* Returns whether the given faces servlet mapping is a prefix mapping. Use this method in preference to
* {@link #isPrefixMapping()} when you already have obtained the mapping from {@link #getMapping()} so that the
* mapping won't be calculated twice.
* @param mapping The mapping to be tested.
* @return <code>true</code> if the faces servlet mapping used in the current request is a prefix mapping, otherwise
* <code>false</code>.
* @throws NullPointerException When mapping is <code>null</code>.
*/
public static boolean isPrefixMapping(String mapping) {
return (mapping.charAt(0) == '/');
}
/**
* Returns the current phase ID.
* @return The current phase ID.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getCurrentPhaseId()
*/
public static PhaseId getCurrentPhaseId() {
return getContext().getCurrentPhaseId();
}
/**
* Signals Faces that the validations phase of the current request has failed. This can be invoked in any other
* phase than the validations phase. The value can be read by {@link #isValidationFailed()} in Java and by
* <code>#{facesContext.validationFailed}</code> in EL.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#validationFailed()
*/
public static void validationFailed() {
getContext().validationFailed();
}
/**
* Returns whether the validations phase of the current request has failed.
* <p>
* This is also available in EL as <code>#{faces.validationFailed}</code>.
* @return <code>true</code> if the validations phase of the current request has failed, otherwise
* <code>false</code>.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#isValidationFailed()
*/
public static boolean isValidationFailed() {
return getContext().isValidationFailed();
}
/**
* Returns the current EL context.
* @return The current EL context.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getELContext()
* @since 2.0
*/
public static ELContext getELContext() {
return getContext().getELContext();
}
/**
* Programmatically evaluate the given EL expression and return the evaluated value.
* @param <T> The expected return type.
* @param expression The EL expression to be evaluated.
* @return The evaluated value of the given EL expression.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see Application#evaluateExpressionGet(FacesContext, String, Class)
*/
public static <T> T evaluateExpressionGet(String expression) {
return FacesLocal.evaluateExpressionGet(getContext(), expression);
}
/**
* Programmatically evaluate the given EL expression and set the given value.
* @param expression The EL expression to be evaluated.
* @param value The value to be set in the property behind the given EL expression.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getExpressionFactory()
* @see ExpressionFactory#createValueExpression(ELContext, String, Class)
* @see ValueExpression#setValue(ELContext, Object)
* @since 1.1
*/
public static void evaluateExpressionSet(String expression, Object value) {
FacesLocal.evaluateExpressionSet(getContext(), expression, value);
}
/**
* Programmatically EL-resolve the given property on the given base object and return the resolved value.
* @param <T> The expected return type.
* @param base The base object whose property value is to be returned, or null to resolve a top-level variable.
* @param property The property or variable to be resolved on the given base.
* @return The resolved value of the given property on the given base object.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see Application#getELResolver()
* @see ELResolver#getValue(ELContext, Object, Object)
* @since 2.1
*/
public static <T> T resolveExpressionGet(Object base, String property) {
return FacesLocal.resolveExpressionGet(getContext(), base, property);
}
/**
* Programmatically EL-resolve the given property on the given base object and set the given value.
* @param base The base object whose property value is to be set, or null to set a top-level variable.
* @param property The property or variable to be set on the given base.
* @param value The value to be set in the property on the given base.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getELResolver()
* @see ELResolver#setValue(ELContext, Object, Object, Object)
* @since 2.1
*/
public static void resolveExpressionSet(Object base, String property, Object value) {
FacesLocal.resolveExpressionSet(getContext(), base, property, value);
}
/**
* Returns the Faces context attribute value associated with the given name.
* @param <T> The expected return type.
* @param name The Faces context attribute name.
* @return The Faces context attribute value associated with the given name.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see FacesContext#getAttributes()
* @since 1.3
*/
public static <T> T getContextAttribute(String name) {
return FacesLocal.getContextAttribute(getContext(), name);
}
/**
* Returns the Faces context attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The Faces context attribute name.
* @param computeIfAbsent The computed Faces context attribute value when absent. Useful if it represents a collection, map or bean.
* @return The Faces context attribute value associated with the given name.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getRequestMap()
* @since 3.1
*/
public static <T> T getContextAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getContextAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the Faces context attribute value associated with the given name.
* @param name The Faces context attribute name.
* @param value The Faces context attribute value.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getAttributes()
* @since 1.3
*/
public static void setContextAttribute(String name, Object value) {
FacesLocal.setContextAttribute(getContext(), name, value);
}
/**
* Creates and returns a Faces converter associated with given object identifier. If the given identifier is an
* instance of string, then delegate to {@link #createConverter(String)}. If the given identifier is an instance of
* class, then delegate to {@link #createConverter(Class)}. If the given identifier is a concrete converter
* instance, then return it directly.
* If no converter instance can be associated, then return null.
* @param <T> The expected converter type.
* @param identifier The Faces converter object identifier. This can be a string representing the converter ID, or a
* class representing the target type, or a class representing the converter class, or even the converter instance
* itself.
* @return A Faces converter associated with given object identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Converter<T> createConverter(Object identifier) {
return FacesLocal.createConverter(getContext(), identifier);
}
/**
* Creates and returns a Faces converter associated with given string identifier. First use the identifier as
* converter ID in {@link Application#createConverter(String)}. If that didn't return anything, then try to interpret
* the string identifier as class name and delegate to {@link #createConverter(Class)}.
* If no converter instance can be associated, then return null.
* @param <T> The expected converter type.
* @param identifier The Faces converter string identifier.
* @return A Faces converter associated with given string identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Converter<T> createConverter(String identifier) {
return FacesLocal.createConverter(getContext(), identifier);
}
/**
* Creates and returns a Faces converter associated with given class identifier. If the given identifier is not
* assignable to Converter.class, then use that as target type in {@link Application#createConverter(Class)}. If
* the given identifier is assignable to Converter.class, and the {@link FacesConverter} annotation is present,
* then instantiate it using CDI, else instantiate it using default constructor.
* If no converter instance can be associated, then return null.
* @param <T> The expected converter type.
* @param identifier The Faces converter class identifier.
* @return A Faces converter associated with given class identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Converter<T> createConverter(Class<?> identifier) {
return FacesLocal.createConverter(getContext(), identifier);
}
/**
* Creates and returns a Faces validator associated with given object identifier. If the given identifier is an
* instance of string, then delegate to {@link #createValidator(String)}. If the given identifier is an instance of
* class, then delegate to {@link #createValidator(Class)}. If the given identifier is a concrete validator
* instance, then return it directly.
* If no validator instance can be associated, then return null.
* @param <T> The expected validator type.
* @param identifier The Faces validator object identifier. This can be a string representing the validator ID, or a
* class representing the validator class, or even the validator instance itself.
* @return A Faces validator associated with given object identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Validator<T> createValidator(Object identifier) {
return FacesLocal.createValidator(getContext(), identifier);
}
/**
* Creates and returns a Faces validator associated with given string identifier. First use the identifier as
* validator ID in {@link Application#createValidator(String)}. If that didn't return anything, then try to
* interpret the string identifier as class name and delegate to {@link #createValidator(Class)}.
* If no validator instance can be associated, then return null.
* @param <T> The expected validator type.
* @param identifier The Faces validator string identifier.
* @return A Faces validator associated with given string identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Validator<T> createValidator(String identifier) {
return FacesLocal.createValidator(getContext(), identifier);
}
/**
* Creates and returns a Faces validator associated with given class identifier. If the given identifier is
* assignable to Validator.class, and the {@link FacesValidator} annotation is present, then instantiate it using
* CDI, else instantiate it using default constructor.
* If no validator instance can be associated, then return null.
* @param <T> The expected validator type.
* @param identifier The Faces validator class identifier.
* @return A Faces validator associated with given class identifier.
* @throws NullPointerException When faces context is unavailable.
* @since 2.5
*/
public static <T> Validator<T> createValidator(Class<?> identifier) {
return FacesLocal.createValidator(getContext(), identifier);
}
/**
* Creates and returns a Faces resource associated with given resource name.
* If no resource can be allocated, then return null.
* @param resourceName The resource name.
* @return A Faces resource associated with given resource name.
* @throws NullPointerException When faces context is unavailable.
* @see ResourceHandler#createResource(String)
* @since 3.6
*/
public static Resource createResource(String resourceName) {
return FacesLocal.createResource(getContext(), resourceName);
}
/**
* Creates and returns a Faces resource associated with given library name and resource name.
* If no resource can be allocated, then return null.
* @param libraryName The library name.
* @param resourceName The resource name.
* @return A Faces resource associated with given library name and resource name.
* @throws NullPointerException When faces context is unavailable.
* @see ResourceHandler#createResource(String, String)
* @since 3.6
*/
public static Resource createResource(String libraryName, String resourceName) {
return FacesLocal.createResource(getContext(), libraryName, resourceName);
}
/**
* Creates and returns a Faces resource associated with given resource identifier.
* If no resource can be allocated, then return null.
* @param resourceIdentifier The resource identifier.
* @return A Faces resource associated with given resource identifier.
* @throws NullPointerException When faces context is unavailable.
* @see ResourceHandler#createResource(String, String)
* @since 3.6
*/
public static Resource createResource(ResourceIdentifier resourceIdentifier) {
return FacesLocal.createResource(getContext(), resourceIdentifier);
}
/**
* Returns The {@link Lifecycle} associated with current Faces application.
* @return The {@link Lifecycle} associated with current Faces application.
* @throws NullPointerException When faces context is unavailable.
* @see LifecycleFactory#getLifecycle(String)
* @since 2.5
*/
public static Lifecycle getLifecycle() {
return FacesLocal.getLifecycle(getContext());
}
// Faces views ------------------------------------------------------------------------------------------------------
/**
* Returns the current view root.
* @return The current view root.
* @throws NullPointerException When faces context is unavailable.
* @see FacesContext#getViewRoot()
*/
public static UIViewRoot getViewRoot() {
return getContext().getViewRoot();
}
/**
* Sets the current view root to the given view ID. The view ID must start with a leading slash. If an invalid view
* ID is given, then the response will simply result in a 404.
* @param viewId The ID of the view which needs to be set as the current view root.
* @throws NullPointerException When faces context is unavailable.
* @see ViewHandler#createView(FacesContext, String)
* @see FacesContext#setViewRoot(UIViewRoot)
* @since 1.1
*/
public static void setViewRoot(String viewId) {
FacesLocal.setViewRoot(getContext(), viewId);
}
/**
* Returns the ID of the current view root, or <code>null</code> if there is no view.
* <p>
* This is also available in EL as <code>#{faces.viewId}</code>, although <code>#{view.viewId}</code> could be used.
* @return The ID of the current view root, or <code>null</code> if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see UIViewRoot#getViewId()
*/
public static String getViewId() {
return FacesLocal.getViewId(getContext());
}
/**
* Returns the ID of the current view root with view and hash parameters.
* <p>
* This is also available in EL as <code>#{faces.viewIdWithParameters}</code>.
* @return The ID of the current view root with view and hash parameters.
* @throws NullPointerException When faces context is unavailable.
* @see UIViewRoot#getViewId()
* @see ViewMetadata#getViewParameters(UIViewRoot)
* @see Faces#getHashParameters()
* @since 2.5
*/
public static String getViewIdWithParameters() {
return FacesLocal.getViewIdWithParameters(getContext());
}
/**
* Returns the base name of the current view, without extension, or <code>null</code> if there is no view.
* E.g. if the view ID is <code>/path/to/some.xhtml</code>, then this will return <code>some</code>.
* <p>
* This is also available in EL as <code>#{faces.viewName}</code>.
* @return The base name of the current view, without extension, or <code>null</code> if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see UIViewRoot#getViewId()
* @since 2.3
*/
public static String getViewName() {
return FacesLocal.getViewName(getContext());
}
/**
* Returns the {@link ViewDeclarationLanguage} associated with the "current" view ID.
* <p>
* The current view ID is the view ID that's set for the view root that's associated with
* the current faces context.
*
* @return The {@link ViewDeclarationLanguage} associated with the "current" view ID.
* @throws NullPointerException When faces context is unavailable.
* @since 1.8
*/
public static ViewDeclarationLanguage getViewDeclarationLanguage() {
return FacesLocal.getViewDeclarationLanguage(getContext());
}
/**
* Returns the {@link RenderKit} associated with the "current" view ID or view handler.
* <p>
* The current view ID is the view ID that's set for the view root that's associated with the current faces context.
* Or if there is none, then the current view handler will be assumed, which is the view handler that's associated
* with the requested view.
*
* @return The {@link RenderKit} associated with the "current" view ID or view handler.
* @throws NullPointerException When faces context is unavailable.
* @since 2.2
* @see UIViewRoot#getRenderKitId()
* @see ViewHandler#calculateRenderKitId(FacesContext)
*/
public static RenderKit getRenderKit() {
return FacesLocal.getRenderKit(getContext());
}
/**
* Normalize the given path as a valid view ID based on the current mapping, if necessary.
* <ul>
* <li>If the current mapping is a prefix mapping and the given path starts with it, then remove it.
* <li>If the current mapping is a suffix mapping and the given path ends with it, then replace it with the default
* Facelets suffix.
* </ul>
* @param path The path to be normalized as a valid view ID based on the current mapping.
* @return The path as a valid view ID.
* @throws NullPointerException When faces context is unavailable.
* @see #getMapping()
* @see #isPrefixMapping(String)
*/
public static String normalizeViewId(String path) {
return FacesLocal.normalizeViewId(getContext(), path);
}
/**
* Returns the view parameters of the current view, or an empty collection if there is no view.
* @return The view parameters of the current view, or an empty collection if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see ViewMetadata#getViewParameters(UIViewRoot)
*/
public static Collection<UIViewParameter> getViewParameters() {
return FacesLocal.getViewParameters(getContext());
}
/**
* Returns the view parameters of the current view as a parameter map, or an empty map if there is no view. This is
* ready for usage in among others {@link ViewHandler#getBookmarkableURL(FacesContext, String, Map, boolean)}.
* @return The view parameters of the current view as a parameter map, or an empty map if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see ViewMetadata#getViewParameters(UIViewRoot)
*/
public static Map<String, List<String>> getViewParameterMap() {
return FacesLocal.getViewParameterMap(getContext());
}
/**
* Returns the hash parameters of the current view, or an empty collection if there is no view.
* @return The hash parameters of the current view, or an empty collection if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see HashParam
* @since 3.2
*/
public static Collection<HashParam> getHashParameters() {
return FacesLocal.getHashParameters(getContext());
}
/**
* Returns the hash parameters of the current view as a parameter map, or an empty map if there is no view.
* @return The hash parameters of the current view as a parameter map, or an empty map if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see HashParam
* @since 3.2
*/
public static Map<String, List<String>> getHashParameterMap() {
return FacesLocal.getHashParameterMap(getContext());
}
/**
* Returns the hash query string of the current view, or <code>null</code> if there is none.
* This is the part after the <code>#</code> in the request URL as the enduser sees in browser address bar.
* This works only if the hash parameters are via <code><o:hashParam></code> registered in the view.
* @return The hash query string of the current view, or <code>null</code> if there is none.
* @throws NullPointerException When faces context is unavailable.
* @see HashParam
* @since 3.2
*/
public static String getHashQueryString() {
return FacesLocal.getHashQueryString(getContext());
}
/**
* Returns the script parameters of the current view, or an empty collection if there is no view.
* @return The script parameters of the current view, or an empty collection if there is no view.
* @throws NullPointerException When faces context is unavailable.
* @see ScriptParam
* @since 3.6
*/
public static Collection<ScriptParam> getScriptParameters() {
return FacesLocal.getScriptParameters(getContext());
}
/**
* Returns the metadata attribute map of the given view ID, or an empty map if there is no view metadata.
* @param viewId The view ID to return the metadata attribute map for.
* @return The metadata attribute map of the given view ID, or an empty map if there is no view metadata.
* @throws NullPointerException When faces context is unavailable.
* @see ViewDeclarationLanguage#getViewMetadata(FacesContext, String)
* @since 1.4
*/
public static Map<String, Object> getMetadataAttributes(String viewId) {
return FacesLocal.getMetadataAttributes(getContext(), viewId);
}
/**
* Returns the metadata attribute map of the current view, or an empty map if there is no view metadata.
* @return The metadata attribute map of the current view, or an empty map if there is no view metadata.
* @throws NullPointerException When faces context is unavailable.
* @see UIViewRoot#getAttributes()
* @since 2.0
*/
public static Map<String, Object> getMetadataAttributes() {
return FacesLocal.getMetadataAttributes(getContext());
}
/**
* Returns the metadata attribute of the given view ID associated with the given name.
* Note: this is not the same as the view scope, for that use {@link #getViewAttribute(String)}.
* @param <T> The expected return type.
* @param viewId The view ID to return the metadata attribute for.
* @param name The metadata attribute name.
* @return The metadata attribute of the given view ID associated with the given name.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ViewDeclarationLanguage#getViewMetadata(FacesContext, String)
* @since 1.4
*/
public static <T> T getMetadataAttribute(String viewId, String name) {
return FacesLocal.getMetadataAttribute(getContext(), viewId, name);
}
/**
* Returns the metadata attribute of the current view associated with the given name.
* Note: this is not the same as the view scope, for that use {@link #getViewAttribute(String)}.
* @param <T> The expected return type.
* @param name The metadata attribute name.
* @return The metadata attribute of the current view associated with the given name.
* @throws NullPointerException When faces context is unavailable.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see UIViewRoot#getAttributes()
* @since 1.4
*/
public static <T> T getMetadataAttribute(String name) {
return FacesLocal.getMetadataAttribute(getContext(), name);
}
/**
* Returns the current locale. If the locale set in the Faces view root is not null, then return it. Else if the
* client preferred locale is not null and is among supported locales, then return it. Else if the Faces default
* locale is not null, then return it. Else return the system default locale.
* @return The current locale.
* @throws NullPointerException When faces context is unavailable.
* @see UIViewRoot#getLocale()
* @see ExternalContext#getRequestLocale()
* @see Application#getDefaultLocale()
* @see Locale#getDefault()
*/
public static Locale getLocale() {
return FacesLocal.getLocale(getContext());
}
/**
* Returns the default locale, or <code>null</code> if there is none.
* @return The default locale, or <code>null</code> if there is none.
* @see Application#getDefaultLocale()
*/
public static Locale getDefaultLocale() {
return FacesLocal.getDefaultLocale(getContext());
}
/**
* Returns an unordered list of all supported locales on this application, with the default locale as the first
* item, if any. This will return an empty list if there are no locales definied in <code>faces-config.xml</code>.
* @return An unordered list of all supported locales on this application, with the default locale as the first
* item, if any. If you need an ordered list, use {@link FacesConfigXml#getSupportedLocales()} instead.
* @throws NullPointerException When faces context is unavailable.
* @see Application#getDefaultLocale()
* @see Application#getSupportedLocales()
*/
public static List<Locale> getSupportedLocales() {
return FacesLocal.getSupportedLocales(getContext());
}
/**
* Set the locale of the current view, which is to be used in localizing of the response.
* @param locale The locale of the current view.
* @throws NullPointerException When faces context is unavailable.
* @throws IllegalStateException When there is no view (i.e. when it is <code>null</code>). This can happen if the
* method is called at the wrong moment in the Faces lifecycle, e.g. before the view has been restored/created.
* @see UIViewRoot#setLocale(Locale)
* @since 1.2
*/
public static void setLocale(Locale locale) {
FacesLocal.setLocale(getContext(), locale);