439663 Allow mappings to be declared before servlet/filter

This commit is contained in:
Jan Bartel 2014-07-16 15:52:04 +10:00
parent dd5cdab54c
commit 50f11bea09
2 changed files with 28 additions and 11 deletions

View File

@ -116,8 +116,6 @@ public class ServletHandler extends ScopedHandler
private ServletHolder[] _servlets=new ServletHolder[0];
private ServletMapping[] _servletMappings;
private Map<String,ServletMapping> _servletPathMappings = new HashMap<String,ServletMapping>();
private final Map<String,FilterHolder> _filterNameMap= new HashMap<>();
private List<FilterMapping> _filterPathMappings;
private MultiMap<FilterMapping> _filterNameMappings;
@ -340,7 +338,6 @@ public class ServletHandler extends ScopedHandler
_filterPathMappings=null;
_filterNameMappings=null;
_servletPathMap=null;
_servletPathMappings=null;
}
/* ------------------------------------------------------------ */
@ -415,10 +412,26 @@ public class ServletHandler extends ScopedHandler
*/
public ServletMapping getServletMapping(String pathSpec)
{
if (pathSpec == null || _servletPathMappings == null)
if (pathSpec == null || _servletMappings == null)
return null;
return _servletPathMappings.get(pathSpec);
ServletMapping mapping = null;
for (int i=0; i<_servletMappings.length && mapping == null; i++)
{
ServletMapping m = _servletMappings[i];
if (m.getPathSpecs() != null)
{
for (String p:m.getPathSpecs())
{
if (pathSpec.equals(p))
{
mapping = m;
break;
}
}
}
}
return mapping;
}
/* ------------------------------------------------------------ */
@ -1443,9 +1456,7 @@ public class ServletHandler extends ScopedHandler
//for each path, look at the mappings where it is referenced
//if a mapping is for a servlet that is not enabled, skip it
Set<ServletMapping> mappings = sms.get(pathSpec);
ServletMapping finalMapping = null;
for (ServletMapping mapping : mappings)
{
@ -1483,7 +1494,6 @@ public class ServletHandler extends ScopedHandler
}
_servletPathMap=pm;
_servletPathMappings=servletPathMappings;
}
// flush filter chain cache
@ -1542,7 +1552,7 @@ public class ServletHandler extends ScopedHandler
{
updateBeans(_filterMappings,filterMappings);
_filterMappings = filterMappings;
updateMappings();
if (isStarted()) updateMappings();
invalidateChainsCache();
}
@ -1567,7 +1577,7 @@ public class ServletHandler extends ScopedHandler
{
updateBeans(_servletMappings,servletMappings);
_servletMappings = servletMappings;
updateMappings();
if (isStarted()) updateMappings();
invalidateChainsCache();
}

View File

@ -335,6 +335,7 @@ public class ServletHandlerTest
//add a non-programmatic one to begin with
handler.addFilterWithMapping(fh1, "/*", EnumSet.allOf(DispatcherType.class));
handler.updateMappings();
FilterMapping[] mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertTrue(fh1 == mappings[0].getFilterHolder());
@ -343,6 +344,7 @@ public class ServletHandlerTest
fh4.setServletHandler(handler);
handler.addFilter(fh4);
fh4.getRegistration().addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertEquals(2, mappings.length);
@ -353,6 +355,7 @@ public class ServletHandlerTest
fh3.setServletHandler(handler);
handler.addFilter(fh3);
fh3.getRegistration().addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertEquals(3, mappings.length);
@ -364,6 +367,7 @@ public class ServletHandlerTest
fh5.setServletHandler(handler);
handler.addFilter(fh5);
fh5.getRegistration().addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertEquals(4, mappings.length);
@ -376,6 +380,7 @@ public class ServletHandlerTest
FilterHolder f = new FilterHolder(Source.EMBEDDED);
f.setName("non-programmatic");
handler.addFilterWithMapping(f, "/*", EnumSet.allOf(DispatcherType.class));
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertEquals(5, mappings.length);
@ -391,6 +396,7 @@ public class ServletHandlerTest
pf.setName("programmaticA");
handler.addFilter(pf);
pf.getRegistration().addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);
assertEquals(6, mappings.length);
@ -407,6 +413,7 @@ public class ServletHandlerTest
pf2.setName("programmaticB");
handler.addFilter(pf2);
pf2.getRegistration().addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
handler.updateMappings();
mappings = handler.getFilterMappings();
assertNotNull(mappings);