1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.webapp;
14
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.util.Collections;
18
19 import org.eclipse.jetty.security.ConstraintAware;
20 import org.eclipse.jetty.security.ConstraintMapping;
21 import org.eclipse.jetty.security.SecurityHandler;
22 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
23 import org.eclipse.jetty.servlet.ServletHandler;
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.resource.Resource;
26
27
28
29
30
31
32 public class WebXmlConfiguration implements Configuration
33 {
34
35
36
37
38
39
40
41 public void preConfigure (WebAppContext context) throws Exception
42 {
43
44 if (context.isStarted())
45 {
46 if (Log.isDebugEnabled())
47 {
48 Log.debug("Cannot configure webapp after it is started");
49 }
50 return;
51 }
52
53
54 WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR);
55 if (processor == null)
56 {
57 processor = new WebXmlProcessor (context);
58 context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
59 }
60
61
62 String defaultsDescriptor = context.getDefaultsDescriptor();
63 if (defaultsDescriptor != null && defaultsDescriptor.length() > 0)
64 {
65 Resource dftResource = Resource.newSystemResource(defaultsDescriptor);
66 if (dftResource == null)
67 dftResource = context.newResource(defaultsDescriptor);
68 processor.parseDefaults (dftResource);
69 processor.processDefaults();
70 }
71
72
73 Resource webxml = findWebXml(context);
74 if (webxml != null)
75 {
76 processor.parseWebXml(webxml);
77 }
78 }
79
80
81
82
83
84
85 public void configure (WebAppContext context) throws Exception
86 {
87
88 if (context.isStarted())
89 {
90 if (Log.isDebugEnabled()) Log.debug("Cannot configure webapp after it is started");
91 return;
92 }
93
94 WebXmlProcessor processor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR);
95 if (processor == null)
96 {
97 processor = new WebXmlProcessor (context);
98 context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, processor);
99 }
100
101
102 processor.processWebXml();
103
104
105 String overrideDescriptor = context.getOverrideDescriptor();
106 if (overrideDescriptor != null && overrideDescriptor.length() > 0)
107 {
108 Resource orideResource = Resource.newSystemResource(overrideDescriptor);
109 if (orideResource == null)
110 orideResource = context.newResource(overrideDescriptor);
111 processor.parseOverride(orideResource);
112 processor.processOverride();
113 }
114 }
115
116 public void postConfigure(WebAppContext context) throws Exception
117 {
118 context.setAttribute(WebXmlProcessor.WEB_PROCESSOR, null);
119 context.setAttribute(WebXmlProcessor.METADATA_COMPLETE, null);
120 context.setAttribute(WebXmlProcessor.WEBXML_VERSION, null);
121 context.setAttribute(WebXmlProcessor.WEBXML_CLASSNAMES, null);
122 }
123
124
125 protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException
126 {
127 String descriptor = context.getDescriptor();
128 if (descriptor != null)
129 {
130 Resource web = context.newResource(descriptor);
131 if (web.exists() && !web.isDirectory()) return web;
132 }
133
134 Resource web_inf = context.getWebInf();
135 if (web_inf != null && web_inf.isDirectory())
136 {
137
138 Resource web = web_inf.addPath("web.xml");
139 if (web.exists()) return web;
140 Log.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only");
141 }
142 return null;
143 }
144
145
146
147 public void deconfigure (WebAppContext context) throws Exception
148 {
149
150
151 ServletHandler _servletHandler = context.getServletHandler();
152 SecurityHandler _securityHandler = (SecurityHandler)context.getSecurityHandler();
153
154 _servletHandler.setFilters(null);
155 _servletHandler.setFilterMappings(null);
156 _servletHandler.setServlets(null);
157 _servletHandler.setServletMappings(null);
158
159 context.setEventListeners(null);
160 context.setWelcomeFiles(null);
161 if (_securityHandler instanceof ConstraintAware)
162 ((ConstraintAware) _securityHandler).setConstraintMappings(new ConstraintMapping[]{}, Collections.EMPTY_SET);
163
164 if (context.getErrorHandler() instanceof ErrorPageErrorHandler)
165 ((ErrorPageErrorHandler)
166 context.getErrorHandler()).setErrorPages(null);
167
168
169
170
171 }
172 }