/
WebModule.java
2673 lines (2353 loc) · 97.1 KB
/
WebModule.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
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.web;
import static com.sun.enterprise.security.ee.SecurityUtil.getContextID;
import static com.sun.enterprise.web.Constants.DEPLOYMENT_CONTEXT_ATTRIBUTE;
import static com.sun.enterprise.web.Constants.ENABLE_HA_ATTRIBUTE;
import static com.sun.enterprise.web.Constants.IS_DISTRIBUTABLE_ATTRIBUTE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.text.MessageFormat.format;
import static java.util.logging.Level.WARNING;
import static org.glassfish.web.LogFacade.UNABLE_TO_RESTORE_SESSIONS_DURING_REDEPLOY;
import static org.glassfish.web.LogFacade.UNABLE_TO_SAVE_SESSIONS_DURING_REDEPLOY;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RunAs;
import javax.security.jacc.PolicyConfigurationFactory;
import javax.security.jacc.PolicyContextException;
import javax.servlet.Filter;
import javax.servlet.HttpMethodConstraintElement;
import javax.servlet.Servlet;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletSecurityElement;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import org.apache.catalina.Container;
import org.apache.catalina.ContainerListener;
import org.apache.catalina.InstanceListener;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.Pipeline;
import org.apache.catalina.Realm;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.DynamicServletRegistrationImpl;
import org.apache.catalina.core.ServletRegistrationImpl;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardPipeline;
import org.apache.catalina.core.StandardWrapper;
import org.apache.catalina.deploy.FilterMaps;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.servlets.DefaultServlet;
import org.apache.catalina.session.StandardManager;
import org.apache.jasper.servlet.JspServlet;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.embeddable.web.Context;
import org.glassfish.embeddable.web.config.FormLoginConfig;
import org.glassfish.embeddable.web.config.LoginConfig;
import org.glassfish.embeddable.web.config.SecurityConfig;
import org.glassfish.embeddable.web.config.TransportGuarantee;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.classmodel.reflect.Types;
import org.glassfish.internal.api.ServerContext;
import org.glassfish.security.common.Role;
import org.glassfish.web.LogFacade;
import org.glassfish.web.admin.monitor.ServletProbeProvider;
import org.glassfish.web.admin.monitor.SessionProbeProvider;
import org.glassfish.web.admin.monitor.WebModuleProbeProvider;
import org.glassfish.web.deployment.annotation.handlers.ServletSecurityHandler;
import org.glassfish.web.deployment.descriptor.AbsoluteOrderingDescriptor;
import org.glassfish.web.deployment.descriptor.AuthorizationConstraintImpl;
import org.glassfish.web.deployment.descriptor.LoginConfigurationImpl;
import org.glassfish.web.deployment.descriptor.SecurityConstraintImpl;
import org.glassfish.web.deployment.descriptor.UserDataConstraintImpl;
import org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl;
import org.glassfish.web.deployment.descriptor.WebComponentDescriptorImpl;
import org.glassfish.web.deployment.descriptor.WebResourceCollectionImpl;
import org.glassfish.web.deployment.runtime.CookieProperties;
import org.glassfish.web.deployment.runtime.LocaleCharsetInfo;
import org.glassfish.web.deployment.runtime.LocaleCharsetMap;
import org.glassfish.web.deployment.runtime.SessionConfig;
import org.glassfish.web.deployment.runtime.SessionManager;
import org.glassfish.web.deployment.runtime.SessionProperties;
import org.glassfish.web.deployment.runtime.SunWebAppImpl;
import org.glassfish.web.deployment.runtime.WebProperty;
import org.glassfish.web.loader.ServletContainerInitializerUtil;
import org.glassfish.web.valve.GlassFishValve;
import org.jvnet.hk2.config.types.Property;
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.ConfigBeansUtilities;
import com.sun.enterprise.config.serverbeans.ServerTags;
import com.sun.enterprise.container.common.spi.util.JavaEEIOUtils;
import com.sun.enterprise.deployment.RunAsIdentityDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.deployment.WebComponentDescriptor;
import com.sun.enterprise.deployment.WebServiceEndpoint;
import com.sun.enterprise.deployment.WebServicesDescriptor;
import com.sun.enterprise.deployment.runtime.web.SunWebApp;
import com.sun.enterprise.deployment.web.LoginConfiguration;
import com.sun.enterprise.deployment.web.SecurityConstraint;
import com.sun.enterprise.deployment.web.ServletFilterMapping;
import com.sun.enterprise.deployment.web.UserDataConstraint;
import com.sun.enterprise.deployment.web.WebResourceCollection;
import com.sun.enterprise.security.ee.SecurityUtil;
import com.sun.enterprise.security.integration.RealmInitializer;
import com.sun.enterprise.util.StringUtils;
import com.sun.enterprise.web.deploy.LoginConfigDecorator;
import com.sun.enterprise.web.pwc.PwcWebModule;
import com.sun.enterprise.web.session.PersistenceType;
import com.sun.enterprise.web.session.SessionCookieConfig;
import com.sun.web.security.RealmAdapter;
import fish.payara.jacc.JaccConfigurationFactory;
/**
* Class representing a web module for use by the Application Server.
*/
public class WebModule extends PwcWebModule implements Context {
// ----------------------------------------------------- Class Variables
private static final Logger logger = LogFacade.getLogger();
protected static final ResourceBundle rb = logger.getResourceBundle();
private static final String ALTERNATE_FROM = "from=";
private static final String ALTERNATE_DOCBASE = "dir=";
private static final Base64.Encoder encoder = Base64.getMimeEncoder();
private static final Base64.Decoder decoder = Base64.getMimeDecoder();
private static final String WS_SERVLET_CONTEXT_LISTENER =
"com.sun.xml.ws.transport.http.servlet.WSServletContextListener";
// ----------------------------------------------------- Instance Variables
// Object containing sun-web.xml information
private SunWebAppImpl iasBean = null;
//locale-charset-info tag from sun-web.xml
private LocaleCharsetMap[] _lcMap = null;
/**
* Is the default-web.xml parsed?
*/
private boolean hasBeenXmlConfigured = false;
private WebContainer webContainer;
private final Map<String,AdHocServletInfo> adHocPaths;
private boolean hasAdHocPaths;
private final Map<String,AdHocServletInfo> adHocSubtrees;
private boolean hasAdHocSubtrees;
private StandardPipeline adHocPipeline;
// File encoding of static resources
private String fileEncoding;
/**
* Cached findXXX results
*/
protected Object[] cachedFinds;
private Application bean;
private WebBundleDescriptor webBundleDescriptor;
private boolean hasStarted = false;
private String compEnvId = null;
private ServerContext serverContext = null;
private ServletProbeProvider servletProbeProvider = null;
private SessionProbeProvider sessionProbeProvider = null;
private WebModuleProbeProvider webModuleProbeProvider = null;
private JavaEEIOUtils javaEEIOUtils;
// The id of the parent container (i.e., virtual server) on which this
// web module was deployed
private String vsId;
private String monitoringNodeName;
private WebModuleConfig wmInfo;
// true if standalone WAR, false if embedded in EAR file
private boolean isStandalone = true;
private ServiceLocator services;
/**
* Constructor.
*/
public WebModule() {
this(null);
}
public WebModule(ServiceLocator services) {
super();
this.services = services;
this.adHocPaths = new HashMap<String,AdHocServletInfo>();
this.adHocSubtrees = new HashMap<String,AdHocServletInfo>();
this.adHocPipeline = new StandardPipeline(this);
this.adHocPipeline.setBasic(new AdHocContextValve(this));
notifyContainerListeners = false;
}
/**
* set the sun-web.xml config bean
*/
public void setIasWebAppConfigBean(SunWebAppImpl iasBean) {
this.iasBean = iasBean;
}
/**
* gets the sun-web.xml config bean
*/
public SunWebAppImpl getIasWebAppConfigBean() {
return iasBean;
}
/**
* Gets the web container in which this web module was loaded.
*
* @return the web container in which this web module was loaded
*/
public WebContainer getWebContainer() {
return webContainer;
}
/**
* Sets the web container in which this web module was loaded.
*
*/
public void setWebContainer(WebContainer webContainer) {
this.webContainer = webContainer;
this.servletProbeProvider = webContainer.getServletProbeProvider();
this.sessionProbeProvider = webContainer.getSessionProbeProvider();
this.webModuleProbeProvider =
webContainer.getWebModuleProbeProvider();
this.javaEEIOUtils =
webContainer.getJavaEEIOUtils();
}
public void setWebModuleConfig(WebModuleConfig wmInfo) {
this.wmInfo = wmInfo;
}
public WebModuleConfig getWebModuleConfig() {
return wmInfo;
}
void setMonitoringNodeName(String monitoringNodeName) {
this.monitoringNodeName = monitoringNodeName;
}
public String getMonitoringNodeName() {
return monitoringNodeName;
}
/**
* Sets the parameter encoding (i18n) info from web.xml, sun-web.xml, glassfish-web.xml and payara-web.xml.
*/
public void setI18nInfo() {
if (webBundleDescriptor != null) {
String reqEncoding = webBundleDescriptor.getRequestCharacterEncoding();
if (reqEncoding != null) {
setRequestCharacterEncoding(reqEncoding);
}
String resEncoding = webBundleDescriptor.getResponseCharacterEncoding();
if (resEncoding != null) {
setResponseCharacterEncoding(resEncoding);
}
}
if (iasBean == null) {
return;
}
if (iasBean.isParameterEncoding()) {
formHintField = iasBean.getAttributeValue(
SunWebApp.PARAMETER_ENCODING,
SunWebApp.FORM_HINT_FIELD);
defaultCharset = iasBean.getAttributeValue(
SunWebApp.PARAMETER_ENCODING,
SunWebApp.DEFAULT_CHARSET);
}
LocaleCharsetInfo lcinfo = iasBean.getLocaleCharsetInfo();
if (lcinfo != null) {
if (lcinfo.getAttributeValue(
LocaleCharsetInfo.DEFAULT_LOCALE) != null) {
logger.warning(LogFacade.DEFAULT_LOCALE_DEPRECATED);
}
/*
* <parameter-encoding> subelem of <sun-web-app> takes precedence
* over that of <locale-charset-info>
*/
if (lcinfo.isParameterEncoding()
&& !iasBean.isParameterEncoding()) {
formHintField = lcinfo.getAttributeValue(
LocaleCharsetInfo.PARAMETER_ENCODING,
LocaleCharsetInfo.FORM_HINT_FIELD);
defaultCharset = lcinfo.getAttributeValue(
LocaleCharsetInfo.PARAMETER_ENCODING,
LocaleCharsetInfo.DEFAULT_CHARSET);
}
_lcMap = lcinfo.getLocaleCharsetMap();
}
if (defaultCharset != null) {
setRequestCharacterEncoding(defaultCharset);
setResponseCharacterEncoding(defaultCharset);
}
}
/**
* return locale-charset-map
*/
public LocaleCharsetMap[] getLocaleCharsetMap() {
return _lcMap;
}
/**
* Returns true if this web module specifies a locale-charset-map in its
* sun-web.xml, false otherwise.
*
* @return true if this web module specifies a locale-charset-map in its
* sun-web.xml, false otherwise
*/
@Override
public boolean hasLocaleToCharsetMapping() {
LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
return (locCharsetMap != null && locCharsetMap.length > 0);
}
/**
* Matches the given request locales against the charsets specified in
* the locale-charset-map of this web module's sun-web.xml, and returns
* the first matching charset.
*
* @param locales Request locales
*
* @return First matching charset, or null if this web module does not
* specify any locale-charset-map in its sun-web.xml, or no match was
* found
*/
@Override
public String mapLocalesToCharset(Enumeration locales) {
String encoding = null;
LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
if (locCharsetMap != null && locCharsetMap.length > 0) {
/*
* Check to see if there is a match between the request
* locales (in preference order) and the locales in the
* locale-charset-map.
*/
boolean matchFound = false;
while (locales.hasMoreElements() && !matchFound) {
Locale reqLoc = (Locale) locales.nextElement();
for (int i=0; i<locCharsetMap.length && !matchFound; i++) {
String language = locCharsetMap[i].getAttributeValue(
LocaleCharsetMap.LOCALE);
if (language == null || "".equals(language)) {
continue;
}
String country = null;
int index = language.indexOf('_');
if (index != -1) {
country = language.substring(index+1);
language = language.substring(0, index);
}
Locale mapLoc = null;
if (country != null) {
mapLoc = new Locale(language, country);
} else {
mapLoc = new Locale(language);
}
if (mapLoc.equals(reqLoc)) {
/*
* Match found. Get the charset to which the
* matched locale maps.
*/
encoding = locCharsetMap[i].getAttributeValue(
LocaleCharsetMap.CHARSET);
matchFound = true;
}
}
}
}
return encoding;
}
/**
* Creates an ObjectInputStream that provides special deserialization
* logic for classes that are normally not serializable (such as
* javax.naming.Context).
*/
@Override
public ObjectInputStream createObjectInputStream(InputStream is)
throws IOException {
ObjectInputStream ois = null;
Loader loader = getLoader();
if (loader != null) {
ClassLoader classLoader = loader.getClassLoader();
if (classLoader != null) {
try {
ois = javaEEIOUtils.createObjectInputStream(
is, true, classLoader, getUniqueId());
} catch (Exception e) {
logger.log(Level.SEVERE,
LogFacade.CREATE_CUSTOM_OBJECT_INTPUT_STREAM_ERROR, e);
}
}
}
if (ois == null) {
ois = new ObjectInputStream(is);
}
return ois;
}
/**
* Creates an ObjectOutputStream that provides special serialization
* logic for classes that are normally not serializable (such as
* javax.naming.Context).
*/
@Override
public ObjectOutputStream createObjectOutputStream(OutputStream os)
throws IOException {
ObjectOutputStream oos = null;
try {
oos = javaEEIOUtils.createObjectOutputStream(os, true);
} catch (IOException ioe) {
logger.log(Level.SEVERE,
LogFacade.CREATE_CUSTOM_BOJECT_OUTPUT_STREAM_ERROR, ioe);
oos = new ObjectOutputStream(os);
}
return oos;
}
/**
* Set to <code>true</code> when the default-web.xml has been read for
* this module.
*/
public void setXmlConfigured(boolean hasBeenXmlConfigured){
this.hasBeenXmlConfigured = hasBeenXmlConfigured;
}
/**
* Return <code>true</code> if the default=web.xml has been read for
* this module.
*/
public boolean hasBeenXmlConfigured(){
return hasBeenXmlConfigured;
}
/**
* Cache the result of doing findXX on this object
* NOTE: this method MUST be used only when loading/using
* the content of default-web.xml
*/
public void setCachedFindOperation(Object[] cachedFinds){
this.cachedFinds = cachedFinds;
}
/**
* Return the cached result of doing findXX on this object
* NOTE: this method MUST be used only when loading/using
* the content of default-web.xml
*/
public Object[] getCachedFindOperation(){
return cachedFinds;
}
@Override
public void setRealm(Realm realm) {
if ((realm != null) && !(realm instanceof RealmAdapter)) {
logger.log(Level.SEVERE, LogFacade.IGNORE_INVALID_REALM,
new Object[] { realm.getClass().getName(),
RealmAdapter.class.getName() });
} else {
super.setRealm(realm);
}
}
/**
* Starts this web module.
*/
@Override
public synchronized void start() throws LifecycleException {
// Get interestList of ServletContainerInitializers present, if any.
List<Object> orderingList = null;
boolean hasOthers = false;
Map<String, String> webFragmentMap = Collections.emptyMap();
if (webBundleDescriptor != null) {
AbsoluteOrderingDescriptor aod =
((WebBundleDescriptorImpl)webBundleDescriptor).getAbsoluteOrderingDescriptor();
if (aod != null) {
orderingList = aod.getOrdering();
hasOthers = aod.hasOthers();
}
webFragmentMap = webBundleDescriptor.getJarNameToWebFragmentNameMap();
}
boolean servletInitializersEnabled = true;
if (webBundleDescriptor != null) {
servletInitializersEnabled = webBundleDescriptor.getServletInitializersEnabled();
}
Iterable<ServletContainerInitializer> allInitializers =
ServletContainerInitializerUtil.getServletContainerInitializers(
webFragmentMap, orderingList, hasOthers,
wmInfo.getAppClassLoader(), servletInitializersEnabled);
setServletContainerInitializerInterestList(allInitializers);
DeploymentContext dc = getWebModuleConfig().getDeploymentContext();
if (dc != null) {
directoryDeployed =
Boolean.valueOf(dc.getAppProps().getProperty(ServerTags.DIRECTORY_DEPLOYED));
}
if (webBundleDescriptor != null) {
showArchivedRealPathEnabled = webBundleDescriptor.isShowArchivedRealPathEnabled();
servletReloadCheckSecs = webBundleDescriptor.getServletReloadCheckSecs();
String reqEncoding = webBundleDescriptor.getRequestCharacterEncoding();
if (reqEncoding != null) {
setRequestCharacterEncoding(reqEncoding);
}
String resEncoding = webBundleDescriptor.getResponseCharacterEncoding();
if (resEncoding != null) {
setResponseCharacterEncoding(resEncoding);
}
}
// Start and register Tomcat mbeans
super.start();
// Configure catalina listeners and valves. This can only happen
// after this web module has been started, in order to be able to
// load the specified listener and valve classes.
configureValves();
configureCatalinaProperties();
webModuleStartedEvent();
if (directoryListing) {
setDirectoryListing(directoryListing);
}
hasStarted = true;
}
/**
* Stops this web module.
*/
@Override
public void stop() throws LifecycleException {
// Unregister monitoring mbeans only if this web module was
// successfully started, because if stop() is called during an
// aborted start(), no monitoring mbeans will have been registered
if (hasStarted) {
webModuleStoppedEvent();
hasStarted = false;
}
// Stop and unregister Tomcat mbeans
super.stop(getWebContainer().isShutdown());
}
@Override
protected void contextListenerStart() {
ServletContext servletContext = getServletContext();
WebBundleDescriptor webBundleDescriptor = getWebBundleDescriptor();
JaccConfigurationFactory jaccConfigurationFactory = getJaccConfigurationFactory();
try {
// For JSF injection
servletContext.setAttribute(
DEPLOYMENT_CONTEXT_ATTRIBUTE,
getWebModuleConfig().getDeploymentContext());
// Null check for OSGi/HTTP
if (webBundleDescriptor != null) {
servletContext.setAttribute(IS_DISTRIBUTABLE_ATTRIBUTE, webBundleDescriptor.isDistributable());
webBundleDescriptor.setAppContextId(getAppContextId(servletContext));
if (jaccConfigurationFactory != null) {
// Add a mapping from the JACC context Id, which is not available to the application yet at this point
// to the Servlet based application Id, which the application uses
jaccConfigurationFactory.addContextIdMapping(
webBundleDescriptor.getAppContextId(), // Servlet application context Id
getContextID(webBundleDescriptor)); // JACC context Id
}
}
servletContext.setAttribute(
ENABLE_HA_ATTRIBUTE,
webContainer.getServerConfigLookup().calculateWebAvailabilityEnabledFromConfig(this));
super.contextListenerStart();
} finally {
servletContext.removeAttribute(DEPLOYMENT_CONTEXT_ATTRIBUTE);
servletContext.removeAttribute(IS_DISTRIBUTABLE_ATTRIBUTE);
servletContext.removeAttribute(ENABLE_HA_ATTRIBUTE);
}
servletRegisMap.values()
.stream()
.filter(DynamicWebServletRegistrationImpl.class::isInstance)
.map(DynamicWebServletRegistrationImpl.class::cast)
.forEach(DynamicWebServletRegistrationImpl::postProcessAnnotations);
if (jaccConfigurationFactory != null && webBundleDescriptor != null) {
if (jaccConfigurationFactory.getContextProviderByPolicyContextId(getContextID(webBundleDescriptor)) != null) {
webBundleDescriptor.setPolicyModified(true);
}
}
webContainer.afterServletContextInitializedEvent(webBundleDescriptor);
}
private String getAppContextId(ServletContext servletContext) {
return servletContext.getVirtualServerName() + " " + servletContext.getContextPath();
}
private JaccConfigurationFactory getJaccConfigurationFactory() {
try {
PolicyConfigurationFactory policyConfigurationFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
if (policyConfigurationFactory instanceof JaccConfigurationFactory) {
return (JaccConfigurationFactory) policyConfigurationFactory;
}
} catch (ClassNotFoundException | PolicyContextException e) {
// Ignore
}
return null;
}
@Override
protected Types getTypes() {
if (wmInfo.getDeploymentContext()!=null) {
return wmInfo.getDeploymentContext().getTransientAppMetaData(Types.class.getName(), Types.class);
} else {
return null;
}
}
@Override
protected void callServletContainerInitializers()
throws LifecycleException {
WebComponentInvocation inv = new WebComponentInvocation(this);
try {
webContainer.getInvocationManager().preInvoke(inv);
super.callServletContainerInitializers();
}
finally {
webContainer.getInvocationManager().postInvoke(inv);
}
if (!isJsfApplication() && !contextListeners.isEmpty()) {
/*
* Remove any JSF related ServletContextListeners from
* non-JSF apps.
* This can be done reliably only after all
* ServletContainerInitializers have been invoked, because
* system-wide ServletContainerInitializers may be invoked in
* any order, and it is only after JSF's FacesInitializer has
* been invoked that isJsfApplication(), which checks for the
* existence of a mapping to the FacesServlet in the app, may
* be used reliably because such mapping would have been added
* by JSF's FacesInitializer. See also IT 10223
*/
ArrayList<ServletContextListener> listeners =
new ArrayList<ServletContextListener>(contextListeners);
String listenerClassName = null;
for (ServletContextListener listener : listeners) {
if (listener instanceof
StandardContext.RestrictedServletContextListener) {
listenerClassName = ((StandardContext.RestrictedServletContextListener) listener).getNestedListener().getClass().getName();
} else {
listenerClassName = listener.getClass().getName();
}
/*
* TBD: Retrieve listener class name from JSF's TldProvider
*/
if ("com.sun.faces.config.ConfigureListener".equals(
listenerClassName)) {
contextListeners.remove(listener);
}
}
}
}
/**
* Sets the virtual server parent of this web module, and passes it on to
* this web module's realm adapter..
*
* @param container The virtual server parent
*/
@Override
public void setParent(Container container) {
super.setParent(container);
if (container instanceof VirtualServer) {
vsId = ((VirtualServer) container).getID();
}
// The following assumes that the realm has been set on this WebModule
// before the WebModule is added as a child to the virtual server on
// which it is being deployed.
/*RealmAdapter ra = (RealmAdapter) getRealm();
if (ra != null) {
1 ra.setVirtualServer(container);
}*/
Realm ra = getRealm();
if (ra != null && ra instanceof RealmInitializer) {
((RealmInitializer) ra).setVirtualServer(container);
}
}
/**
* Indicates whether this web module contains any ad-hoc paths.
*
* An ad-hoc path is a servlet path that is mapped to a servlet
* not declared in the web module's deployment descriptor.
*
* A web module all of whose mappings are for ad-hoc paths is called an
* ad-hoc web module.
*
* @return true if this web module contains any ad-hoc paths, false
* otherwise
*/
@Override
public boolean hasAdHocPaths() {
return this.hasAdHocPaths;
}
/**
* Indicates whether this web module contains any ad-hoc subtrees.
*
* @return true if this web module contains any ad-hoc subtrees, false
* otherwise
*/
public boolean hasAdHocSubtrees() {
return this.hasAdHocSubtrees;
}
/*
* Adds the given ad-hoc path and subtree, along with information about
* the servlet that will be responsible for servicing it, to this web
* module.
*
* @param path The ad-hoc path to add
* @param subtree The ad-hoc subtree path to add
* @param servletInfo Information about the servlet that is responsible
* for servicing the given ad-hoc path
*/
void addAdHocPathAndSubtree(String path,
String subtree,
AdHocServletInfo servletInfo) {
if (path == null && subtree == null) {
return;
}
Wrapper adHocWrapper = (Wrapper)
findChild(servletInfo.getServletName());
if (adHocWrapper == null) {
adHocWrapper = createAdHocWrapper(servletInfo);
addChild(adHocWrapper);
}
if (path != null) {
adHocPaths.put(path, servletInfo);
hasAdHocPaths = true;
}
if (subtree != null) {
adHocSubtrees.put(subtree, servletInfo);
hasAdHocSubtrees = true;
}
}
/*
* Adds the given ad-hoc path to servlet mappings to this web module.
*
* @param newPaths Mappings of ad-hoc paths to the servlets responsible
* for servicing them
*/
void addAdHocPaths(Map<String, AdHocServletInfo> newPaths) {
if (newPaths == null || newPaths.isEmpty()) {
return;
}
for (Map.Entry<String, AdHocServletInfo> entry : newPaths.entrySet()) {
AdHocServletInfo servletInfo = entry.getValue();
Wrapper adHocWrapper = (Wrapper)
findChild(servletInfo.getServletName());
if(adHocWrapper == null) {
adHocWrapper = createAdHocWrapper(servletInfo);
addChild(adHocWrapper);
}
adHocPaths.put(entry.getKey(), servletInfo);
}
hasAdHocPaths = true;
}
/*
* Adds the given ad-hoc subtree path to servlet mappings to this web
* module.
*
* @param newSubtrees Mappings of ad-hoc subtree paths to the servlets
* responsible for servicing them
*/
void addAdHocSubtrees(Map<String, AdHocServletInfo> newSubtrees) {
if (newSubtrees == null || newSubtrees.isEmpty()) {
return;
}
for (Map.Entry<String, AdHocServletInfo> entry : newSubtrees.entrySet()) {
AdHocServletInfo servletInfo = entry.getValue();
Wrapper adHocWrapper = (Wrapper)findChild(servletInfo.getServletName());
if(adHocWrapper == null) {
adHocWrapper = createAdHocWrapper(servletInfo);
addChild(adHocWrapper);
}
adHocSubtrees.put(entry.getKey(), servletInfo);
}
hasAdHocSubtrees = true;
}
/*
* Gets the ad-hoc path to servlet mappings managed by this web module.
*
* @return The ad-hoc path to servlet mappings managed by this web
* module.
*/
Map<String, AdHocServletInfo> getAdHocPaths() {
return adHocPaths;
}
/*
* Gets the ad-hoc subtree path to servlet mappings managed by this
* web module.
*
* @return The ad-hoc subtree path to servlet mappings managed by
* this web module.
*/
Map<String, AdHocServletInfo> getAdHocSubtrees() {
return adHocSubtrees;
}
/**
* Returns the name of the ad-hoc servlet responsible for servicing the
* given path.
*
* @param path The path whose associated ad-hoc servlet is needed
*
* @return The name of the ad-hoc servlet responsible for servicing the
* given path, or null if the given path does not represent an ad-hoc
* path
*/
@Override
public String getAdHocServletName(String path) {
if (!hasAdHocPaths() && !hasAdHocSubtrees()) {
return null;
}
AdHocServletInfo servletInfo = null;
// Check if given path matches any of the ad-hoc paths (exact match)
if (path == null) {
servletInfo = adHocPaths.get("");
} else {
servletInfo = adHocPaths.get(path);
}
// Check if given path starts with any of the ad-hoc subtree paths
if (servletInfo == null && path != null && hasAdHocSubtrees()) {
for(String adHocSubtree : adHocSubtrees.keySet()) {
if(path.startsWith(adHocSubtree)) {
servletInfo = adHocSubtrees.get(adHocSubtree);
break;
}
}
}
if (servletInfo != null) {
return servletInfo.getServletName();
} else {
return null;
}
}
/*
* Removes the given ad-hoc path from this web module.
*
* @param path The ad-hoc path to remove
*/
void removeAdHocPath(String path) {
if (path == null) {
return;
}
adHocPaths.remove(path);
if (adHocPaths.isEmpty()) {
this.hasAdHocPaths = false;
}
}
/*
* Removes the given ad-hoc path from this web module.
*
* @param subtree The ad-hoc subtree to remove
*/
void removeAdHocSubtree(String subtree) {