Add check for duplicate FilterHolder,ServletHolder objects
This commit is contained in:
Jan Bartel 2016-11-03 10:55:02 +11:00
parent 3a8cdc93d8
commit f3f31d163c
2 changed files with 197 additions and 7 deletions

View File

@ -1015,7 +1015,11 @@ public class ServletHandler extends ScopedHandler
try
{
setServlets(ArrayUtil.addToArray(holders, servlet, ServletHolder.class));
synchronized (this)
{
if (servlet != null && !containsServletHolder(servlet))
setServlets(ArrayUtil.addToArray(holders, servlet, ServletHolder.class));
}
ServletMapping mapping = new ServletMapping();
mapping.setServletName(servlet.getName());
@ -1039,7 +1043,14 @@ public class ServletHandler extends ScopedHandler
*/
public void addServlet(ServletHolder holder)
{
setServlets(ArrayUtil.addToArray(getServlets(), holder, ServletHolder.class));
if (holder == null)
return;
synchronized (this)
{
if (!containsServletHolder(holder))
setServlets(ArrayUtil.addToArray(getServlets(), holder, ServletHolder.class));
}
}
/* ------------------------------------------------------------ */
@ -1121,7 +1132,11 @@ public class ServletHandler extends ScopedHandler
try
{
setFilters(ArrayUtil.addToArray(holders, holder, FilterHolder.class));
synchronized (this)
{
if (holder != null && !containsFilterHolder(holder))
setFilters(ArrayUtil.addToArray(holders, holder, FilterHolder.class));
}
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
@ -1189,7 +1204,11 @@ public class ServletHandler extends ScopedHandler
try
{
setFilters(ArrayUtil.addToArray(holders, holder, FilterHolder.class));
synchronized (this)
{
if (holder != null && !containsFilterHolder(holder))
setFilters(ArrayUtil.addToArray(holders, holder, FilterHolder.class));
}
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
@ -1234,7 +1253,13 @@ public class ServletHandler extends ScopedHandler
public void addFilter (FilterHolder filter, FilterMapping filterMapping)
{
if (filter != null)
setFilters(ArrayUtil.addToArray(getFilters(), filter, FilterHolder.class));
{
synchronized (this)
{
if (!containsFilterHolder(filter))
setFilters(ArrayUtil.addToArray(getFilters(), filter, FilterHolder.class));
}
}
if (filterMapping != null)
addFilterMapping(filterMapping);
}
@ -1246,8 +1271,14 @@ public class ServletHandler extends ScopedHandler
*/
public void addFilter (FilterHolder filter)
{
if (filter != null)
setFilters(ArrayUtil.addToArray(getFilters(), filter, FilterHolder.class));
if (filter == null)
return;
synchronized (this)
{
if (!containsFilterHolder(filter))
setFilters(ArrayUtil.addToArray(getFilters(), filter, FilterHolder.class));
}
}
/* ------------------------------------------------------------ */
@ -1578,7 +1609,36 @@ public class ServletHandler extends ScopedHandler
if (getHandler()!=null)
nextHandle(URIUtil.addPaths(request.getServletPath(),request.getPathInfo()),baseRequest,request,response);
}
protected synchronized boolean containsFilterHolder (FilterHolder holder)
{
if (_filters == null)
return false;
boolean found = false;
for (FilterHolder f:_filters)
{
if (f == holder)
found = true;
}
return found;
}
protected synchronized boolean containsServletHolder (ServletHolder holder)
{
if (_servlets == null)
return false;
boolean found = false;
for (ServletHolder s:_servlets)
{
if (s == holder)
found = true;
}
return found;
}
/* ------------------------------------------------------------ */
/**
* @param filterChainsCached The filterChainsCached to set.

View File

@ -101,6 +101,136 @@ public class ServletHandlerTest
}
@Test
public void testAddFilterIgnoresDuplicates() throws Exception
{
ServletHandler handler = new ServletHandler();
FilterHolder h = new FilterHolder();
h.setName("x");
handler.addFilter(h);
FilterHolder[] holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders[0]==h);
handler.addFilter(h);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 1);
assertTrue(holders[0] == h);
FilterHolder h2 = new FilterHolder();
h2.setName("x"); //not allowed by servlet spec, just here to test object equality
handler.addFilter(h2);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 2);
assertTrue(holders[1] == h2);
}
@Test
public void testAddFilterIgnoresDuplicates2() throws Exception
{
ServletHandler handler = new ServletHandler();
FilterHolder h = new FilterHolder();
h.setName("x");
FilterMapping m = new FilterMapping();
m.setPathSpec("/*");
m.setFilterHolder(h);
handler.addFilter(h,m);
FilterHolder[] holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders[0]==h);
FilterMapping m2 = new FilterMapping();
m2.setPathSpec("/*");
m2.setFilterHolder(h);
handler.addFilter(h, m2);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 1);
assertTrue(holders[0] == h);
FilterHolder h2 = new FilterHolder();
h2.setName("x"); //not allowed by servlet spec, just here to test object equality
FilterMapping m3 = new FilterMapping();
m3.setPathSpec("/*");
m3.setFilterHolder(h);
handler.addFilter(h2, m3);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 2);
assertTrue(holders[1] == h2);
}
@Test
public void testAddFilterWithMappingIgnoresDuplicateFilters() throws Exception
{
ServletHandler handler = new ServletHandler();
FilterHolder h = new FilterHolder();
h.setName("x");
handler.addFilterWithMapping(h,"/*", 0);
FilterHolder[] holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders[0]==h);
handler.addFilterWithMapping(h, "/*", 1);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 1);
assertTrue(holders[0] == h);
FilterHolder h2 = new FilterHolder();
h2.setName("x"); //not allowed by servlet spec, just here to test object equality
handler.addFilterWithMapping(h2, "/*", 0);
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 2);
assertTrue(holders[1] == h2);
}
@Test
public void testAddFilterWithMappingIngoresDuplicateFilters2 () throws Exception
{
ServletHandler handler = new ServletHandler();
FilterHolder h = new FilterHolder();
h.setName("x");
handler.addFilterWithMapping(h,"/*", EnumSet.allOf(DispatcherType.class));
FilterHolder[] holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders[0]==h);
handler.addFilterWithMapping(h, "/x", EnumSet.allOf(DispatcherType.class));
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 1);
assertTrue(holders[0] == h);
FilterHolder h2 = new FilterHolder();
h2.setName("x"); //not allowed by servlet spec, just here to test object equality
handler.addFilterWithMapping(h2, "/*", EnumSet.allOf(DispatcherType.class));
holders = handler.getFilters();
assertNotNull(holders);
assertTrue(holders.length == 2);
assertTrue(holders[1] == h2);
}
@Test
public void testDuplicateMappingsForbidden() throws Exception
{