package org.dandelion.web;

import com.google.common.collect.Lists;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.*;
import io.undertow.servlet.handlers.DefaultServlet;
import io.undertow.servlet.util.ImmediateInstanceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

/**
 * Created on 2016-01-16.
 *
 * @author dolphineor
 */
public class WebAppServer implements DisposableBean {
    private final List<String> staticResourceMappings =
            Lists.newArrayList("*.css", "js", "*.ico", "*.gif", "*.jpg", "*.jpeg", "*.png");
    private final Logger logger = LoggerFactory.getLogger(WebAppServer.class);

    private final String webAppName;
    private final int port;

    private Undertow undertowServer;
    private DeploymentManager deploymentManager;

    public WebAppServer(String webAppName, int port) {
        this.webAppName = webAppName;
        this.port = port;
    }

    public WebAppServer start() throws IOException, ServletException {
        InstanceFactory<WebAppServletContainerInitializer> instanceFactory =
                new ImmediateInstanceFactory<>(new WebAppServletContainerInitializer());
        ServletContainerInitializerInfo sciInfo = new ServletContainerInitializerInfo(
                WebAppServletContainerInitializer.class, instanceFactory, new HashSet<>()
        );

        ServletInfo defaultServlet = Servlets.servlet("default", DefaultServlet.class)
                .addMappings(staticResourceMappings);
        DeploymentInfo deploymentInfo = Servlets.deployment()
                .addServletContainerInitalizer(sciInfo)
                .setClassLoader(this.getClass().getClassLoader())
                .setContextPath("/")
                .setDefaultEncoding("UTF-8")
                .setDeploymentName(webAppName + "-exploded")
                .setResourceManager(new ClassPathResourceManager(this.getClass().getClassLoader()))
                .addServlet(defaultServlet);
        deploymentManager = Servlets.defaultContainer().addDeployment(deploymentInfo);
        deploymentManager.deploy();

        HttpHandler httpHandler = deploymentManager.start();
        PathHandler pathHandler = Handlers.path(Handlers.redirect("/" + webAppName));
        pathHandler.addPrefixPath("/" + webAppName, httpHandler);

        undertowServer = Undertow.builder()
                .addHttpListener(port, "::0")
                .setHandler(httpHandler)
                .build();

        undertowServer.start();

        return this;
    }

    @Override
    public void destroy() throws Exception {
        logger.info("Stopping Undertow web server on port " + port);
        undertowServer.stop();
        deploymentManager.stop();
        deploymentManager.undeploy();
        logger.info("Undertow web server on port " + port + " stopped");
    }
}

@@ -65,37 +65,8 @@
 ts;%0A
-import io.undertow.Handlers;%0A
 impo
@@ -133,56 +133,8 @@
 er;%0A
-import io.undertow.server.handlers.PathHandler;%0A
 impo
@@ -822,16 +822,18 @@
 .css%22, %22
+*.
 js%22, %22*.
@@ -2117,22 +2117,8 @@
 Name
- + %22-exploded%22
 )%0A  
@@ -2448,160 +2448,8 @@
 t();
-%0A        PathHandler pathHandler = Handlers.path(Handlers.redirect(%22/%22 + webAppName));%0A        pathHandler.addPrefixPath(%22/%22 + webAppName, httpHandler);
 %0A%0A  
@@ -2534,11 +2534,17 @@
 t, %22
-::0
+localhost
 %22)%0A 

Update WebAppServer.java