/
WebApp.java
612 lines (562 loc) · 18.8 KB
/
WebApp.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
/*
* Copyright 2007 Alin Dreghiciu.
*
* 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
*
* http://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.ops4j.pax.web.extender.war.internal.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.osgi.framework.Bundle;
import org.osgi.service.http.HttpContext;
import org.ops4j.lang.NullArgumentException;
import org.ops4j.pax.web.extender.war.internal.WebAppVisitor;
/**
* Root element of web.xml.
*
* @author Alin Dreghiciu
* @since 0.3.0, December 27, 2007
*/
public class WebApp
{
/**
* Application display name.
*/
private String m_displayName;
/**
* Context name.
*/
private String m_contextName;
/**
* Session timeout.
*/
private String m_sessionTimeout;
/**
* Bundle that contains the parsed web.xml.
* Is not set by the parser but by the webXmlObserver after the parsing is done.
*/
private Bundle m_bundle;
/**
* The http context used during registration of error page.
* Is not set by the parser but by the registration visitor during registration.
*/
private HttpContext m_httpContext;
/**
* Servlets.
*/
private final Map<String, WebAppServlet> m_servlets;
/**
* Mapping between servlet name and servlet mapping.
*/
private final Map<String, Set<WebAppServletMapping>> m_servletMappings;
/**
* Filters.
*/
private final Map<String, WebAppFilter> m_filters;
/**
* Mapping between filter name and filter mapping.
*/
private final Map<String, Set<WebAppFilterMapping>> m_filterMappings;
/**
* Filters order. List in the order the filters should be applied. When read from an web xml it should respect
* SRV.6.2.4 section in servlet specs which is the order defined in filter mappings.
*/
private final List<String> m_orderedFilters;
/**
* Context parameters.
*/
private final Set<WebAppInitParam> m_contextParams;
/**
* Mime mappings.
*/
private final Set<WebAppMimeMapping> m_mimeMappings;
/**
* Listeners.
*/
private final List<WebAppListener> m_listeners;
/**
* Error pages.
*/
private final List<WebAppErrorPage> m_errorPages;
/**
* Welcome files.
*/
private final List<String> m_welcomeFiles;
/**
* SecurityConstraints
*/
private final List<WebAppConstraintMapping> m_constraintsMapping;
private final List<WebAppSecurityRole> m_securityRoles;
private final List<WebAppLoginConfig> m_loginConfig;
/**
* Creates a new web app.
*/
public WebApp()
{
m_servlets = new HashMap<String, WebAppServlet>();
m_servletMappings = new HashMap<String, Set<WebAppServletMapping>>();
m_filters = new LinkedHashMap<String, WebAppFilter>();
m_filterMappings = new HashMap<String, Set<WebAppFilterMapping>>();
m_orderedFilters = new ArrayList<String>();
m_listeners = new ArrayList<WebAppListener>();
m_errorPages = new ArrayList<WebAppErrorPage>();
m_contextParams = new HashSet<WebAppInitParam>();
m_mimeMappings = new HashSet<WebAppMimeMapping>();
m_welcomeFiles = new ArrayList<String>();
m_constraintsMapping = new ArrayList<WebAppConstraintMapping>();
m_securityRoles = new ArrayList<WebAppSecurityRole>();
m_loginConfig = new ArrayList<WebAppLoginConfig>();
}
/**
* Setter.
*
* @param displayName value to set
*/
public void setDisplayName( final String displayName )
{
m_displayName = displayName;
}
/**
* Setter.
*
* @param contextName value to set. Cannot be null.
*
* @throws NullArgumentException if context name is null
*/
public void setContextName( final String contextName )
{
NullArgumentException.validateNotNull( contextName, "Context name" );
m_contextName = contextName;
// set the context name into the context params
final WebAppInitParam initParam = new WebAppInitParam();
initParam.setParamName( "webapp.context" );
initParam.setParamValue( contextName );
m_contextParams.add( initParam );
}
/**
* Setter.
*
* @param minutes session timeout
*/
public void setSessionTimeout( final String minutes )
{
m_sessionTimeout = minutes;
}
/**
* Getter.
*
* @return session timeout in minutes
*/
public String getSessionTimeout()
{
return m_sessionTimeout;
}
/**
* Getter.
*
* @return bundle
*/
public Bundle getBundle()
{
return m_bundle;
}
/**
* Setter.
*
* @param bundle value to set
*/
public void setBundle( Bundle bundle )
{
m_bundle = bundle;
}
/**
* Add a servlet.
*
* @param servlet to add
*
* @throws NullArgumentException if servlet, servlet name or servlet class is null
*/
public void addServlet( final WebAppServlet servlet )
{
NullArgumentException.validateNotNull( servlet, "Servlet" );
NullArgumentException.validateNotNull( servlet.getServletName(), "Servlet name" );
NullArgumentException.validateNotNull( servlet.getServletClass(), "Servlet class" );
m_servlets.put( servlet.getServletName(), servlet );
// add aliases for servlet mappings added before servlet
for( WebAppServletMapping mapping : getServletMappings( servlet.getServletName() ) )
{
servlet.addUrlPattern( mapping.getUrlPattern() );
}
}
/**
* Add a servlet mapping.
*
* @param servletMapping to add
*
* @throws NullArgumentException if servlet mapping, servlet name or url pattern is null
*/
public void addServletMapping( final WebAppServletMapping servletMapping )
{
NullArgumentException.validateNotNull( servletMapping, "Servlet mapping" );
NullArgumentException.validateNotNull( servletMapping.getServletName(), "Servlet name" );
NullArgumentException.validateNotNull( servletMapping.getUrlPattern(), "Url pattern" );
Set<WebAppServletMapping> servletMappings = m_servletMappings.get( servletMapping.getServletName() );
if( servletMappings == null )
{
servletMappings = new HashSet<WebAppServletMapping>();
m_servletMappings.put( servletMapping.getServletName(), servletMappings );
}
servletMappings.add( servletMapping );
final WebAppServlet servlet = m_servlets.get( servletMapping.getServletName() );
// can be that the servlet is not yet added
if( servlet != null )
{
servlet.addUrlPattern( servletMapping.getUrlPattern() );
}
}
/**
* Returns a servlet mapping by servlet name.
*
* @param servletName servlet name
*
* @return array of servlet mappings for requested servlet name
*/
private WebAppServletMapping[] getServletMappings( final String servletName )
{
final Set<WebAppServletMapping> servletMappings = m_servletMappings.get( servletName );
if( servletMappings == null )
{
return new WebAppServletMapping[0];
}
return servletMappings.toArray( new WebAppServletMapping[servletMappings.size()] );
}
/**
* Add a filter.
*
* @param filter to add
*
* @throws NullArgumentException if filter, filter name or filter class is null
*/
public void addFilter( final WebAppFilter filter )
{
NullArgumentException.validateNotNull( filter, "Filter" );
NullArgumentException.validateNotNull( filter.getFilterName(), "Filter name" );
NullArgumentException.validateNotNull( filter.getFilterClass(), "Filter class" );
m_filters.put( filter.getFilterName(), filter );
// add url patterns and servlet names for filter mappings added before filter
for( WebAppFilterMapping mapping : getFilterMappings( filter.getFilterName() ) )
{
if( mapping.getUrlPattern() != null && mapping.getUrlPattern().trim().length() > 0 )
{
filter.addUrlPattern( mapping.getUrlPattern() );
}
if( mapping.getServletName() != null && mapping.getServletName().trim().length() > 0 )
{
filter.addServletName( mapping.getServletName() );
}
}
}
/**
* Add a filter mapping.
*
* @param filterMapping to add
*
* @throws NullArgumentException if filter mapping or filter name is null
*/
public void addFilterMapping( final WebAppFilterMapping filterMapping )
{
NullArgumentException.validateNotNull( filterMapping, "Filter mapping" );
NullArgumentException.validateNotNull( filterMapping.getFilterName(), "Filter name" );
final String filterName = filterMapping.getFilterName();
if( !m_orderedFilters.contains( filterName ) )
{
m_orderedFilters.add( filterName );
}
Set<WebAppFilterMapping> filterMappings = m_filterMappings.get( filterName );
if( filterMappings == null )
{
filterMappings = new HashSet<WebAppFilterMapping>();
m_filterMappings.put( filterName, filterMappings );
}
filterMappings.add( filterMapping );
final WebAppFilter filter = m_filters.get( filterName );
// can be that the filter is not yet added
if( filter != null )
{
if( filterMapping.getUrlPattern() != null && filterMapping.getUrlPattern().trim().length() > 0 )
{
filter.addUrlPattern( filterMapping.getUrlPattern() );
}
if( filterMapping.getServletName() != null && filterMapping.getServletName().trim().length() > 0 )
{
filter.addServletName( filterMapping.getServletName() );
}
}
}
/**
* Returns filter mappings by filter name.
*
* @param filterName filter name
*
* @return array of filter mappings for requested filter name
*/
private WebAppFilterMapping[] getFilterMappings( final String filterName )
{
final Set<WebAppFilterMapping> filterMappings = m_filterMappings.get( filterName );
if( filterMappings == null )
{
return new WebAppFilterMapping[0];
}
return filterMappings.toArray( new WebAppFilterMapping[filterMappings.size()] );
}
/**
* Add a listener.
*
* @param listener to add
*
* @throws NullArgumentException if listener or listener class is null
*/
public void addListener( final WebAppListener listener )
{
NullArgumentException.validateNotNull( listener, "Listener" );
NullArgumentException.validateNotNull( listener.getListenerClass(), "Listener class" );
m_listeners.add( listener );
}
/**
* Add an error page.
*
* @param errorPage to add
*
* @throws NullArgumentException if error page is null or both error type and exception code is null
*/
public void addErrorPage( final WebAppErrorPage errorPage )
{
NullArgumentException.validateNotNull( errorPage, "Error page" );
if( errorPage.getErrorCode() == null && errorPage.getExceptionType() == null )
{
throw new NullPointerException( "At least one of error type or exception code must be set" );
}
m_errorPages.add( errorPage );
}
/**
* Add a welcome file.
*
* @param welcomeFile to add
*
* @throws NullArgumentException if welcome file is null or empty
*/
public void addWelcomeFile( final String welcomeFile )
{
NullArgumentException.validateNotEmpty( welcomeFile, "Welcome file" );
m_welcomeFiles.add( welcomeFile );
}
/**
* Return all welcome files.
*
* @return an array of all welcome files
*/
public String[] getWelcomeFiles()
{
return m_welcomeFiles.toArray( new String[m_welcomeFiles.size()] );
}
/**
* Add a context param.
*
* @param contextParam to add
*
* @throws NullArgumentException if context param, param name or param value is null
*/
public void addContextParam( final WebAppInitParam contextParam )
{
NullArgumentException.validateNotNull( contextParam, "Context param" );
NullArgumentException.validateNotNull( contextParam.getParamName(), "Context param name" );
NullArgumentException.validateNotNull( contextParam.getParamValue(), "Context param value" );
m_contextParams.add( contextParam );
}
/**
* Return all context params.
*
* @return an array of all context params
*/
public WebAppInitParam[] getContextParams()
{
return m_contextParams.toArray( new WebAppInitParam[m_contextParams.size()] );
}
/**
* Add a mime mapping.
*
* @param mimeMapping to add
*
* @throws NullArgumentException if mime mapping, extension or mime type is null
*/
public void addMimeMapping( final WebAppMimeMapping mimeMapping )
{
NullArgumentException.validateNotNull( mimeMapping, "Mime mapping" );
NullArgumentException.validateNotNull( mimeMapping.getExtension(), "Mime mapping extension" );
NullArgumentException.validateNotNull( mimeMapping.getMimeType(), "Mime mapping type" );
m_mimeMappings.add( mimeMapping );
}
/**
* Add a security constraint
*
* @param securityConstraint
*
* @throws NullArgumentException if security constraint is null
*/
public void addConstraintMapping( final WebAppConstraintMapping constraintMapping ) {
NullArgumentException.validateNotNull( constraintMapping, "constraint mapping");
m_constraintsMapping.add(constraintMapping);
}
/**
* @return
*/
public WebAppConstraintMapping[] getConstraintMappings() {
return m_constraintsMapping.toArray(new WebAppConstraintMapping[m_constraintsMapping.size()]);
}
/**
* Adds a security role
*
* @param securityRole
*/
public void addSecurityRole( final WebAppSecurityRole securityRole ) {
NullArgumentException.validateNotNull(securityRole, "Security Role");
m_securityRoles.add(securityRole);
}
/**
* @return
*/
public WebAppSecurityRole[] getSecurityRoles() {
return m_securityRoles.toArray(new WebAppSecurityRole[m_securityRoles.size()]);
}
/**
* Adds a login config
*
* @param loginConfig
*/
public void addLoginConfig( final WebAppLoginConfig loginConfig ) {
NullArgumentException.validateNotNull(loginConfig, "Login Config");
NullArgumentException.validateNotNull(loginConfig.getAuthMethod(), "Login Config Authorization Method");
NullArgumentException.validateNotNull(loginConfig.getRealmName(), "Login Config Realm Name");
m_loginConfig.add(loginConfig);
}
/**
* @return
*/
public WebAppLoginConfig[] getLoginConfigs() {
return m_loginConfig.toArray(new WebAppLoginConfig[m_loginConfig.size()]);
}
/**
* Return all mime mappings.
*
* @return an array of all mime mappings
*/
public WebAppMimeMapping[] getMimeMappings()
{
return m_mimeMappings.toArray( new WebAppMimeMapping[m_mimeMappings.size()] );
}
/**
* Getter.
*
* @return http context
*/
public HttpContext getHttpContext()
{
return m_httpContext;
}
/**
* Setter.
*
* @param httpContext value to set
*/
public void setHttpContext( HttpContext httpContext )
{
m_httpContext = httpContext;
}
/**
* Accepts a visitor for inner elements.
*
* @param visitor visitor
*/
public void accept( final WebAppVisitor visitor )
{
visitor.visit( this ); //First do everything else
for( WebAppListener listener : m_listeners )
{
visitor.visit( listener );
}
if( !m_filters.isEmpty() )
{
// first visit the filters with a filter mapping in mapping order
final List<WebAppFilter> remainingFilters = new ArrayList<WebAppFilter>( m_filters.values() );
for( String filterName : m_orderedFilters )
{
final WebAppFilter filter = m_filters.get( filterName );
visitor.visit( filter );
remainingFilters.remove( filter );
}
// then visit filters without a mapping order in the order they were added
for( WebAppFilter filter : remainingFilters )
{
visitor.visit( filter );
}
}
if( !m_servlets.isEmpty() )
{
for( WebAppServlet servlet : getSortedWebAppServlet() ) //Fix for PAXWEB-205
{
visitor.visit( servlet );
}
}
if ( !m_constraintsMapping.isEmpty() ) //Added for PAXWEB-210 - might be a to late for initialization
{
for (WebAppConstraintMapping constraintMapping : m_constraintsMapping) {
visitor.visit(constraintMapping);
}
}
for( WebAppErrorPage errorPage : m_errorPages )
{
visitor.visit( errorPage );
}
}
static final Comparator<WebAppServlet> WebAppServletComparator = new Comparator<WebAppServlet>() {
public int compare(WebAppServlet servlet1, WebAppServlet servlet2) {
return
servlet1.getLoadOnStartup() - servlet2.getLoadOnStartup();
}
};
private Collection<WebAppServlet> getSortedWebAppServlet() {
List <WebAppServlet> servlets = new ArrayList<WebAppServlet>( m_servlets.values() );
Collections.sort( servlets, WebAppServletComparator );
return servlets;
}
@Override
public String toString()
{
return new StringBuffer()
.append( this.getClass().getSimpleName() )
.append( "{" )
.append( "displayName=" ).append( m_displayName )
.append( ",contextName=" ).append( m_contextName )
.append( "}" )
.toString();
}
}