1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.servlet;
15
16 import java.util.EnumSet;
17
18 import javax.servlet.Filter;
19 import javax.servlet.RequestDispatcher;
20 import javax.servlet.Servlet;
21 import javax.servlet.ServletContext;
22
23 import org.eclipse.jetty.security.SecurityHandler;
24 import org.eclipse.jetty.server.Dispatcher;
25 import org.eclipse.jetty.server.DispatcherType;
26 import org.eclipse.jetty.server.Handler;
27 import org.eclipse.jetty.server.HandlerContainer;
28 import org.eclipse.jetty.server.handler.ContextHandler;
29 import org.eclipse.jetty.server.handler.ErrorHandler;
30 import org.eclipse.jetty.server.handler.HandlerCollection;
31 import org.eclipse.jetty.server.handler.HandlerWrapper;
32 import org.eclipse.jetty.server.session.SessionHandler;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class ServletContextHandler extends ContextHandler
47 {
48 public final static int SESSIONS=1;
49 public final static int SECURITY=2;
50 public final static int NO_SESSIONS=0;
51 public final static int NO_SECURITY=0;
52
53 protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
54 protected SessionHandler _sessionHandler;
55 protected SecurityHandler _securityHandler;
56 protected ServletHandler _servletHandler;
57 protected int _options;
58
59
60 public ServletContextHandler()
61 {
62 this(null,null,null,null,null);
63 }
64
65
66 public ServletContextHandler(int options)
67 {
68 this(null,null,options);
69 }
70
71
72 public ServletContextHandler(HandlerContainer parent, String contextPath)
73 {
74 this(parent,contextPath,null,null,null,null);
75 }
76
77
78 public ServletContextHandler(HandlerContainer parent, String contextPath, int options)
79 {
80 this(parent,contextPath,null,null,null,null);
81 _options=options;
82 }
83
84
85 public ServletContextHandler(HandlerContainer parent, String contextPath, boolean sessions, boolean security)
86 {
87 this(parent,contextPath,(sessions?SESSIONS:0)|(security?SECURITY:0));
88 }
89
90
91 public ServletContextHandler(HandlerContainer parent, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
92 {
93 this(parent,null,sessionHandler,securityHandler,servletHandler,errorHandler);
94 }
95
96
97 public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
98 {
99 super((ContextHandler.Context)null);
100 _scontext = new Context();
101 _sessionHandler = sessionHandler;
102 _securityHandler = securityHandler;
103 _servletHandler = servletHandler;
104
105 if (errorHandler!=null)
106 setErrorHandler(errorHandler);
107
108 if (contextPath!=null)
109 setContextPath(contextPath);
110
111 if (parent instanceof HandlerWrapper)
112 ((HandlerWrapper)parent).setHandler(this);
113 else if (parent instanceof HandlerCollection)
114 ((HandlerCollection)parent).addHandler(this);
115 }
116
117
118
119
120
121 public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
122 {
123 return _defaultSecurityHandlerClass;
124 }
125
126
127
128
129
130 public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
131 {
132 _defaultSecurityHandlerClass = defaultSecurityHandlerClass;
133 }
134
135
136 protected SessionHandler newSessionHandler()
137 {
138 return new SessionHandler();
139 }
140
141
142 protected SecurityHandler newSecurityHandler()
143 {
144 try
145 {
146 return (SecurityHandler)_defaultSecurityHandlerClass.newInstance();
147 }
148 catch(Exception e)
149 {
150 throw new IllegalStateException(e);
151 }
152 }
153
154
155 protected ServletHandler newServletHandler()
156 {
157 return new ServletHandler();
158 }
159
160
161
162
163
164
165
166 protected void startContext() throws Exception
167 {
168
169 getSessionHandler();
170 getSecurityHandler();
171 getServletHandler();
172
173 Handler handler = _servletHandler;
174 if (_securityHandler!=null)
175 {
176 _securityHandler.setHandler(handler);
177 handler=_securityHandler;
178 }
179
180 if (_sessionHandler!=null)
181 {
182 _sessionHandler.setHandler(handler);
183 handler=_sessionHandler;
184 }
185
186 setHandler(handler);
187
188 super.startContext();
189
190
191 if (_servletHandler != null && _servletHandler.isStarted())
192 _servletHandler.initialize();
193 }
194
195
196
197
198
199 public SecurityHandler getSecurityHandler()
200 {
201 if (_securityHandler==null && (_options&SECURITY)!=0 && !isStarted())
202 _securityHandler=newSecurityHandler();
203
204 return _securityHandler;
205 }
206
207
208
209
210
211 public ServletHandler getServletHandler()
212 {
213 if (_servletHandler==null && !isStarted())
214 _servletHandler=newServletHandler();
215 return _servletHandler;
216 }
217
218
219
220
221
222 public SessionHandler getSessionHandler()
223 {
224 if (_sessionHandler==null && (_options&SESSIONS)!=0 && !isStarted())
225 _sessionHandler=newSessionHandler();
226 return _sessionHandler;
227 }
228
229
230
231
232 public ServletHolder addServlet(String className,String pathSpec)
233 {
234 return getServletHandler().addServletWithMapping(className, pathSpec);
235 }
236
237
238
239
240 public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
241 {
242 return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
243 }
244
245
246
247
248 public void addServlet(ServletHolder servlet,String pathSpec)
249 {
250 getServletHandler().addServletWithMapping(servlet, pathSpec);
251 }
252
253
254
255
256 public void addFilter(FilterHolder holder,String pathSpec,int dispatches)
257 {
258 getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches);
259 }
260
261
262
263
264 public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,int dispatches)
265 {
266 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
267 }
268
269
270
271
272 public FilterHolder addFilter(String filterClass,String pathSpec,int dispatches)
273 {
274 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
275 }
276
277
278
279
280
281 public void setSessionHandler(SessionHandler sessionHandler)
282 {
283 if (isStarted())
284 throw new IllegalStateException("STARTED");
285
286 _sessionHandler = sessionHandler;
287 }
288
289
290
291
292
293 public void setSecurityHandler(SecurityHandler securityHandler)
294 {
295 if (isStarted())
296 throw new IllegalStateException("STARTED");
297
298 _securityHandler = securityHandler;
299 }
300
301
302
303
304
305 public void setServletHandler(ServletHandler servletHandler)
306 {
307 if (isStarted())
308 throw new IllegalStateException("STARTED");
309
310 _servletHandler = servletHandler;
311 }
312
313
314 public class Context extends ContextHandler.Context
315 {
316
317
318
319
320
321 public RequestDispatcher getNamedDispatcher(String name)
322 {
323 ContextHandler context=org.eclipse.jetty.servlet.ServletContextHandler.this;
324 if (_servletHandler==null || _servletHandler.getServlet(name)==null)
325 return null;
326 return new Dispatcher(context, name);
327 }
328
329
330
331 public void addFilterMappingForServletNames(String filterName, EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames)
332 {
333 if (isStarted())
334 throw new IllegalStateException();
335 ServletHandler handler = ServletContextHandler.this.getServletHandler();
336 FilterMapping mapping = new FilterMapping();
337 mapping.setFilterName(filterName);
338 mapping.setServletNames(servletNames);
339 mapping.setDispatcherTypes(dispatcherTypes);
340 handler.addFilterMapping(mapping);
341 }
342
343
344 public void addServletMapping(String servletName, String[] urlPatterns)
345 {
346 if (isStarted())
347 throw new IllegalStateException();
348 ServletHandler handler = ServletContextHandler.this.getServletHandler();
349 ServletHolder holder= handler.newServletHolder();
350 holder.setName(servletName);
351 handler.addServlet(holder);
352 }
353
354
355
356 }
357 }