package leap.spring.boot.web;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.ServletContext;

@Configuration
public class Web2Configuration extends AbstractWebConfiguration {

    @Configuration
    @ConditionalOnClass(ConfigurableJettyWebServerFactory.class)
    @ConditionalOnProperty(value = "jetty-booting", matchIfMissing = true)
    static class Jetty2Configuration {

        @Bean
        public WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> leapJettyWebServerFactoryCustomizer(
                Environment environment,
                ServerProperties serverProperties) {
            return new WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>() {
                @Override
                public void customize(ConfigurableJettyWebServerFactory factory) {
                    factory.addServerCustomizers(new JettyServerCustomizer() {
                        @Override
                        public void customize(Server server) {
                            Handler handler;
                            if (server.getHandler() instanceof HandlerWrapper) {
                                handler = ((HandlerWrapper) server.getHandler()).getHandler();
                            } else {
                                handler = server.getHandler();
                            }
                            WebAppContext context = (WebAppContext) handler;
                            ServletContext servletContext = context.getServletContext();
                            boot(servletContext);
                        }
                    });
                }
            };
        }

    }

}
@@ -1772,23 +1772,73 @@
     
-Handler h
+WebAppContext context = resolveWebAppContext(server.getH
 andler
+())
 ;%0A  
@@ -1871,149 +1871,142 @@
 if (
-server.getHandler() instanceof HandlerWrapper) %7B%0A                                handler = ((HandlerWrapper) server.getHandler()).getHandler(
+null == context) %7B%0A                                throw new IllegalStateException(%22Unable to resolve jetty's web application context%22
 );%0A 
@@ -2037,15 +2037,8 @@
    %7D
- else %7B
 %0A   
@@ -2066,39 +2066,65 @@
     
-    handler = server.getHandler
+ServletContext servletContext = context.getServletContext
 ();%0A
@@ -2143,33 +2143,53 @@
                 
-%7D
+boot(servletContext);
 %0A               
@@ -2188,37 +2188,68 @@
                 
+ %7D%0A%0A
      
+                   private 
 WebAppContext co
@@ -2250,19 +2250,15 @@
 ext 
-context = (
+resolve
 WebA
@@ -2270,17 +2270,24 @@
 text
-)
+(Handler
  handler
 ;%0A  
@@ -2282,17 +2282,19 @@
  handler
-;
+) %7B
 %0A       
@@ -2318,117 +2318,382 @@
     
-ServletContext servletContext = context.getServletContext();%0A                            boot(servletContext)
+if (handler instanceof WebAppContext) %7B%0A                                return (WebAppContext) handler;%0A                            %7D%0A                            if (handler instanceof HandlerWrapper) %7B%0A                                return resolveWebAppContext(((HandlerWrapper) handler).getHandler());%0A                            %7D%0A                            return null
 ;%0A  

resolve web app context