initialHandler = servletChain(targetServlet, targetServlet.getManagedServlet());
} else {
initialHandler = defaultHandler;
}
} else {
FilterHandler handler;
if (targetServlet != null) {
handler = new FilterHandler(noExtension, targetServlet);
} else {
handler = new FilterHandler(noExtension, defaultServlet);
}
initialHandler = servletChain(handler, targetServlet == null ? defaultServlet.getManagedServlet() : targetServlet.getManagedServlet());
}
if (path.endsWith("/*")) {
String prefix = path.substring(0, path.length() - 2);
builder.addPrefixMatch(prefix, initialHandler);
for (Map.Entry<String, Map<DispatcherType, List<ManagedFilter>>> entry : extension.entrySet()) {
ServletHandler pathServlet = targetServlet;
if (pathServlet == null) {
pathServlet = extensionServlets.get(entry.getKey());
}
if (pathServlet == null) {
pathServlet = defaultServlet;
}
HttpHandler handler = pathServlet;
if (!entry.getValue().isEmpty()) {
handler = new FilterHandler(entry.getValue(), handler);
}
builder.addExtensionMatch(prefix, entry.getKey(), servletChain(handler, pathServlet.getManagedServlet()));
}
} else if (path.isEmpty()) {
builder.addExactMatch("/", initialHandler);
} else {
builder.addExactMatch(path, initialHandler);
}
}
//now setup name based mappings
//these are used for name based dispatch
for (Map.Entry<String, ServletHandler> entry : allServlets.entrySet()) {
final Map<DispatcherType, List<ManagedFilter>> filters = new HashMap<DispatcherType, List<ManagedFilter>>();
for (final FilterMappingInfo filterMapping : deploymentInfo.getFilterMappings()) {
ManagedFilter filter = managedFilterMap.get(filterMapping.getFilterName());
if (filterMapping.getMappingType() == FilterMappingInfo.MappingType.SERVLET) {
if (filterMapping.getMapping().equals(entry.getKey())) {
addToListMap(filters, filterMapping.getDispatcher(), filter);
}
}
}
if (filters.isEmpty()) {
builder.addNameMatch(entry.getKey(), servletChain(entry.getValue(), entry.getValue().getManagedServlet()));
} else {
builder.addNameMatch(entry.getKey(), servletChain(new FilterHandler(filters, entry.getValue()), entry.getValue().getManagedServlet()));
}
}
builder.setDefaultServlet(defaultHandler);