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