2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.EnumSet;
26 import java.util.EventListener;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
33 import javax.servlet.DispatcherType;
34 import javax.servlet.Filter;
35 import javax.servlet.FilterRegistration;
36 import javax.servlet.RequestDispatcher;
37 import javax.servlet.Servlet;
38 import javax.servlet.ServletContainerInitializer;
39 import javax.servlet.ServletContext;
40 import javax.servlet.ServletContextEvent;
41 import javax.servlet.ServletContextListener;
42 import javax.servlet.ServletException;
43 import javax.servlet.ServletRegistration;
44 import javax.servlet.ServletSecurityElement;
45 import javax.servlet.SessionCookieConfig;
46 import javax.servlet.SessionTrackingMode;
47 import javax.servlet.descriptor.JspConfigDescriptor;
48 import javax.servlet.descriptor.JspPropertyGroupDescriptor;
49 import javax.servlet.descriptor.TaglibDescriptor;
51 import org.eclipse.jetty.security.ConstraintAware;
52 import org.eclipse.jetty.security.ConstraintMapping;
53 import org.eclipse.jetty.security.ConstraintSecurityHandler;
54 import org.eclipse.jetty.security.SecurityHandler;
55 import org.eclipse.jetty.server.Dispatcher;
56 import org.eclipse.jetty.server.Handler;
57 import org.eclipse.jetty.server.HandlerContainer;
58 import org.eclipse.jetty.server.handler.ContextHandler;
59 import org.eclipse.jetty.server.handler.ErrorHandler;
60 import org.eclipse.jetty.server.handler.HandlerCollection;
61 import org.eclipse.jetty.server.handler.HandlerWrapper;
62 import org.eclipse.jetty.server.session.SessionHandler;
63 import org.eclipse.jetty.servlet.BaseHolder.Source;
64 import org.eclipse.jetty.util.annotation.ManagedAttribute;
65 import org.eclipse.jetty.util.annotation.ManagedObject;
66 import org.eclipse.jetty.util.component.LifeCycle;
69 /* ------------------------------------------------------------ */
71 * This extension to the ContextHandler allows for
72 * simple construction of a context with ServletHandler and optionally
73 * session and security handlers, et.<pre>
74 * new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);
77 * This class should have been called ServletContext, but this would have
78 * cause confusion with {@link ServletContext}.
80 @ManagedObject("Servlet Context Handler")
81 public class ServletContextHandler extends ContextHandler
83 public final static int SESSIONS=1;
84 public final static int SECURITY=2;
85 public final static int NO_SESSIONS=0;
86 public final static int NO_SECURITY=0;
88 public interface ServletContainerInitializerCaller extends LifeCycle {};
90 protected final List<Decorator> _decorators= new ArrayList<>();
91 protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
92 protected SessionHandler _sessionHandler;
93 protected SecurityHandler _securityHandler;
94 protected ServletHandler _servletHandler;
95 protected int _options;
96 protected JspConfigDescriptor _jspConfig;
98 /* ------------------------------------------------------------ */
99 public ServletContextHandler()
101 this(null,null,null,null,null);
104 /* ------------------------------------------------------------ */
105 public ServletContextHandler(int options)
107 this(null,null,options);
110 /* ------------------------------------------------------------ */
111 public ServletContextHandler(HandlerContainer parent, String contextPath)
113 this(parent,contextPath,null,null,null,null);
116 /* ------------------------------------------------------------ */
117 public ServletContextHandler(HandlerContainer parent, String contextPath, int options)
119 this(parent,contextPath,null,null,null,null,options);
122 /* ------------------------------------------------------------ */
123 public ServletContextHandler(HandlerContainer parent, String contextPath, boolean sessions, boolean security)
125 this(parent,contextPath,(sessions?SESSIONS:0)|(security?SECURITY:0));
128 /* ------------------------------------------------------------ */
129 public ServletContextHandler(HandlerContainer parent, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
131 this(parent,null,sessionHandler,securityHandler,servletHandler,errorHandler);
134 /* ------------------------------------------------------------ */
135 public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
137 this(parent,contextPath,sessionHandler,securityHandler,servletHandler,errorHandler,0);
140 public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler,int options)
142 super((ContextHandler.Context)null);
144 _scontext = new Context();
145 _sessionHandler = sessionHandler;
146 _securityHandler = securityHandler;
147 _servletHandler = servletHandler;
149 if (contextPath!=null)
150 setContextPath(contextPath);
152 if (parent instanceof HandlerWrapper)
153 ((HandlerWrapper)parent).setHandler(this);
154 else if (parent instanceof HandlerCollection)
155 ((HandlerCollection)parent).addHandler(this);
161 if (errorHandler!=null)
162 setErrorHandler(errorHandler);
166 /* ------------------------------------------------------------ */
167 private void relinkHandlers()
169 HandlerWrapper handler=this;
171 // Skip any injected handlers
172 while (handler.getHandler() instanceof HandlerWrapper)
174 HandlerWrapper wrapper = (HandlerWrapper)handler.getHandler();
175 if (wrapper instanceof SessionHandler ||
176 wrapper instanceof SecurityHandler ||
177 wrapper instanceof ServletHandler)
182 if (getSessionHandler()!=null)
185 super.setHandler(_sessionHandler);
187 handler.setHandler(_sessionHandler);
188 handler=_sessionHandler;
191 if (getSecurityHandler()!=null)
194 super.setHandler(_securityHandler);
196 handler.setHandler(_securityHandler);
197 handler=_securityHandler;
200 if (getServletHandler()!=null)
203 super.setHandler(_servletHandler);
205 handler.setHandler(_servletHandler);
206 handler=_servletHandler;
210 /* ------------------------------------------------------------ */
212 * @see org.eclipse.jetty.server.handler.ContextHandler#doStop()
215 protected void doStop() throws Exception
218 if (_decorators != null)
222 /* ------------------------------------------------------------ */
223 /** Get the defaultSecurityHandlerClass.
224 * @return the defaultSecurityHandlerClass
226 public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
228 return _defaultSecurityHandlerClass;
231 /* ------------------------------------------------------------ */
232 /** Set the defaultSecurityHandlerClass.
233 * @param defaultSecurityHandlerClass the defaultSecurityHandlerClass to set
235 public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
237 _defaultSecurityHandlerClass = defaultSecurityHandlerClass;
240 /* ------------------------------------------------------------ */
241 protected SessionHandler newSessionHandler()
243 return new SessionHandler();
246 /* ------------------------------------------------------------ */
247 protected SecurityHandler newSecurityHandler()
251 return (SecurityHandler)_defaultSecurityHandlerClass.newInstance();
255 throw new IllegalStateException(e);
259 /* ------------------------------------------------------------ */
260 protected ServletHandler newServletHandler()
262 return new ServletHandler();
265 /* ------------------------------------------------------------ */
267 * Finish constructing handlers and link them together.
269 * @see org.eclipse.jetty.server.handler.ContextHandler#startContext()
272 protected void startContext() throws Exception
274 ServletContainerInitializerCaller sciBean = getBean(ServletContainerInitializerCaller.class);
278 if (_servletHandler != null)
280 //Call decorators on all holders, and also on any EventListeners before
281 //decorators are called on any other classes (like servlets and filters)
282 for (int i=_decorators.size()-1;i>=0; i--)
284 Decorator decorator = _decorators.get(i);
285 //Do any decorations on the ListenerHolders AND the listener instances first up
286 if (_servletHandler.getListeners()!=null)
288 for (ListenerHolder holder:_servletHandler.getListeners())
290 decorator.decorate(holder.getListener());
296 super.startContext();
298 // OK to Initialize servlet handler now that all relevant object trees have been started
299 if (_servletHandler != null)
300 _servletHandler.initialize();
303 /* ------------------------------------------------------------ */
305 * @return Returns the securityHandler.
307 @ManagedAttribute(value="context security handler", readonly=true)
308 public SecurityHandler getSecurityHandler()
310 if (_securityHandler==null && (_options&SECURITY)!=0 && !isStarted())
311 _securityHandler=newSecurityHandler();
313 return _securityHandler;
316 /* ------------------------------------------------------------ */
318 * @return Returns the servletHandler.
320 @ManagedAttribute(value="context servlet handler", readonly=true)
321 public ServletHandler getServletHandler()
323 if (_servletHandler==null && !isStarted())
324 _servletHandler=newServletHandler();
325 return _servletHandler;
328 /* ------------------------------------------------------------ */
330 * @return Returns the sessionHandler.
332 @ManagedAttribute(value="context session handler", readonly=true)
333 public SessionHandler getSessionHandler()
335 if (_sessionHandler==null && (_options&SESSIONS)!=0 && !isStarted())
336 _sessionHandler=newSessionHandler();
337 return _sessionHandler;
340 /* ------------------------------------------------------------ */
341 /** conveniance method to add a servlet.
343 public ServletHolder addServlet(String className,String pathSpec)
345 return getServletHandler().addServletWithMapping(className, pathSpec);
348 /* ------------------------------------------------------------ */
349 /** conveniance method to add a servlet.
351 public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
353 return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
356 /* ------------------------------------------------------------ */
357 /** conveniance method to add a servlet.
359 public void addServlet(ServletHolder servlet,String pathSpec)
361 getServletHandler().addServletWithMapping(servlet, pathSpec);
364 /* ------------------------------------------------------------ */
365 /** conveniance method to add a filter
367 public void addFilter(FilterHolder holder,String pathSpec,EnumSet<DispatcherType> dispatches)
369 getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches);
372 /* ------------------------------------------------------------ */
373 /** convenience method to add a filter
375 public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
377 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
380 /* ------------------------------------------------------------ */
381 /** convenience method to add a filter
383 public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
385 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
389 * notification that a ServletRegistration has been created so we can track the annotations
390 * @param holder new holder created through the api.
391 * @return the ServletRegistration.Dynamic
393 protected ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder) {
394 return holder.getRegistration();
398 * delegate for ServletContext.declareRole method
399 * @param roleNames role names to add
401 protected void addRoles(String... roleNames) {
402 //Get a reference to the SecurityHandler, which must be ConstraintAware
403 if (_securityHandler != null && _securityHandler instanceof ConstraintAware)
405 HashSet<String> union = new HashSet<String>();
406 Set<String> existing = ((ConstraintAware)_securityHandler).getRoles();
407 if (existing != null)
408 union.addAll(existing);
409 union.addAll(Arrays.asList(roleNames));
410 ((ConstraintSecurityHandler)_securityHandler).setRoles(union);
415 * Delegate for ServletRegistration.Dynamic.setServletSecurity method
416 * @param registration ServletRegistration.Dynamic instance that setServletSecurity was called on
417 * @param servletSecurityElement new security info
418 * @return the set of exact URL mappings currently associated with the registration that are also present in the web.xml
419 * security constraints and thus will be unaffected by this call.
421 public Set<String> setServletSecurity(ServletRegistration.Dynamic registration, ServletSecurityElement servletSecurityElement)
423 //Default implementation is to just accept them all. If using a webapp, then this behaviour is overridden in WebAppContext.setServletSecurity
424 Collection<String> pathSpecs = registration.getMappings();
425 if (pathSpecs != null)
427 for (String pathSpec:pathSpecs)
429 List<ConstraintMapping> mappings = ConstraintSecurityHandler.createConstraintsWithMappingsForPath(registration.getName(), pathSpec, servletSecurityElement);
430 for (ConstraintMapping m:mappings)
431 ((ConstraintAware)getSecurityHandler()).addConstraintMapping(m);
434 return Collections.emptySet();
438 public void callContextInitialized(ServletContextListener l, ServletContextEvent e)
442 //toggle state of the dynamic API so that the listener cannot use it
443 if(isProgrammaticListener(l))
444 this.getServletContext().setEnabled(false);
446 super.callContextInitialized(l, e);
450 //untoggle the state of the dynamic API
451 this.getServletContext().setEnabled(true);
457 public void callContextDestroyed(ServletContextListener l, ServletContextEvent e)
459 super.callContextDestroyed(l, e);
462 /* ------------------------------------------------------------ */
464 * @param sessionHandler The sessionHandler to set.
466 public void setSessionHandler(SessionHandler sessionHandler)
469 throw new IllegalStateException("STARTED");
471 if (_sessionHandler!=null)
472 _sessionHandler.setHandler(null);
474 _sessionHandler = sessionHandler;
478 /* ------------------------------------------------------------ */
480 * @param securityHandler The {@link SecurityHandler} to set on this context.
482 public void setSecurityHandler(SecurityHandler securityHandler)
485 throw new IllegalStateException("STARTED");
487 if (_securityHandler!=null)
488 _securityHandler.setHandler(null);
489 _securityHandler = securityHandler;
493 /* ------------------------------------------------------------ */
495 * @param servletHandler The servletHandler to set.
497 public void setServletHandler(ServletHandler servletHandler)
500 throw new IllegalStateException("STARTED");
503 if (_servletHandler!=null)
505 next=_servletHandler.getHandler();
506 _servletHandler.setHandler(null);
508 _servletHandler = servletHandler;
510 _servletHandler.setHandler(next);
513 /* ------------------------------------------------------------ */
515 public void setHandler(Handler handler)
517 if (handler instanceof ServletHandler)
518 setServletHandler((ServletHandler) handler);
519 else if (handler instanceof SessionHandler)
520 setSessionHandler((SessionHandler) handler);
521 else if (handler instanceof SecurityHandler)
522 setSecurityHandler((SecurityHandler)handler);
523 else if (handler == null || handler instanceof HandlerWrapper)
525 super.setHandler(handler);
529 throw new IllegalArgumentException();
533 /* ------------------------------------------------------------ */
535 * Insert a HandlerWrapper before the first Session,Security or ServletHandler
536 * but after any other HandlerWrappers.
538 public void insertHandler(HandlerWrapper handler)
540 HandlerWrapper h=this;
542 // Skip any injected handlers
543 while (h.getHandler() instanceof HandlerWrapper)
545 HandlerWrapper wrapper = (HandlerWrapper)h.getHandler();
546 if (wrapper instanceof SessionHandler ||
547 wrapper instanceof SecurityHandler ||
548 wrapper instanceof ServletHandler)
553 h.setHandler(handler);
557 /* ------------------------------------------------------------ */
559 * @return The decorator list used to resource inject new Filters, Servlets and EventListeners
561 public List<Decorator> getDecorators()
563 return Collections.unmodifiableList(_decorators);
566 /* ------------------------------------------------------------ */
568 * @param decorators The lis of {@link Decorator}s
570 public void setDecorators(List<Decorator> decorators)
573 _decorators.addAll(decorators);
576 /* ------------------------------------------------------------ */
578 * @param decorator The decorator to add
580 public void addDecorator(Decorator decorator)
582 _decorators.add(decorator);
585 /* ------------------------------------------------------------ */
586 void destroyServlet(Servlet servlet)
588 for (Decorator decorator : _decorators)
589 decorator.destroy(servlet);
592 /* ------------------------------------------------------------ */
593 void destroyFilter(Filter filter)
595 for (Decorator decorator : _decorators)
596 decorator.destroy(filter);
599 /* ------------------------------------------------------------ */
600 public static class JspPropertyGroup implements JspPropertyGroupDescriptor
602 private List<String> _urlPatterns = new ArrayList<String>();
603 private String _elIgnored;
604 private String _pageEncoding;
605 private String _scriptingInvalid;
606 private String _isXml;
607 private List<String> _includePreludes = new ArrayList<String>();
608 private List<String> _includeCodas = new ArrayList<String>();
609 private String _deferredSyntaxAllowedAsLiteral;
610 private String _trimDirectiveWhitespaces;
611 private String _defaultContentType;
612 private String _buffer;
613 private String _errorOnUndeclaredNamespace;
618 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getUrlPatterns()
620 public Collection<String> getUrlPatterns()
622 return new ArrayList<String>(_urlPatterns); // spec says must be a copy
625 public void addUrlPattern (String s)
627 if (!_urlPatterns.contains(s))
632 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getElIgnored()
634 public String getElIgnored()
639 public void setElIgnored (String s)
645 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getPageEncoding()
647 public String getPageEncoding()
649 return _pageEncoding;
652 public void setPageEncoding(String pageEncoding)
654 _pageEncoding = pageEncoding;
657 public void setScriptingInvalid(String scriptingInvalid)
659 _scriptingInvalid = scriptingInvalid;
662 public void setIsXml(String isXml)
667 public void setDeferredSyntaxAllowedAsLiteral(String deferredSyntaxAllowedAsLiteral)
669 _deferredSyntaxAllowedAsLiteral = deferredSyntaxAllowedAsLiteral;
672 public void setTrimDirectiveWhitespaces(String trimDirectiveWhitespaces)
674 _trimDirectiveWhitespaces = trimDirectiveWhitespaces;
677 public void setDefaultContentType(String defaultContentType)
679 _defaultContentType = defaultContentType;
682 public void setBuffer(String buffer)
687 public void setErrorOnUndeclaredNamespace(String errorOnUndeclaredNamespace)
689 _errorOnUndeclaredNamespace = errorOnUndeclaredNamespace;
693 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getScriptingInvalid()
695 public String getScriptingInvalid()
697 return _scriptingInvalid;
701 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIsXml()
703 public String getIsXml()
709 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIncludePreludes()
711 public Collection<String> getIncludePreludes()
713 return new ArrayList<String>(_includePreludes); //must be a copy
716 public void addIncludePrelude(String prelude)
718 if (!_includePreludes.contains(prelude))
719 _includePreludes.add(prelude);
723 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIncludeCodas()
725 public Collection<String> getIncludeCodas()
727 return new ArrayList<String>(_includeCodas); //must be a copy
730 public void addIncludeCoda (String coda)
732 if (!_includeCodas.contains(coda))
733 _includeCodas.add(coda);
737 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getDeferredSyntaxAllowedAsLiteral()
739 public String getDeferredSyntaxAllowedAsLiteral()
741 return _deferredSyntaxAllowedAsLiteral;
745 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getTrimDirectiveWhitespaces()
747 public String getTrimDirectiveWhitespaces()
749 return _trimDirectiveWhitespaces;
753 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getDefaultContentType()
755 public String getDefaultContentType()
757 return _defaultContentType;
761 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getBuffer()
763 public String getBuffer()
769 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getErrorOnUndeclaredNamespace()
771 public String getErrorOnUndeclaredNamespace()
773 return _errorOnUndeclaredNamespace;
776 public String toString ()
778 StringBuffer sb = new StringBuffer();
779 sb.append("JspPropertyGroupDescriptor:");
780 sb.append(" el-ignored="+_elIgnored);
781 sb.append(" is-xml="+_isXml);
782 sb.append(" page-encoding="+_pageEncoding);
783 sb.append(" scripting-invalid="+_scriptingInvalid);
784 sb.append(" deferred-syntax-allowed-as-literal="+_deferredSyntaxAllowedAsLiteral);
785 sb.append(" trim-directive-whitespaces"+_trimDirectiveWhitespaces);
786 sb.append(" default-content-type="+_defaultContentType);
787 sb.append(" buffer="+_buffer);
788 sb.append(" error-on-undeclared-namespace="+_errorOnUndeclaredNamespace);
789 for (String prelude:_includePreludes)
790 sb.append(" include-prelude="+prelude);
791 for (String coda:_includeCodas)
792 sb.append(" include-coda="+coda);
793 return sb.toString();
797 /* ------------------------------------------------------------ */
798 public static class TagLib implements TaglibDescriptor
801 private String _location;
804 * @see javax.servlet.descriptor.TaglibDescriptor#getTaglibURI()
806 public String getTaglibURI()
811 public void setTaglibURI(String uri)
817 * @see javax.servlet.descriptor.TaglibDescriptor#getTaglibLocation()
819 public String getTaglibLocation()
824 public void setTaglibLocation(String location)
826 _location = location;
829 public String toString()
831 return ("TagLibDescriptor: taglib-uri="+_uri+" location="+_location);
836 /* ------------------------------------------------------------ */
837 public static class JspConfig implements JspConfigDescriptor
839 private List<TaglibDescriptor> _taglibs = new ArrayList<TaglibDescriptor>();
840 private List<JspPropertyGroupDescriptor> _jspPropertyGroups = new ArrayList<JspPropertyGroupDescriptor>();
842 public JspConfig() {}
845 * @see javax.servlet.descriptor.JspConfigDescriptor#getTaglibs()
847 public Collection<TaglibDescriptor> getTaglibs()
849 return new ArrayList<TaglibDescriptor>(_taglibs);
852 public void addTaglibDescriptor (TaglibDescriptor d)
858 * @see javax.servlet.descriptor.JspConfigDescriptor#getJspPropertyGroups()
860 public Collection<JspPropertyGroupDescriptor> getJspPropertyGroups()
862 return new ArrayList<JspPropertyGroupDescriptor>(_jspPropertyGroups);
865 public void addJspPropertyGroup(JspPropertyGroupDescriptor g)
867 _jspPropertyGroups.add(g);
870 public String toString()
872 StringBuffer sb = new StringBuffer();
873 sb.append("JspConfigDescriptor: \n");
874 for (TaglibDescriptor taglib:_taglibs)
875 sb.append(taglib+"\n");
876 for (JspPropertyGroupDescriptor jpg:_jspPropertyGroups)
878 return sb.toString();
883 /* ------------------------------------------------------------ */
884 public class Context extends ContextHandler.Context
886 /* ------------------------------------------------------------ */
888 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
891 public RequestDispatcher getNamedDispatcher(String name)
893 ContextHandler context=org.eclipse.jetty.servlet.ServletContextHandler.this;
894 if (_servletHandler==null)
896 ServletHolder holder = _servletHandler.getServlet(name);
897 if (holder==null || !holder.isEnabled())
899 return new Dispatcher(context, name);
902 /* ------------------------------------------------------------ */
904 * @since servlet-api-3.0
907 public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
910 throw new IllegalStateException();
912 if (filterName == null || "".equals(filterName.trim()))
913 throw new IllegalStateException("Missing filter name");
916 throw new UnsupportedOperationException();
918 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
919 FilterHolder holder = handler.getFilter(filterName);
923 holder = handler.newFilterHolder(Source.JAVAX_API);
924 holder.setName(filterName);
925 holder.setHeldClass(filterClass);
926 handler.addFilter(holder);
927 return holder.getRegistration();
929 if (holder.getClassName()==null && holder.getHeldClass()==null)
931 //preliminary filter registration completion
932 holder.setHeldClass(filterClass);
933 return holder.getRegistration();
936 return null; //existing filter
939 /* ------------------------------------------------------------ */
941 * @since servlet-api-3.0
944 public FilterRegistration.Dynamic addFilter(String filterName, String className)
947 throw new IllegalStateException();
949 if (filterName == null || "".equals(filterName.trim()))
950 throw new IllegalStateException("Missing filter name");
953 throw new UnsupportedOperationException();
955 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
956 FilterHolder holder = handler.getFilter(filterName);
960 holder = handler.newFilterHolder(Source.JAVAX_API);
961 holder.setName(filterName);
962 holder.setClassName(className);
963 handler.addFilter(holder);
964 return holder.getRegistration();
966 if (holder.getClassName()==null && holder.getHeldClass()==null)
968 //preliminary filter registration completion
969 holder.setClassName(className);
970 return holder.getRegistration();
973 return null; //existing filter
977 /* ------------------------------------------------------------ */
979 * @since servlet-api-3.0
982 public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
985 throw new IllegalStateException();
987 if (filterName == null || "".equals(filterName.trim()))
988 throw new IllegalStateException("Missing filter name");
991 throw new UnsupportedOperationException();
993 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
994 FilterHolder holder = handler.getFilter(filterName);
998 holder = handler.newFilterHolder(Source.JAVAX_API);
999 holder.setName(filterName);
1000 holder.setFilter(filter);
1001 handler.addFilter(holder);
1002 return holder.getRegistration();
1005 if (holder.getClassName()==null && holder.getHeldClass()==null)
1007 //preliminary filter registration completion
1008 holder.setFilter(filter);
1009 return holder.getRegistration();
1012 return null; //existing filter
1015 /* ------------------------------------------------------------ */
1017 * @since servlet-api-3.0
1020 public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
1023 throw new IllegalStateException();
1025 if (servletName == null || "".equals(servletName.trim()))
1026 throw new IllegalStateException("Missing servlet name");
1029 throw new UnsupportedOperationException();
1031 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1032 ServletHolder holder = handler.getServlet(servletName);
1036 holder = handler.newServletHolder(Source.JAVAX_API);
1037 holder.setName(servletName);
1038 holder.setHeldClass(servletClass);
1039 handler.addServlet(holder);
1040 return dynamicHolderAdded(holder);
1043 //complete a partial registration
1044 if (holder.getClassName()==null && holder.getHeldClass()==null)
1046 holder.setHeldClass(servletClass);
1047 return holder.getRegistration();
1050 return null; //existing completed registration for servlet name
1053 /* ------------------------------------------------------------ */
1055 * @since servlet-api-3.0
1058 public ServletRegistration.Dynamic addServlet(String servletName, String className)
1061 throw new IllegalStateException();
1063 if (servletName == null || "".equals(servletName.trim()))
1064 throw new IllegalStateException("Missing servlet name");
1067 throw new UnsupportedOperationException();
1070 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1071 ServletHolder holder = handler.getServlet(servletName);
1075 holder = handler.newServletHolder(Source.JAVAX_API);
1076 holder.setName(servletName);
1077 holder.setClassName(className);
1078 handler.addServlet(holder);
1079 return dynamicHolderAdded(holder);
1082 //complete a partial registration
1083 if (holder.getClassName()==null && holder.getHeldClass()==null)
1085 holder.setClassName(className);
1086 return holder.getRegistration();
1089 return null; //existing completed registration for servlet name
1092 /* ------------------------------------------------------------ */
1094 * @since servlet-api-3.0
1097 public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
1100 throw new IllegalStateException();
1102 if (servletName == null || "".equals(servletName.trim()))
1103 throw new IllegalStateException("Missing servlet name");
1106 throw new UnsupportedOperationException();
1108 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1109 ServletHolder holder = handler.getServlet(servletName);
1112 holder = handler.newServletHolder(Source.JAVAX_API);
1113 holder.setName(servletName);
1114 holder.setServlet(servlet);
1115 handler.addServlet(holder);
1116 return dynamicHolderAdded(holder);
1119 //complete a partial registration
1120 if (holder.getClassName()==null && holder.getHeldClass()==null)
1122 holder.setServlet(servlet);
1123 return holder.getRegistration();
1126 return null; //existing completed registration for servlet name
1129 /* ------------------------------------------------------------ */
1131 public boolean setInitParameter(String name, String value)
1134 throw new IllegalStateException();
1137 throw new UnsupportedOperationException();
1139 return super.setInitParameter(name,value);
1142 /* ------------------------------------------------------------ */
1144 public <T extends Filter> T createFilter(Class<T> c) throws ServletException
1148 T f = createInstance(c);
1149 for (int i=_decorators.size()-1; i>=0; i--)
1151 Decorator decorator = _decorators.get(i);
1152 f=decorator.decorate(f);
1158 throw new ServletException(e);
1162 /* ------------------------------------------------------------ */
1164 public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
1168 T s = createInstance(c);
1169 for (int i=_decorators.size()-1; i>=0; i--)
1171 Decorator decorator = _decorators.get(i);
1172 s=decorator.decorate(s);
1178 throw new ServletException(e);
1184 public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
1186 if (_sessionHandler!=null)
1187 return _sessionHandler.getSessionManager().getDefaultSessionTrackingModes();
1192 public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
1194 if (_sessionHandler!=null)
1195 return _sessionHandler.getSessionManager().getEffectiveSessionTrackingModes();
1200 public FilterRegistration getFilterRegistration(String filterName)
1203 throw new UnsupportedOperationException();
1205 final FilterHolder holder=ServletContextHandler.this.getServletHandler().getFilter(filterName);
1206 return (holder==null)?null:holder.getRegistration();
1210 public Map<String, ? extends FilterRegistration> getFilterRegistrations()
1213 throw new UnsupportedOperationException();
1215 HashMap<String, FilterRegistration> registrations = new HashMap<String, FilterRegistration>();
1216 ServletHandler handler=ServletContextHandler.this.getServletHandler();
1217 FilterHolder[] holders=handler.getFilters();
1220 for (FilterHolder holder : holders)
1221 registrations.put(holder.getName(),holder.getRegistration());
1223 return registrations;
1227 public ServletRegistration getServletRegistration(String servletName)
1230 throw new UnsupportedOperationException();
1232 final ServletHolder holder=ServletContextHandler.this.getServletHandler().getServlet(servletName);
1233 return (holder==null)?null:holder.getRegistration();
1237 public Map<String, ? extends ServletRegistration> getServletRegistrations()
1240 throw new UnsupportedOperationException();
1242 HashMap<String, ServletRegistration> registrations = new HashMap<String, ServletRegistration>();
1243 ServletHandler handler=ServletContextHandler.this.getServletHandler();
1244 ServletHolder[] holders=handler.getServlets();
1247 for (ServletHolder holder : holders)
1248 registrations.put(holder.getName(),holder.getRegistration());
1250 return registrations;
1254 public SessionCookieConfig getSessionCookieConfig()
1257 throw new UnsupportedOperationException();
1259 if (_sessionHandler!=null)
1260 return _sessionHandler.getSessionManager().getSessionCookieConfig();
1265 public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
1268 throw new IllegalStateException();
1270 throw new UnsupportedOperationException();
1273 if (_sessionHandler!=null)
1274 _sessionHandler.getSessionManager().setSessionTrackingModes(sessionTrackingModes);
1278 public void addListener(String className)
1281 throw new IllegalStateException();
1283 throw new UnsupportedOperationException();
1284 super.addListener(className);
1288 public <T extends EventListener> void addListener(T t)
1291 throw new IllegalStateException();
1293 throw new UnsupportedOperationException();
1294 super.addListener(t);
1295 ListenerHolder holder = getServletHandler().newListenerHolder(Source.JAVAX_API);
1296 holder.setListener(t);
1297 getServletHandler().addListener(holder);
1301 public void addListener(Class<? extends EventListener> listenerClass)
1304 throw new IllegalStateException();
1306 throw new UnsupportedOperationException();
1307 super.addListener(listenerClass);
1311 public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
1315 T l = createInstance(clazz);
1316 for (int i=_decorators.size()-1; i>=0; i--)
1318 Decorator decorator = _decorators.get(i);
1319 l=decorator.decorate(l);
1325 throw new ServletException(e);
1331 public JspConfigDescriptor getJspConfigDescriptor()
1337 public void setJspConfigDescriptor(JspConfigDescriptor d)
1344 public void declareRoles(String... roleNames)
1347 throw new IllegalStateException();
1349 throw new UnsupportedOperationException();
1350 addRoles(roleNames);
1359 /* ------------------------------------------------------------ */
1360 /** Interface to decorate loaded classes.
1362 public interface Decorator
1364 <T> T decorate (T o);
1365 void destroy (Object o);