Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c6ccd950e8fa51a9f6bf4bf366df5b56511a3745 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle Corporation and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Oracle Corporation - initial API and implementation
 *******************************************************************************/


package org.eclipse.jst.jsf.common.webxml;

import java.util.List;

import org.eclipse.jst.javaee.core.Description;
import org.eclipse.jst.javaee.core.JavaeeFactory;
import org.eclipse.jst.javaee.core.Listener;
import org.eclipse.jst.javaee.core.ParamValue;
import org.eclipse.jst.javaee.core.UrlPatternType;
import org.eclipse.jst.javaee.web.Filter;
import org.eclipse.jst.javaee.web.FilterMapping;
import org.eclipse.jst.javaee.web.Servlet;
import org.eclipse.jst.javaee.web.ServletMapping;
import org.eclipse.jst.javaee.web.WebApp;
import org.eclipse.jst.javaee.web.WebFactory;


/**
 * Web.xml editing utilities for Java EE.
 *
 * @author Debajit Adhikary
 *
 */
/**
 * @author cbateman
 *
 */
/**
 * @author cbateman
 *
 */
/**
 * @author cbateman
 *
 */
public class WebXmlUtilsForJavaEE
{
    /**
     * @param webapp
     * @param servletName
     * @param servletClass
     * @param loadOnStartup
     */
    public static void addServlet (final WebApp webapp,
                                   final String servletName,
                                   final String servletClass,
                                   final String loadOnStartup)
    {
        if (existsServlet(webapp, servletName, servletClass))
            return;

        // Create new servlet
        final Servlet servlet = WebFactory.eINSTANCE.createServlet();
        servlet.setServletName(servletName);
        servlet.setServletClass(servletClass);
        servlet.setLoadOnStartup(loadOnStartup);

        webapp.getServlets().add(servlet);
    }


    /**
     * @param webapp
     * @param servletName
     * @param servletClass
     * @return true if the servlet exists.
     */
    public static boolean existsServlet (final WebApp webapp,
                                         final String servletName,
                                         final String servletClass)
    {
        return findServlet(webapp, servletName, servletClass) != null;
    }


    /**
     * @param webapp
     * @param servletName
     * @param servletClass
     * @return the servlet or null if not found.
     */
    public static Servlet findServlet (final WebApp webapp,
                                       final String servletName,
                                       final String servletClass)
    {
        for (final Object s : webapp.getServlets())
        {
            final Servlet servlet = (Servlet) s;
            if (servlet.getServletName().equals(servletName) && servlet.getServletClass().equals(servletClass))
            {
                return servlet;
            }
        }

        return null;
    }


    /**
     * @param servletName
     * @param webApp
     * @return the servlet or null if not found.
     */
    public static Servlet findServlet (final String servletName,
                                       final WebApp webApp)
    {
        for (final Object servlet : webApp.getServlets())
        {
            if (((Servlet) servlet).getServletClass().trim().equals(servletName))
                return (Servlet) servlet;
        }

        return null;
    }


    /**
     * @param webApp
     * @param servlet
     */
    public static void removeServlet (final WebApp webApp,
                                      final Servlet servlet)
    {
        webApp.getServlets().remove(servlet);
    }


    /**
     * @param webApp
     * @param servletName
     * @param servletClass
     * @param urlPatternString
     */
    public static void addServletMapping (final WebApp webApp,
                                          final String servletName,
                                          final String servletClass,
                                          final String urlPatternString)
    {
        if (existsServletMapping(webApp, servletName, urlPatternString))
            return;

        // Create new servlet mapping.
        final ServletMapping servletMapping = WebFactory.eINSTANCE.createServletMapping();
        servletMapping.setServletName(servletName);
        servletMapping.getUrlPatterns().add(createUrlPattern(urlPatternString));

        webApp.getServletMappings().add(servletMapping);
    }


    /**
     * @param webApp
     * @param servletName
     * @param urlPatternString
     * @return true if the servlet mapping exists.
     */
    public static boolean existsServletMapping (final WebApp webApp,
                                                final String servletName,
                                                final String urlPatternString)
    {
        return findServletMapping(webApp, servletName, urlPatternString) != null;
    }


