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