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