2 // ========================================================================
3 // Copyright (c) 1995-2016 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.ServletContextEvent;
39 import javax.servlet.ServletContextListener;
40 import javax.servlet.ServletException;
41 import javax.servlet.ServletRegistration;
42 import javax.servlet.ServletSecurityElement;
43 import javax.servlet.SessionCookieConfig;
44 import javax.servlet.SessionTrackingMode;
45 import javax.servlet.descriptor.JspConfigDescriptor;
46 import javax.servlet.descriptor.JspPropertyGroupDescriptor;
47 import javax.servlet.descriptor.TaglibDescriptor;
49 import org.eclipse.jetty.security.ConstraintAware;
50 import org.eclipse.jetty.security.ConstraintMapping;
51 import org.eclipse.jetty.security.ConstraintSecurityHandler;
52 import org.eclipse.jetty.security.SecurityHandler;
53 import org.eclipse.jetty.server.Dispatcher;
54 import org.eclipse.jetty.server.Handler;
55 import org.eclipse.jetty.server.HandlerContainer;
56 import org.eclipse.jetty.server.handler.ContextHandler;
57 import org.eclipse.jetty.server.handler.ErrorHandler;
58 import org.eclipse.jetty.server.handler.HandlerCollection;
59 import org.eclipse.jetty.server.handler.HandlerWrapper;
60 import org.eclipse.jetty.server.session.SessionHandler;
61 import org.eclipse.jetty.servlet.BaseHolder.Source;
62 import org.eclipse.jetty.util.annotation.ManagedAttribute;
63 import org.eclipse.jetty.util.annotation.ManagedObject;
64 import org.eclipse.jetty.util.component.LifeCycle;
67 /* ------------------------------------------------------------ */
69 * This extension to the ContextHandler allows for
70 * simple construction of a context with ServletHandler and optionally
71 * session and security handlers, et.<pre>
72 * new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);
75 * This class should have been called ServletContext, but this would have
76 * cause confusion with {@link ServletContext}.
78 @ManagedObject("Servlet Context Handler")
79 public class ServletContextHandler extends ContextHandler
81 public final static int SESSIONS=1;
82 public final static int SECURITY=2;
83 public final static int NO_SESSIONS=0;
84 public final static int NO_SECURITY=0;
86 public interface ServletContainerInitializerCaller extends LifeCycle {};
88 protected final List<Decorator> _decorators= new ArrayList<>();
89 protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
90 protected SessionHandler _sessionHandler;
91 protected SecurityHandler _securityHandler;
92 protected ServletHandler _servletHandler;
93 protected int _options;
94 protected JspConfigDescriptor _jspConfig;
96 /* ------------------------------------------------------------ */
97 public ServletContextHandler()
99 this(null,null,null,null,null);
102 /* ------------------------------------------------------------ */
103 public ServletContextHandler(int options)
105 this(null,null,options);
108 /* ------------------------------------------------------------ */
109 public ServletContextHandler(HandlerContainer parent, String contextPath)
111 this(parent,contextPath,null,null,null,null);
114 /* ------------------------------------------------------------ */
115 public ServletContextHandler(HandlerContainer parent, String contextPath, int options)
117 this(parent,contextPath,null,null,null,null,options);
120 /* ------------------------------------------------------------ */
121 public ServletContextHandler(HandlerContainer parent, String contextPath, boolean sessions, boolean security)
123 this(parent,contextPath,(sessions?SESSIONS:0)|(security?SECURITY:0));
126 /* ------------------------------------------------------------ */
127 public ServletContextHandler(HandlerContainer parent, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
129 this(parent,null,sessionHandler,securityHandler,servletHandler,errorHandler);
132 /* ------------------------------------------------------------ */
133 public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
135 this(parent,contextPath,sessionHandler,securityHandler,servletHandler,errorHandler,0);
138 public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler,int options)
140 super((ContextHandler.Context)null);
142 _scontext = new Context();
143 _sessionHandler = sessionHandler;
144 _securityHandler = securityHandler;
145 _servletHandler = servletHandler;
147 if (contextPath!=null)
148 setContextPath(contextPath);
150 if (parent instanceof HandlerWrapper)
151 ((HandlerWrapper)parent).setHandler(this);
152 else if (parent instanceof HandlerCollection)
153 ((HandlerCollection)parent).addHandler(this);
159 if (errorHandler!=null)
160 setErrorHandler(errorHandler);
164 /* ------------------------------------------------------------ */
165 private void relinkHandlers()
167 HandlerWrapper handler=this;
169 // Skip any injected handlers
170 while (handler.getHandler() instanceof HandlerWrapper)
172 HandlerWrapper wrapper = (HandlerWrapper)handler.getHandler();
173 if (wrapper instanceof SessionHandler ||
174 wrapper instanceof SecurityHandler ||
175 wrapper instanceof ServletHandler)
180 if (getSessionHandler()!=null)
183 super.setHandler(_sessionHandler);
185 handler.setHandler(_sessionHandler);
186 handler=_sessionHandler;
189 if (getSecurityHandler()!=null)
192 super.setHandler(_securityHandler);
194 handler.setHandler(_securityHandler);
195 handler=_securityHandler;
198 if (getServletHandler()!=null)
201 super.setHandler(_servletHandler);
203 handler.setHandler(_servletHandler);
204 handler=_servletHandler;
208 /* ------------------------------------------------------------ */
210 * @see org.eclipse.jetty.server.handler.ContextHandler#doStop()
213 protected void doStop() throws Exception
216 if (_decorators != null)
220 /* ------------------------------------------------------------ */
221 /** Get the defaultSecurityHandlerClass.
222 * @return the defaultSecurityHandlerClass
224 public Class<? extends SecurityHandler> getDefaultSecurityHandlerClass()
226 return _defaultSecurityHandlerClass;
229 /* ------------------------------------------------------------ */
230 /** Set the defaultSecurityHandlerClass.
231 * @param defaultSecurityHandlerClass the defaultSecurityHandlerClass to set
233 public void setDefaultSecurityHandlerClass(Class<? extends SecurityHandler> defaultSecurityHandlerClass)
235 _defaultSecurityHandlerClass = defaultSecurityHandlerClass;
238 /* ------------------------------------------------------------ */
239 protected SessionHandler newSessionHandler()
241 return new SessionHandler();
244 /* ------------------------------------------------------------ */
245 protected SecurityHandler newSecurityHandler()
249 return (SecurityHandler)_defaultSecurityHandlerClass.newInstance();
253 throw new IllegalStateException(e);
257 /* ------------------------------------------------------------ */
258 protected ServletHandler newServletHandler()
260 return new ServletHandler();
263 /* ------------------------------------------------------------ */
265 * Finish constructing handlers and link them together.
267 * @see org.eclipse.jetty.server.handler.ContextHandler#startContext()
270 protected void startContext() throws Exception
272 ServletContainerInitializerCaller sciBean = getBean(ServletContainerInitializerCaller.class);
276 if (_servletHandler != null)
278 //Call decorators on all holders, and also on any EventListeners before
279 //decorators are called on any other classes (like servlets and filters)
280 for (int i=_decorators.size()-1;i>=0; i--)
282 Decorator decorator = _decorators.get(i);
283 //Do any decorations on the ListenerHolders AND the listener instances first up
284 if (_servletHandler.getListeners()!=null)
286 for (ListenerHolder holder:_servletHandler.getListeners())
288 decorator.decorate(holder.getListener());
294 super.startContext();
296 // OK to Initialize servlet handler now that all relevant object trees have been started
297 if (_servletHandler != null)
298 _servletHandler.initialize();
301 /* ------------------------------------------------------------ */
303 * @return Returns the securityHandler.
305 @ManagedAttribute(value="context security handler", readonly=true)
306 public SecurityHandler getSecurityHandler()
308 if (_securityHandler==null && (_options&SECURITY)!=0 && !isStarted())
309 _securityHandler=newSecurityHandler();
311 return _securityHandler;
314 /* ------------------------------------------------------------ */
316 * @return Returns the servletHandler.
318 @ManagedAttribute(value="context servlet handler", readonly=true)
319 public ServletHandler getServletHandler()
321 if (_servletHandler==null && !isStarted())
322 _servletHandler=newServletHandler();
323 return _servletHandler;
326 /* ------------------------------------------------------------ */
328 * @return Returns the sessionHandler.
330 @ManagedAttribute(value="context session handler", readonly=true)
331 public SessionHandler getSessionHandler()
333 if (_sessionHandler==null && (_options&SESSIONS)!=0 && !isStarted())
334 _sessionHandler=newSessionHandler();
335 return _sessionHandler;
338 /* ------------------------------------------------------------ */
339 /** conveniance method to add a servlet.
341 public ServletHolder addServlet(String className,String pathSpec)
343 return getServletHandler().addServletWithMapping(className, pathSpec);
346 /* ------------------------------------------------------------ */
347 /** conveniance method to add a servlet.
349 public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
351 return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
354 /* ------------------------------------------------------------ */
355 /** conveniance method to add a servlet.
357 public void addServlet(ServletHolder servlet,String pathSpec)
359 getServletHandler().addServletWithMapping(servlet, pathSpec);
362 /* ------------------------------------------------------------ */
363 /** conveniance method to add a filter
365 public void addFilter(FilterHolder holder,String pathSpec,EnumSet<DispatcherType> dispatches)
367 getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches);
370 /* ------------------------------------------------------------ */
371 /** convenience method to add a filter
373 public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
375 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
378 /* ------------------------------------------------------------ */
379 /** convenience method to add a filter
381 public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches)
383 return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches);
387 * notification that a ServletRegistration has been created so we can track the annotations
388 * @param holder new holder created through the api.
389 * @return the ServletRegistration.Dynamic
391 protected ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder) {
392 return holder.getRegistration();
396 * delegate for ServletContext.declareRole method
397 * @param roleNames role names to add
399 protected void addRoles(String... roleNames) {
400 //Get a reference to the SecurityHandler, which must be ConstraintAware
401 if (_securityHandler != null && _securityHandler instanceof ConstraintAware)
403 HashSet<String> union = new HashSet<String>();
404 Set<String> existing = ((ConstraintAware)_securityHandler).getRoles();
405 if (existing != null)
406 union.addAll(existing);
407 union.addAll(Arrays.asList(roleNames));
408 ((ConstraintSecurityHandler)_securityHandler).setRoles(union);
413 * Delegate for ServletRegistration.Dynamic.setServletSecurity method
414 * @param registration ServletRegistration.Dynamic instance that setServletSecurity was called on
415 * @param servletSecurityElement new security info
416 * @return the set of exact URL mappings currently associated with the registration that are also present in the web.xml
417 * security constraints and thus will be unaffected by this call.
419 public Set<String> setServletSecurity(ServletRegistration.Dynamic registration, ServletSecurityElement servletSecurityElement)
421 //Default implementation is to just accept them all. If using a webapp, then this behaviour is overridden in WebAppContext.setServletSecurity
422 Collection<String> pathSpecs = registration.getMappings();
423 if (pathSpecs != null)
425 for (String pathSpec:pathSpecs)
427 List<ConstraintMapping> mappings = ConstraintSecurityHandler.createConstraintsWithMappingsForPath(registration.getName(), pathSpec, servletSecurityElement);
428 for (ConstraintMapping m:mappings)
429 ((ConstraintAware)getSecurityHandler()).addConstraintMapping(m);
432 return Collections.emptySet();
436 public void callContextInitialized(ServletContextListener l, ServletContextEvent e)
440 //toggle state of the dynamic API so that the listener cannot use it
441 if(isProgrammaticListener(l))
442 this.getServletContext().setEnabled(false);
444 super.callContextInitialized(l, e);
448 //untoggle the state of the dynamic API
449 this.getServletContext().setEnabled(true);
455 public void callContextDestroyed(ServletContextListener l, ServletContextEvent e)
457 super.callContextDestroyed(l, e);
460 /* ------------------------------------------------------------ */
462 * @param sessionHandler The sessionHandler to set.
464 public void setSessionHandler(SessionHandler sessionHandler)
467 throw new IllegalStateException("STARTED");
469 if (_sessionHandler!=null)
470 _sessionHandler.setHandler(null);
472 _sessionHandler = sessionHandler;
476 /* ------------------------------------------------------------ */
478 * @param securityHandler The {@link SecurityHandler} to set on this context.
480 public void setSecurityHandler(SecurityHandler securityHandler)
483 throw new IllegalStateException("STARTED");
485 if (_securityHandler!=null)
486 _securityHandler.setHandler(null);
487 _securityHandler = securityHandler;
491 /* ------------------------------------------------------------ */
493 * @param servletHandler The servletHandler to set.
495 public void setServletHandler(ServletHandler servletHandler)
498 throw new IllegalStateException("STARTED");
501 if (_servletHandler!=null)
503 next=_servletHandler.getHandler();
504 _servletHandler.setHandler(null);
506 _servletHandler = servletHandler;
508 _servletHandler.setHandler(next);
511 /* ------------------------------------------------------------ */
513 public void setHandler(Handler handler)
515 if (handler instanceof ServletHandler)
516 setServletHandler((ServletHandler) handler);
517 else if (handler instanceof SessionHandler)
518 setSessionHandler((SessionHandler) handler);
519 else if (handler instanceof SecurityHandler)
520 setSecurityHandler((SecurityHandler)handler);
521 else if (handler == null || handler instanceof HandlerWrapper)
523 super.setHandler(handler);
527 throw new IllegalArgumentException();
531 /* ------------------------------------------------------------ */
533 * Insert a HandlerWrapper before the first Session,Security or ServletHandler
534 * but after any other HandlerWrappers.
536 public void insertHandler(HandlerWrapper handler)
538 HandlerWrapper h=this;
540 // Skip any injected handlers
541 while (h.getHandler() instanceof HandlerWrapper)
543 HandlerWrapper wrapper = (HandlerWrapper)h.getHandler();
544 if (wrapper instanceof SessionHandler ||
545 wrapper instanceof SecurityHandler ||
546 wrapper instanceof ServletHandler)
551 h.setHandler(handler);
555 /* ------------------------------------------------------------ */
557 * @return The decorator list used to resource inject new Filters, Servlets and EventListeners
559 public List<Decorator> getDecorators()
561 return Collections.unmodifiableList(_decorators);
564 /* ------------------------------------------------------------ */
566 * @param decorators The lis of {@link Decorator}s
568 public void setDecorators(List<Decorator> decorators)
571 _decorators.addAll(decorators);
574 /* ------------------------------------------------------------ */
576 * @param decorator The decorator to add
578 public void addDecorator(Decorator decorator)
580 _decorators.add(decorator);
583 /* ------------------------------------------------------------ */
584 void destroyServlet(Servlet servlet)
586 for (Decorator decorator : _decorators)
587 decorator.destroy(servlet);
590 /* ------------------------------------------------------------ */
591 void destroyFilter(Filter filter)
593 for (Decorator decorator : _decorators)
594 decorator.destroy(filter);
597 /* ------------------------------------------------------------ */
598 public static class JspPropertyGroup implements JspPropertyGroupDescriptor
600 private List<String> _urlPatterns = new ArrayList<String>();
601 private String _elIgnored;
602 private String _pageEncoding;
603 private String _scriptingInvalid;
604 private String _isXml;
605 private List<String> _includePreludes = new ArrayList<String>();
606 private List<String> _includeCodas = new ArrayList<String>();
607 private String _deferredSyntaxAllowedAsLiteral;
608 private String _trimDirectiveWhitespaces;
609 private String _defaultContentType;
610 private String _buffer;
611 private String _errorOnUndeclaredNamespace;
616 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getUrlPatterns()
618 public Collection<String> getUrlPatterns()
620 return new ArrayList<String>(_urlPatterns); // spec says must be a copy
623 public void addUrlPattern (String s)
625 if (!_urlPatterns.contains(s))
630 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getElIgnored()
632 public String getElIgnored()
637 public void setElIgnored (String s)
643 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getPageEncoding()
645 public String getPageEncoding()
647 return _pageEncoding;
650 public void setPageEncoding(String pageEncoding)
652 _pageEncoding = pageEncoding;
655 public void setScriptingInvalid(String scriptingInvalid)
657 _scriptingInvalid = scriptingInvalid;
660 public void setIsXml(String isXml)
665 public void setDeferredSyntaxAllowedAsLiteral(String deferredSyntaxAllowedAsLiteral)
667 _deferredSyntaxAllowedAsLiteral = deferredSyntaxAllowedAsLiteral;
670 public void setTrimDirectiveWhitespaces(String trimDirectiveWhitespaces)
672 _trimDirectiveWhitespaces = trimDirectiveWhitespaces;
675 public void setDefaultContentType(String defaultContentType)
677 _defaultContentType = defaultContentType;
680 public void setBuffer(String buffer)
685 public void setErrorOnUndeclaredNamespace(String errorOnUndeclaredNamespace)
687 _errorOnUndeclaredNamespace = errorOnUndeclaredNamespace;
691 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getScriptingInvalid()
693 public String getScriptingInvalid()
695 return _scriptingInvalid;
699 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIsXml()
701 public String getIsXml()
707 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIncludePreludes()
709 public Collection<String> getIncludePreludes()
711 return new ArrayList<String>(_includePreludes); //must be a copy
714 public void addIncludePrelude(String prelude)
716 if (!_includePreludes.contains(prelude))
717 _includePreludes.add(prelude);
721 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getIncludeCodas()
723 public Collection<String> getIncludeCodas()
725 return new ArrayList<String>(_includeCodas); //must be a copy
728 public void addIncludeCoda (String coda)
730 if (!_includeCodas.contains(coda))
731 _includeCodas.add(coda);
735 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getDeferredSyntaxAllowedAsLiteral()
737 public String getDeferredSyntaxAllowedAsLiteral()
739 return _deferredSyntaxAllowedAsLiteral;
743 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getTrimDirectiveWhitespaces()
745 public String getTrimDirectiveWhitespaces()
747 return _trimDirectiveWhitespaces;
751 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getDefaultContentType()
753 public String getDefaultContentType()
755 return _defaultContentType;
759 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getBuffer()
761 public String getBuffer()
767 * @see javax.servlet.descriptor.JspPropertyGroupDescriptor#getErrorOnUndeclaredNamespace()
769 public String getErrorOnUndeclaredNamespace()
771 return _errorOnUndeclaredNamespace;
774 public String toString ()
776 StringBuffer sb = new StringBuffer();
777 sb.append("JspPropertyGroupDescriptor:");
778 sb.append(" el-ignored="+_elIgnored);
779 sb.append(" is-xml="+_isXml);
780 sb.append(" page-encoding="+_pageEncoding);
781 sb.append(" scripting-invalid="+_scriptingInvalid);
782 sb.append(" deferred-syntax-allowed-as-literal="+_deferredSyntaxAllowedAsLiteral);
783 sb.append(" trim-directive-whitespaces"+_trimDirectiveWhitespaces);
784 sb.append(" default-content-type="+_defaultContentType);
785 sb.append(" buffer="+_buffer);
786 sb.append(" error-on-undeclared-namespace="+_errorOnUndeclaredNamespace);
787 for (String prelude:_includePreludes)
788 sb.append(" include-prelude="+prelude);
789 for (String coda:_includeCodas)
790 sb.append(" include-coda="+coda);
791 return sb.toString();
795 /* ------------------------------------------------------------ */
796 public static class TagLib implements TaglibDescriptor
799 private String _location;
802 * @see javax.servlet.descriptor.TaglibDescriptor#getTaglibURI()
804 public String getTaglibURI()
809 public void setTaglibURI(String uri)
815 * @see javax.servlet.descriptor.TaglibDescriptor#getTaglibLocation()
817 public String getTaglibLocation()
822 public void setTaglibLocation(String location)
824 _location = location;
827 public String toString()
829 return ("TagLibDescriptor: taglib-uri="+_uri+" location="+_location);
834 /* ------------------------------------------------------------ */
835 public static class JspConfig implements JspConfigDescriptor
837 private List<TaglibDescriptor> _taglibs = new ArrayList<TaglibDescriptor>();
838 private List<JspPropertyGroupDescriptor> _jspPropertyGroups = new ArrayList<JspPropertyGroupDescriptor>();
840 public JspConfig() {}
843 * @see javax.servlet.descriptor.JspConfigDescriptor#getTaglibs()
845 public Collection<TaglibDescriptor> getTaglibs()
847 return new ArrayList<TaglibDescriptor>(_taglibs);
850 public void addTaglibDescriptor (TaglibDescriptor d)
856 * @see javax.servlet.descriptor.JspConfigDescriptor#getJspPropertyGroups()
858 public Collection<JspPropertyGroupDescriptor> getJspPropertyGroups()
860 return new ArrayList<JspPropertyGroupDescriptor>(_jspPropertyGroups);
863 public void addJspPropertyGroup(JspPropertyGroupDescriptor g)
865 _jspPropertyGroups.add(g);
868 public String toString()
870 StringBuffer sb = new StringBuffer();
871 sb.append("JspConfigDescriptor: \n");
872 for (TaglibDescriptor taglib:_taglibs)
873 sb.append(taglib+"\n");
874 for (JspPropertyGroupDescriptor jpg:_jspPropertyGroups)
876 return sb.toString();
881 /* ------------------------------------------------------------ */
882 public class Context extends ContextHandler.Context
884 /* ------------------------------------------------------------ */
886 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
889 public RequestDispatcher getNamedDispatcher(String name)
891 ContextHandler context=org.eclipse.jetty.servlet.ServletContextHandler.this;
892 if (_servletHandler==null)
894 ServletHolder holder = _servletHandler.getServlet(name);
895 if (holder==null || !holder.isEnabled())
897 return new Dispatcher(context, name);
900 /* ------------------------------------------------------------ */
902 * @since servlet-api-3.0
905 public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
908 throw new IllegalStateException();
910 if (filterName == null || "".equals(filterName.trim()))
911 throw new IllegalStateException("Missing filter name");
914 throw new UnsupportedOperationException();
916 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
917 FilterHolder holder = handler.getFilter(filterName);
921 holder = handler.newFilterHolder(Source.JAVAX_API);
922 holder.setName(filterName);
923 holder.setHeldClass(filterClass);
924 handler.addFilter(holder);
925 return holder.getRegistration();
927 if (holder.getClassName()==null && holder.getHeldClass()==null)
929 //preliminary filter registration completion
930 holder.setHeldClass(filterClass);
931 return holder.getRegistration();
934 return null; //existing filter
937 /* ------------------------------------------------------------ */
939 * @since servlet-api-3.0
942 public FilterRegistration.Dynamic addFilter(String filterName, String className)
945 throw new IllegalStateException();
947 if (filterName == null || "".equals(filterName.trim()))
948 throw new IllegalStateException("Missing filter name");
951 throw new UnsupportedOperationException();
953 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
954 FilterHolder holder = handler.getFilter(filterName);
958 holder = handler.newFilterHolder(Source.JAVAX_API);
959 holder.setName(filterName);
960 holder.setClassName(className);
961 handler.addFilter(holder);
962 return holder.getRegistration();
964 if (holder.getClassName()==null && holder.getHeldClass()==null)
966 //preliminary filter registration completion
967 holder.setClassName(className);
968 return holder.getRegistration();
971 return null; //existing filter
975 /* ------------------------------------------------------------ */
977 * @since servlet-api-3.0
980 public FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
983 throw new IllegalStateException();
985 if (filterName == null || "".equals(filterName.trim()))
986 throw new IllegalStateException("Missing filter name");
989 throw new UnsupportedOperationException();
991 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
992 FilterHolder holder = handler.getFilter(filterName);
996 holder = handler.newFilterHolder(Source.JAVAX_API);
997 holder.setName(filterName);
998 holder.setFilter(filter);
999 handler.addFilter(holder);
1000 return holder.getRegistration();
1003 if (holder.getClassName()==null && holder.getHeldClass()==null)
1005 //preliminary filter registration completion
1006 holder.setFilter(filter);
1007 return holder.getRegistration();
1010 return null; //existing filter
1013 /* ------------------------------------------------------------ */
1015 * @since servlet-api-3.0
1018 public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
1021 throw new IllegalStateException();
1023 if (servletName == null || "".equals(servletName.trim()))
1024 throw new IllegalStateException("Missing servlet name");
1027 throw new UnsupportedOperationException();
1029 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1030 ServletHolder holder = handler.getServlet(servletName);
1034 holder = handler.newServletHolder(Source.JAVAX_API);
1035 holder.setName(servletName);
1036 holder.setHeldClass(servletClass);
1037 handler.addServlet(holder);
1038 return dynamicHolderAdded(holder);
1041 //complete a partial registration
1042 if (holder.getClassName()==null && holder.getHeldClass()==null)
1044 holder.setHeldClass(servletClass);
1045 return holder.getRegistration();
1048 return null; //existing completed registration for servlet name
1051 /* ------------------------------------------------------------ */
1053 * @since servlet-api-3.0
1056 public ServletRegistration.Dynamic addServlet(String servletName, String className)
1059 throw new IllegalStateException();
1061 if (servletName == null || "".equals(servletName.trim()))
1062 throw new IllegalStateException("Missing servlet name");
1065 throw new UnsupportedOperationException();
1068 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1069 ServletHolder holder = handler.getServlet(servletName);
1073 holder = handler.newServletHolder(Source.JAVAX_API);
1074 holder.setName(servletName);
1075 holder.setClassName(className);
1076 handler.addServlet(holder);
1077 return dynamicHolderAdded(holder);
1080 //complete a partial registration
1081 if (holder.getClassName()==null && holder.getHeldClass()==null)
1083 holder.setClassName(className);
1084 return holder.getRegistration();
1087 return null; //existing completed registration for servlet name
1090 /* ------------------------------------------------------------ */
1092 * @since servlet-api-3.0
1095 public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
1098 throw new IllegalStateException();
1100 if (servletName == null || "".equals(servletName.trim()))
1101 throw new IllegalStateException("Missing servlet name");
1104 throw new UnsupportedOperationException();
1106 final ServletHandler handler = ServletContextHandler.this.getServletHandler();
1107 ServletHolder holder = handler.getServlet(servletName);
1110 holder = handler.newServletHolder(Source.JAVAX_API);
1111 holder.setName(servletName);
1112 holder.setServlet(servlet);
1113 handler.addServlet(holder);
1114 return dynamicHolderAdded(holder);
1117 //complete a partial registration
1118 if (holder.getClassName()==null && holder.getHeldClass()==null)
1120 holder.setServlet(servlet);
1121 return holder.getRegistration();
1124 return null; //existing completed registration for servlet name
1127 /* ------------------------------------------------------------ */
1129 public boolean setInitParameter(String name, String value)
1132 throw new IllegalStateException();
1135 throw new UnsupportedOperationException();
1137 return super.setInitParameter(name,value);
1140 /* ------------------------------------------------------------ */
1142 public <T extends Filter> T createFilter(Class<T> c) throws ServletException
1146 T f = createInstance(c);
1147 for (int i=_decorators.size()-1; i>=0; i--)
1149 Decorator decorator = _decorators.get(i);
1150 f=decorator.decorate(f);
1156 throw new ServletException(e);
1160 /* ------------------------------------------------------------ */
1162 public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
1166 T s = createInstance(c);
1167 for (int i=_decorators.size()-1; i>=0; i--)
1169 Decorator decorator = _decorators.get(i);
1170 s=decorator.decorate(s);
1176 throw new ServletException(e);
1182 public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
1184 if (_sessionHandler!=null)
1185 return _sessionHandler.getSessionManager().getDefaultSessionTrackingModes();
1190 public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
1192 if (_sessionHandler!=null)
1193 return _sessionHandler.getSessionManager().getEffectiveSessionTrackingModes();
1198 public FilterRegistration getFilterRegistration(String filterName)
1201 throw new UnsupportedOperationException();
1203 final FilterHolder holder=ServletContextHandler.this.getServletHandler().getFilter(filterName);
1204 return (holder==null)?null:holder.getRegistration();
1208 public Map<String, ? extends FilterRegistration> getFilterRegistrations()
1211 throw new UnsupportedOperationException();
1213 HashMap<String, FilterRegistration> registrations = new HashMap<String, FilterRegistration>();
1214 ServletHandler handler=ServletContextHandler.this.getServletHandler();
1215 FilterHolder[] holders=handler.getFilters();
1218 for (FilterHolder holder : holders)
1219 registrations.put(holder.getName(),holder.getRegistration());
1221 return registrations;
1225 public ServletRegistration getServletRegistration(String servletName)
1228 throw new UnsupportedOperationException();
1230 final ServletHolder holder=ServletContextHandler.this.getServletHandler().getServlet(servletName);
1231 return (holder==null)?null:holder.getRegistration();
1235 public Map<String, ? extends ServletRegistration> getServletRegistrations()
1238 throw new UnsupportedOperationException();
1240 HashMap<String, ServletRegistration> registrations = new HashMap<String, ServletRegistration>();
1241 ServletHandler handler=ServletContextHandler.this.getServletHandler();
1242 ServletHolder[] holders=handler.getServlets();
1245 for (ServletHolder holder : holders)
1246 registrations.put(holder.getName(),holder.getRegistration());
1248 return registrations;
1252 public SessionCookieConfig getSessionCookieConfig()
1255 throw new UnsupportedOperationException();
1257 if (_sessionHandler!=null)
1258 return _sessionHandler.getSessionManager().getSessionCookieConfig();
1263 public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
1266 throw new IllegalStateException();
1268 throw new UnsupportedOperationException();
1271 if (_sessionHandler!=null)
1272 _sessionHandler.getSessionManager().setSessionTrackingModes(sessionTrackingModes);
1276 public void addListener(String className)
1279 throw new IllegalStateException();
1281 throw new UnsupportedOperationException();
1282 super.addListener(className);
1286 public <T extends EventListener> void addListener(T t)
1289 throw new IllegalStateException();
1291 throw new UnsupportedOperationException();
1292 super.addListener(t);
1293 ListenerHolder holder = getServletHandler().newListenerHolder(Source.JAVAX_API);
1294 holder.setListener(t);
1295 getServletHandler().addListener(holder);
1299 public void addListener(Class<? extends EventListener> listenerClass)
1302 throw new IllegalStateException();
1304 throw new UnsupportedOperationException();
1305 super.addListener(listenerClass);
1309 public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
1313 T l = createInstance(clazz);
1314 for (int i=_decorators.size()-1; i>=0; i--)
1316 Decorator decorator = _decorators.get(i);
1317 l=decorator.decorate(l);
1323 throw new ServletException(e);
1329 public JspConfigDescriptor getJspConfigDescriptor()
1335 public void setJspConfigDescriptor(JspConfigDescriptor d)
1342 public void declareRoles(String... roleNames)
1345 throw new IllegalStateException();
1347 throw new UnsupportedOperationException();
1348 addRoles(roleNames);
1357 /* ------------------------------------------------------------ */
1358 /** Interface to decorate loaded classes.
1360 public interface Decorator
1362 <T> T decorate (T o);
1363 void destroy (Object o);