    /**
     * @param webApp
     * @param servletName
     * @param urlPatternString
     * @return the servlet mapping or null if doesn't exist.
     */
    public static ServletMapping findServletMapping (final WebApp webApp,
                                                     final String servletName,
                                                     final String urlPatternString)
    {
        for (final Object mapping : webApp.getServletMappings())
        {
            final ServletMapping servletMappingToCheck = (ServletMapping) mapping;
            if (servletMappingToCheck.getServletName().trim().equals(servletName))
            {
                // We found a servlet with the same name. Check for urls
                for (final Object pattern : servletMappingToCheck.getUrlPatterns())
                    if (((UrlPatternType) pattern).getValue().equals(urlPatternString))
                        return servletMappingToCheck;
            }
        }

        return null;
    }

    /**
     * @param webApp
     * @param servlet
     */
    public static void removeServletMappings (final WebApp webApp,
                                              final Servlet servlet)
    {
        final List mappings = webApp.getServletMappings();
        String servletName = servlet.getServletName();

        if (servletName != null)
        {
            servletName = servletName.trim();
            for (int i = mappings.size() - 1; i >= 0; --i)
            {
                final ServletMapping mapping = (ServletMapping) mappings.get(i);
                if (mapping != null && mapping.getServletName() != null && mapping.getServletName().trim().equals(servletName))
                {
                    mappings.remove(mapping);
                }
            }
        }
    }


    /**
     * @param webapp
     * @param filterName
     * @param filterClass
     */
    public static void addFilter (final WebApp webapp,
                                  final String filterName,
                                  final String filterClass)
    {
        if (existsFilter(webapp, filterName, filterClass))
            return;

        webapp.getFilters().add(createFilter(filterName, filterClass));
    }


    /**
     * @param filterName
     * @param filterClass
     * @return the filter or null if it doesn't exist.
     */
    public static Filter createFilter (final String filterName,
                                       final String filterClass)
    {
        final Filter filter = WebFactory.eINSTANCE.createFilter();
        filter.setFilterName(filterName);
        filter.setFilterClass(filterClass);
        return filter;
    }


    /**
     * @param webapp
     * @param filterName
     * @param filterClass
     * @return true if the filter exists.
     */
    public static boolean existsFilter (final WebApp webapp,
                                        final String filterName,
                                        final String filterClass)
    {
        return findFilter(webapp, filterName, filterClass) != null;
    }


    /**
     * @param webapp
     * @param filterName
     * @param filterClass
     * @return the filter or null if not found.
     */
    public static Filter findFilter (final WebApp webapp,
                                     final String filterName,
                                     final String filterClass)
    {
        for (final Object f : webapp.getFilters())
        {
            final Filter filter = (Filter) f;
            if (filter.getFilterName().trim().equals(filterName) && filter.getFilterClass().trim().equals(filterClass))
            {
                return filter;
            }
        }

        return null;
    }


    /**
     * @param webApp
     * @param filterClassName
     * @return the filter or null if not found.
     */
    public static Filter findFilter (final WebApp webApp,
                                     final String filterClassName)
    {
        for (final Object filter : webApp.getFilters())
        {
            if (((Filter) filter).getFilterClass().trim().equals(filterClassName))
                return (Filter) filter;
        }

        return null;
    }


    /**
     * @param webApp
     * @param filter
     */
    public static void removeFilter (final WebApp webApp,
                                     final Filter filter)
    {
        webApp.getFilters().remove(filter);
    }


    /**
     * @param webapp
     * @param filterName
     * @param servletName
     */
    public static void addFilterMapping (final WebApp webapp,
                                         final String filterName,
                                         final String servletName)
    {
        if (existsFilterMapping(webapp, filterName, servletName))
            return;

        webapp.getFilterMappings().add(createFilterMapping(filterName, servletName));
    }


    /**
     * @param filterName
     * @param servletName
     * @return the filter mapping or null if not found.
     */
    public static FilterMapping createFilterMapping (final String filterName,
                                                     final String servletName)
    {
        final FilterMapping filterMapping = WebFactory.eINSTANCE.createFilterMapping();
        filterMapping.setFilterName(filterName);
        filterMapping.getServletNames().add(servletName);

        return filterMapping;
    }


    /**
     * @param webapp
     * @param filterName
     * @param servletName
     * @return true if the filter mapping exists.
     */
    public static boolean existsFilterMapping (final WebApp webapp,
                                               final String filterName,
                                               final String servletName)
    {
        return findFilterMapping(webapp, filterName, servletName) != null;
    }


