JETTY-1523 It is imposible to map servlet to "/" using WebApplicationInitializer

This commit is contained in:
Jan Bartel 2012-07-04 17:11:11 +02:00
parent f1d413fe03
commit 96fe2d6c3f
5 changed files with 84 additions and 17 deletions

View File

@ -154,17 +154,53 @@ public class WebServletAnnotation extends DiscoveredAnnotation
}
}
//check the url-patterns, if there annotation has a new one, add it
ServletMapping[] mappings = _context.getServletHandler().getServletMappings();
//check the url-patterns
//ServletSpec 3.0 p81 If a servlet already has url mappings from a
//descriptor the annotation is ignored
if (mappings == null && metaData.getOriginDescriptor(servletName+".servlet.mappings") != null)
//webxml or fragment descriptor the annotation is ignored. However, we want to be able to
//replace mappings that were given in webdefault.xml
boolean mappingsExist = false;
boolean anyNonDefaults = false;
ServletMapping[] allMappings = _context.getServletHandler().getServletMappings();
if (allMappings != null)
{
ServletMapping mapping = new ServletMapping();
mapping.setServletName(servletName);
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
_context.getServletHandler().addServletMapping(mapping);
for (ServletMapping m:allMappings)
{
if (m.getServletName() != null && servletName.equals(m.getServletName()))
{
mappingsExist = true;
if (!m.isDefault())
{
anyNonDefaults = true;
break;
}
}
}
}
if (anyNonDefaults)
return; //if any mappings already set by a descriptor that is not webdefault.xml, we're done
boolean clash = false;
if (mappingsExist)
{
for (String p:urlPatternList)
{
ServletMapping m = _context.getServletHandler().getServletMapping(p);
if (m != null && !m.isDefault())
{
//trying to override a servlet-mapping that was added not by webdefault.xml
clash = true;
break;
}
}
}
if (!mappingsExist || !clash)
{
ServletMapping m = new ServletMapping();
m.setServletName(servletName);
m.setPathSpecs(LazyList.toStringArray(urlPatternList));
_context.getServletHandler().addServletMapping(m);
}
}
}

View File

@ -281,6 +281,7 @@ public class ServletHandler extends ScopedHandler
*/
public ServletMapping getServletMapping(String pattern)
{
ServletMapping theMapping = null;
if (_servletMappings!=null)
{
for (ServletMapping m:_servletMappings)
@ -291,12 +292,12 @@ public class ServletHandler extends ScopedHandler
for (String path:paths)
{
if (pattern.equals(path))
return m;
theMapping = m;
}
}
}
}
return null;
return theMapping;
}
/* ------------------------------------------------------------ */

View File

@ -705,17 +705,24 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
Set<String> clash=null;
for (String pattern : urlPatterns)
{
if (_servletHandler.getServletMapping(pattern)!=null)
ServletMapping mapping = _servletHandler.getServletMapping(pattern);
if (mapping!=null)
{
if (clash==null)
clash=new HashSet<String>();
clash.add(pattern);
//if the servlet mapping was from a default descriptor, then allow it to be overridden
if (!mapping.isDefault())
{
if (clash==null)
clash=new HashSet<String>();
clash.add(pattern);
}
}
}
//if there were any clashes amongst the urls, return them
if (clash!=null)
return clash;
//otherwise apply all of them
ServletMapping mapping = new ServletMapping();
mapping.setServletName(ServletHolder.this.getName());
mapping.setPathSpecs(urlPatterns);

View File

@ -21,6 +21,8 @@ public class ServletMapping
{
private String[] _pathSpecs;
private String _servletName;
private boolean _default;
/* ------------------------------------------------------------ */
public ServletMapping()
@ -72,6 +74,25 @@ public class ServletMapping
_servletName = servletName;
}
/* ------------------------------------------------------------ */
/**
* @return
*/
public boolean isDefault()
{
return _default;
}
/* ------------------------------------------------------------ */
/**
* @param default1
*/
public void setDefault(boolean fromDefault)
{
_default = fromDefault;
}
/* ------------------------------------------------------------ */
public String toString()

View File

@ -621,7 +621,8 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
{
//no servlet mappings
context.getMetaData().setOrigin(servlet_name+".servlet.mappings", descriptor);
addServletMapping(servlet_name, node, context);
ServletMapping mapping = addServletMapping(servlet_name, node, context);
mapping.setDefault(context.getMetaData().getOrigin(servlet_name+".servlet.mappings") == Origin.WebDefaults);
break;
}
case WebXml:
@ -1164,7 +1165,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
* @param node
* @param context
*/
protected void addServletMapping (String servletName, XmlParser.Node node, WebAppContext context)
protected ServletMapping addServletMapping (String servletName, XmlParser.Node node, WebAppContext context)
{
ServletMapping mapping = new ServletMapping();
mapping.setServletName(servletName);
@ -1179,6 +1180,7 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor
}
mapping.setPathSpecs((String[]) paths.toArray(new String[paths.size()]));
context.getServletHandler().addServletMapping(mapping);
return mapping;
}
/**