Skip to content

Commit

Permalink
Issue #4700 ServletContext.createXXX methods should throw Unsupported…
Browse files Browse the repository at this point in the history
…OperationException

Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Mar 23, 2020
1 parent ea1418a commit 9da6520
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,42 @@ public void setSessionTimeout(int sessionTimeout)
_sessionHandler.setMaxInactiveInterval((int)tmp);
}
}

@Override
public <T extends Servlet> T createServlet(Class<T> clazz) throws ServletException
{
if (!_enabled)
throw new UnsupportedOperationException();
return super.createServlet(clazz);
}

@Override
public <T extends Filter> T createFilter(Class<T> clazz) throws ServletException
{
if (!_enabled)
throw new UnsupportedOperationException();
return super.createFilter(clazz);
}

@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException
{
if (!_enabled)
throw new UnsupportedOperationException();
try
{
checkListener(clazz);
}
catch (IllegalArgumentException e)
{
//Bizarrely, according to the spec, it is NOT an error to create an instance of
//a ServletContextListener from inside a ServletContextListener, but it IS an error
//to call addListener with one!
if (!ServletContextListener.class.isAssignableFrom(clazz))
throw e;
}
return super.createListener(clazz);
}

@Override
public void addListener(String className)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,66 @@ public void sessionIdChanged(HttpSessionEvent event, String oldSessionId)
}
}

/**
* ServletContextListener that is designed to be added programmatically,
* which should make all of the createListener, createServlet, createFilter
* methods fail with UnsupportedOperationException
*
*/
public class CreatingSCL implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
try
{
sce.getServletContext().createFilter(MyFilter.class);
sce.getServletContext().setAttribute("CreatingSCL.filter", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
sce.getServletContext().setAttribute("CreatingSCL.filter", Boolean.TRUE);
}
catch (Exception e)
{
fail(e);
}

try
{
sce.getServletContext().createServlet(HelloServlet.class);
sce.getServletContext().setAttribute("CreatingSCL.servlet", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
sce.getServletContext().setAttribute("CreatingSCL.servlet", Boolean.TRUE);
}
catch (Exception e)
{
fail(e);
}

try
{
sce.getServletContext().createListener(MyContextListener.class);
sce.getServletContext().setAttribute("CreatingSCL.listener", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
sce.getServletContext().setAttribute("CreatingSCL.listener", Boolean.TRUE);
}
catch (Exception e)
{
fail(e);
}
}

@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}

public class InitialListener implements ServletContextListener
{
@Override
Expand Down Expand Up @@ -432,7 +492,7 @@ public void contextInitialized(ServletContextEvent sce)
{
MyContextListener contextListener = sce.getServletContext().createListener(MyContextListener.class);
sce.getServletContext().addListener(contextListener);
fail("Adding SCI from an SCI!");
fail("Adding SCL from an SCL!");
}
catch (IllegalArgumentException e)
{
Expand Down Expand Up @@ -748,6 +808,76 @@ public void testAddServletFromServlet() throws Exception
fail(e);
}
}

@Test
public void testCreateMethodsFromSCI() throws Exception
{
//A filter can be created by an SCI
ContextHandlerCollection contexts = new ContextHandlerCollection();
_server.setHandler(contexts);

ServletContextHandler root = new ServletContextHandler(contexts, "/");
class FilterCreatingSCI implements ServletContainerInitializer
{
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
try
{
ctx.createFilter(MyFilter.class);
}
catch (Exception e)
{
fail(e);
}

try
{
ctx.createServlet(HelloServlet.class);
}
catch (Exception e)
{
fail(e);
}

try
{
ctx.createListener(MyContextListener.class);
}
catch (Exception e)
{
fail(e);
}
}
}

root.addBean(new MySCIStarter(root.getServletContext(), new FilterCreatingSCI()), true);
_server.start();
}

@Test
public void testCreateMethodsFromSCL() throws Exception
{
//A filter can be created by an SCI
ContextHandlerCollection contexts = new ContextHandlerCollection();
_server.setHandler(contexts);

ServletContextHandler root = new ServletContextHandler(contexts, "/");
class ListenerCreatingSCI implements ServletContainerInitializer
{
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException
{
ctx.addListener(new CreatingSCL());
}
}

root.addBean(new MySCIStarter(root.getServletContext(), new ListenerCreatingSCI()), true);
_server.start();
assertTrue((Boolean)root.getServletContext().getAttribute("CreatingSCL.filter"));
assertTrue((Boolean)root.getServletContext().getAttribute("CreatingSCL.servlet"));
assertTrue((Boolean)root.getServletContext().getAttribute("CreatingSCL.listener"));
}

@Test
public void testAddFilterFromServlet() throws Exception
Expand Down

0 comments on commit 9da6520

Please sign in to comment.