    /**
     * @param webapp
     * @param filterName
     * @param servletName
     * @return the filter mapping or null.
     */
    public static FilterMapping findFilterMapping (final WebApp webapp,
                                                   final String filterName,
                                                   final String servletName)
    {
        for (final Object fm : webapp.getFilterMappings())
        {
            final FilterMapping filterMapping = (FilterMapping) fm;

            if (filterMapping.getFilterName().trim().equals(filterName) && filterMapping.getServletNames().contains(servletName))
            {
                return filterMapping;
            }
        }

        return null;
    }


    /**
     * @param webApp
     * @param filter
     */
    public static void removeFilterMappings (final WebApp webApp,
                                             final Filter filter)
    {
        final List mappings = webApp.getFilterMappings();
        String filterName = filter.getFilterName();

        if (filterName != null)
        {
            filterName = filterName.trim();
            for (int i = mappings.size() - 1; i >= 0; --i)
            {
                final FilterMapping mapping = (FilterMapping) mappings.get(i);
                if (mapping != null && mapping.getFilterName() != null && mapping.getFilterName().trim().equals(filterName))
                {
                    mappings.remove(mapping);
                }
            }
        }
    }


    /**
     * @param webApp
     * @param paramName
     * @param paramValue
     * @param description
     */
    public static void addContextParam (final WebApp webApp,
                                        final String paramName,
                                        final String paramValue,
                                        final String description)
    {
        if (existsContextParam(webApp, paramName, paramValue))
            return;

        webApp.getContextParams().add(createContextParam(paramName, paramValue, description));
    }


    /**
     * @param paramName
     * @param paramValue
     * @param descriptionString
     * @return the param value or null if not found.
     */
    public static ParamValue createContextParam (final String paramName,
                                                 final String paramValue,
                                                 final String descriptionString)
    {
        final ParamValue param = JavaeeFactory.eINSTANCE.createParamValue();
        param.setParamName(paramName);
        param.setParamValue(paramValue);

        if (descriptionString != null)
        {
            final Description description = JavaeeFactory.eINSTANCE.createDescription();
            description.setValue(descriptionString);
            param.getDescriptions().add(description);
        }


        return param;
    }


    /**
     * @param webApp
     * @param paramName
     * @param paramValue
     * @return true if the context param exists.
     */
    public static boolean existsContextParam (final WebApp webApp,
                                              final String paramName,
                                              final String paramValue)
    {
        return findContextParam(webApp, paramName, paramValue) != null;
    }


    /**
     * @param webApp
     * @param paramName
     * @param paramValue
     * @return the param value or null if not found.
     */
    public static ParamValue findContextParam (final WebApp webApp,
                                               final String paramName,
                                               final String paramValue)
    {
        for (final Object param : webApp.getContextParams())
        {
            final ParamValue contextParam = (ParamValue) param;
            if (contextParam.getParamName().equals(paramName) && contextParam.getParamValue().equals(paramValue))
                return contextParam;
        }

        return null;
    }


    /**
     * @param webapp
     * @param listenerClass
     */
    public static void addListener (final WebApp webapp,
            final String listenerClass)
    {
        if (existsListener(webapp, listenerClass))
            return;

        // Create new listener
        final Listener listener = JavaeeFactory.eINSTANCE.createListener();
        listener.setListenerClass(listenerClass);

        webapp.getListeners().add(listener);
    }


    /**
     * @param webapp
     * @param listenerClass
     * @return true if the listener exists.
     */
    public static boolean existsListener (final WebApp webapp,
                                          final String listenerClass)
    {
        return findListener(webapp, listenerClass) != null;
    }


    /**
     * @param webapp
     * @param listenerClass
     * @return the listener or null if not found.
     */
    public static Listener findListener (final WebApp webapp,
                                         final String listenerClass)
    {
        for (final Object listener : webapp.getListeners())
            if (((Listener) listener).getListenerClass().equals(listenerClass))
                return (Listener) listener;

        return null;
    }


    /**
     * @param urlPatternString
     * @return the UrlPattern or null.
     */
    public static UrlPatternType createUrlPattern (final String urlPatternString)
    {
        final UrlPatternType urlPattern = JavaeeFactory.eINSTANCE.createUrlPatternType();
        urlPattern.setValue(urlPatternString);
        return urlPattern;
    }
}

Back to the top