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
122 public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
123 {
124 return _defaultSecurityHandlerClass;
125 }
126
127
128
129
130
131 public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
132 {
133 _defaultSecurityHandlerClass = defaultSecurityHandlerClass;
134 }
135
136
137 protected SessionHandler newSessionHandler()
138 {
139 return new SessionHandler();
140 }
141
142
143 protected SecurityHandler newSecurityHandler()
144 {
145 try
146 {
147 return (SecurityHandler)_defaultSecurityHandlerClass.newInstance();
148 }
149 catch(Exception e)
150 {
151 throw new IllegalStateException(e);
152 }
153 }
154
155
156 protected ServletHandler newServletHandler()
157 {
158 return new ServletHandler();
159 }
160
161
162
163
164
165
166
167 protected void startContext() throws Exception
168 {
169
170 getSessionHandler();
171 getSecurityHandler();
172 getServletHandler();
173
174 Handler handler = _servletHandler;
175 if (_securityHandler!=null)
176 {
177 _securityHandler.setHandler(handler);
178 handler=_securityHandler;
179 }
180
181 if (_sessionHandler!=null)
182 {
183 _sessionHandler.setHandler(handler);
184 handler=_sessionHandler;
185 }
186
187 setHandler(handler);
188
189 super.startContext();
190
191
192 if (_servletHandler != null && _servletHandler.isStarted())
193 _servletHandler.initialize();
194 }
195
196
197
198
199
200 public SecurityHandler getSecurityHandler()
201 {
202 if (_securityHandler==null && (_options&SECURITY)!=0 && !isStarted())
203 _securityHandler=newSecurityHandler();
204
205 return _securityHandler;
206 }
207
208
209
210
211
212 public ServletHandler getServletHandler()
213 {
214 if (_servletHandler==null && !isStarted())
215 _servletHandler=newServletHandler();
216 return _servletHandler;
217 }
218
219
220
221
222
223 public SessionHandler getSessionHandler()
224 {
225 if (_sessionHandler==null && (_options&SESSIONS)!=0 && !isStarted())
226 _sessionHandler=newSessionHandler();
227 return _sessionHandler;
228 }
229
230
231
232
233 public ServletHolder addServlet(String className,String pathSpec)
234 {
235 return getServletHandler().addServletWithMapping(className, pathSpec);
236 }
237
238
239
240
241 public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
242 {
243 return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
244 }
245
246
247
248
249 public void addServlet(ServletHolder servlet,String pathSpec)
250 {
251 getServletHandler().addServletWithMapping(servlet, pathSpec);
252 }
253
254
255
256
257 public void addFilter(FilterHolder holder,String pathSpec,int dispatches)
258 {
259 getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches);
260 }
261
262
263
264
265 public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,int dispatches)
266 {
267 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
268 }
269
270
271
272
273 public FilterHolder addFilter(String filterClass,String pathSpec,int dispatches)
274 {
275 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
276 }
277
278
279
280
281
282 public void setSessionHandler(SessionHandler sessionHandler)
283 {
284 if (isStarted())
285 throw new IllegalStateException("STARTED");
286
287 _sessionHandler = sessionHandler;
288 }
289
290
291
292
293
294 public void setSecurityHandler(SecurityHandler securityHandler)
295 {
296 if (isStarted())
297 throw new IllegalStateException("STARTED");
298
299 _securityHandler = securityHandler;
300 }
301
302
303
304
305
306 public void setServletHandler(ServletHandler servletHandler)
307 {
308 if (isStarted())
309 throw new IllegalStateException("STARTED");
310
311 _servletHandler = servletHandler;
312 }
313
314
315 public class Context extends ContextHandler.Context
316 {
317
318
319
320
321
322 public RequestDispatcher getNamedDispatcher(String name)
323 {
324 ContextHandler context=org.eclipse.jetty.servlet.ServletContextHandler.this;
325 if (_servletHandler==null || _servletHandler.getServlet(name)==null)
326 return null;
327 return new Dispatcher(context, name);
328 }
329
330
331
332 public void addFilterMappingForServletNames(String filterName, EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames)
333 {
334 if (isStarted())
335 throw new IllegalStateException();
336 ServletHandler handler = ServletContextHandler.this.getServletHandler();
337 FilterMapping mapping = new FilterMapping();
338 mapping.setFilterName(filterName);
339 mapping.setServletNames(servletNames);
340 mapping.setDispatcherTypes(dispatcherTypes);
341 handler.addFilterMapping(mapping);
342 }
343
344
345 public void addServletMapping(String servletName, String[] urlPatterns)
346 {
347 if (isStarted())
348 throw new IllegalStateException();
349 ServletHandler handler = ServletContextHandler.this.getServletHandler();
350 ServletHolder holder= handler.newServletHolder();
351 holder.setName(servletName);
352 handler.addServlet(holder);
353 }
354
355
356
357 }
358 }