Issue #4697 ServletContext sessionModes methods should throw UnsupportedOperationException (#4698)

* Issue #4697 ServletContext sessionModes methods should throw UnsupportedOperationException

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2020-03-25 11:16:36 +01:00 committed by GitHub
parent ea80253cca
commit 57324f3cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 1 deletions

View File

@ -1289,6 +1289,9 @@ public class ServletContextHandler extends ContextHandler
@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
{
if (!_enabled)
throw new UnsupportedOperationException();
if (_sessionHandler != null)
return _sessionHandler.getDefaultSessionTrackingModes();
return null;
@ -1297,6 +1300,9 @@ public class ServletContextHandler extends ContextHandler
@Override
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
{
if (!_enabled)
throw new UnsupportedOperationException();
if (_sessionHandler != null)
return _sessionHandler.getEffectiveSessionTrackingModes();
return null;

View File

@ -49,6 +49,7 @@ import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.ServletResponse;
import javax.servlet.SessionTrackingMode;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -132,6 +133,36 @@ public class ServletContextHandlerTest
throw new IllegalStateException("MySCI already called");
ctx.setAttribute("MySCI.startup", Boolean.TRUE);
ctx.addListener(new MyContextListener(callSessionTimeouts, timeout));
//test that SCI can call the sessionmodes methods
try
{
ctx.getDefaultSessionTrackingModes();
ctx.setAttribute("MySCI.defaultSessionTrackingModes", Boolean.TRUE);
}
catch (UnsupportedOperationException e)
{
ctx.setAttribute("MySCI.defaultSessionTrackingModes", Boolean.FALSE);
}
try
{
ctx.getEffectiveSessionTrackingModes();
ctx.setAttribute("MySCI.effectiveSessionTrackingModes", Boolean.TRUE);
}
catch (UnsupportedOperationException e)
{
ctx.setAttribute("MySCI.effectiveSessionTrackingModes", Boolean.FALSE);
}
try
{
ctx.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.URL));
ctx.setAttribute("MySCI.setSessionTrackingModes", Boolean.TRUE);
}
catch (UnsupportedOperationException e)
{
ctx.setAttribute("MySCI.setSessionTrackingModes", Boolean.FALSE);
}
if (callSessionTimeouts)
{
try
@ -191,6 +222,42 @@ public class ServletContextHandlerTest
assertNull(sce.getServletContext().getAttribute("MyContextListener.contextInitialized"));
sce.getServletContext().setAttribute("MyContextListener.contextInitialized", Boolean.TRUE);
assertNull(sce.getServletContext().getAttribute("MyContextListener.defaultSessionTrackingModes"));
try
{
sce.getServletContext().getDefaultSessionTrackingModes();
sce.getServletContext().setAttribute("MyContextListener.defaultSessionTrackingModes", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
//Should NOT be able to call getDefaultSessionTrackingModes from programmatic SCL
sce.getServletContext().setAttribute("MyContextListener.defaultSessionTrackingModes", Boolean.TRUE);
}
assertNull(sce.getServletContext().getAttribute("MyContextListener.effectiveSessionTrackingModes"));
try
{
sce.getServletContext().getEffectiveSessionTrackingModes();
sce.getServletContext().setAttribute("MyContextListener.effectiveSessionTrackingModes", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
//Should NOT be able to call getEffectiveSessionTrackingModes from programmatic SCL
sce.getServletContext().setAttribute("MyContextListener.effectiveSessionTrackingModes", Boolean.TRUE);
}
assertNull(sce.getServletContext().getAttribute("MyContextListener.setSessionTrackingModes"));
try
{
sce.getServletContext().setSessionTrackingModes(EnumSet.of(SessionTrackingMode.URL));
sce.getServletContext().setAttribute("MyContextListener.setSessionTrackingModes", Boolean.FALSE);
}
catch (UnsupportedOperationException e)
{
//Should NOT be able to call setSessionTrackingModes from programmatic SCL
sce.getServletContext().setAttribute("MyContextListener.setSessionTrackingModes", Boolean.TRUE);
}
if (callSessionTimeouts)
{
try
@ -547,7 +614,13 @@ public class ServletContextHandlerTest
root.addBean(new MySCIStarter(root.getServletContext(), new MySCI()), true);
_server.start();
assertTrue((Boolean)root.getServletContext().getAttribute("MySCI.startup"));
assertTrue((Boolean)root.getServletContext().getAttribute("MySCI.defaultSessionTrackingModes"));
assertTrue((Boolean)root.getServletContext().getAttribute("MySCI.effectiveSessionTrackingModes"));
assertTrue((Boolean)root.getServletContext().getAttribute("MySCI.setSessionTrackingModes"));
assertTrue((Boolean)root.getServletContext().getAttribute("MyContextListener.contextInitialized"));
assertTrue((Boolean)root.getServletContext().getAttribute("MyContextListener.defaultSessionTrackingModes"));
assertTrue((Boolean)root.getServletContext().getAttribute("MyContextListener.effectiveSessionTrackingModes"));
assertTrue((Boolean)root.getServletContext().getAttribute("MyContextListener.setSessionTrackingModes"));
}
@Test