*/
private ServletPathMatches setupServletChains(final ServletContextImpl servletContext, final CompositeThreadSetupAction threadSetupAction, final ApplicationListeners listeners) {
final List<Lifecycle> lifecycles = new ArrayList<Lifecycle>();
//create the default servlet
ServletChain defaultHandler = null;
ServletHandler defaultServlet = null;
final Map<String, ManagedFilter> managedFilterMap = new LinkedHashMap<String, ManagedFilter>();
final Map<String, ServletHandler> allServlets = new HashMap<String, ServletHandler>();
final Map<String, ServletHandler> extensionServlets = new HashMap<String, ServletHandler>();
final Map<String, ServletHandler> pathServlets = new HashMap<String, ServletHandler>();
final Set<String> pathMatches = new HashSet<String>();
final Set<String> extensionMatches = new HashSet<String>();
DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
for (Map.Entry<String, FilterInfo> entry : deploymentInfo.getFilters().entrySet()) {
final ManagedFilter mf = new ManagedFilter(entry.getValue(), servletContext);
managedFilterMap.put(entry.getValue().getName(), mf);
lifecycles.add(mf);
}
for (FilterMappingInfo mapping : deploymentInfo.getFilterMappings()) {
if (mapping.getMappingType() == FilterMappingInfo.MappingType.URL) {
String path = mapping.getMapping();
if (!path.startsWith("*.")) {
pathMatches.add(path);
} else {
extensionMatches.add(path.substring(2));
}
}
}
for (Map.Entry<String, ServletInfo> entry : deploymentInfo.getServlets().entrySet()) {
ServletInfo servlet = entry.getValue();
final ManagedServlet managedServlet = new ManagedServlet(servlet, servletContext);
lifecycles.add(managedServlet);
final ServletHandler handler = new ServletHandler(managedServlet);
allServlets.put(entry.getKey(), handler);
for (String path : entry.getValue().getMappings()) {
if (path.equals("/")) {
//the default servlet
pathMatches.add("/*");
if (pathServlets.containsKey("/*")) {
throw UndertowServletMessages.MESSAGES.twoServletsWithSameMapping(path);
}
defaultServlet = handler;
defaultHandler = servletChain(handler, managedServlet);
} else if (!path.startsWith("*.")) {
pathMatches.add(path);
if (pathServlets.containsKey(path)) {
throw UndertowServletMessages.MESSAGES.twoServletsWithSameMapping(path);
}
pathServlets.put(path, handler);
} else {
String ext = path.substring(2);
extensionMatches.add(ext);
extensionServlets.put(ext, handler);
}
}
}
if (defaultServlet == null) {
final DefaultServletConfig config = deploymentInfo.getDefaultServletConfig() == null ? new DefaultServletConfig() : deploymentInfo.getDefaultServletConfig();
DefaultServlet defaultInstance = new DefaultServlet(deployment, config, deploymentInfo.getWelcomePages());
final ManagedServlet managedDefaultServlet = new ManagedServlet(new ServletInfo("io.undertow.DefaultServlet", DefaultServlet.class, new ImmediateInstanceFactory<Servlet>(defaultInstance)), servletContext);
lifecycles.add(managedDefaultServlet);
pathMatches.add("/*");
defaultServlet = new ServletHandler(managedDefaultServlet);
defaultHandler = new ServletChain(defaultServlet, managedDefaultServlet);
}
final ServletPathMatches.Builder builder = ServletPathMatches.builder();
for (final String path : pathMatches) {
ServletHandler targetServlet = resolveServletForPath(path, pathServlets);
final Map<DispatcherType, List<ManagedFilter>> noExtension = new HashMap<DispatcherType, List<ManagedFilter>>();
final Map<String, Map<DispatcherType, List<ManagedFilter>>> extension = new HashMap<String, Map<DispatcherType, List<ManagedFilter>>>();
for (String ext : extensionMatches) {
extension.put(ext, new HashMap<DispatcherType, List<ManagedFilter>>());
}
for (final FilterMappingInfo filterMapping : deploymentInfo.getFilterMappings()) {
ManagedFilter filter = managedFilterMap.get(filterMapping.getFilterName());
if (filterMapping.getMappingType() == FilterMappingInfo.MappingType.SERVLET) {
if (targetServlet != null) {
if (filterMapping.getMapping().equals(targetServlet.getManagedServlet().getServletInfo().getName())) {
addToListMap(noExtension, filterMapping.getDispatcher(), filter);
for (Map<DispatcherType, List<ManagedFilter>> l : extension.values()) {
addToListMap(l, filterMapping.getDispatcher(), filter);
}
}
}
} else {
if (filterMapping.getMapping().isEmpty() || !filterMapping.getMapping().startsWith("*.")) {
if (isFilterApplicable(path, filterMapping.getMapping())) {
addToListMap(noExtension, filterMapping.getDispatcher(), filter);
for (Map<DispatcherType, List<ManagedFilter>> l : extension.values()) {
addToListMap(l, filterMapping.getDispatcher(), filter);
}
}
} else {
addToListMap(extension.get(filterMapping.getMapping().substring(2)), filterMapping.getDispatcher(), filter);
}
}
}
final ServletChain initialHandler;
if (noExtension.isEmpty()) {
if (targetServlet != null) {
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);