package ch.rasc.musicsearch;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import javax.servlet.Filter;

import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.filter.CharacterEncodingFilter;

import ch.rasc.edsutil.optimizer.WebResourceProcessor;

@Configuration
@ComponentScan(basePackages = { "ch.ralscha.extdirectspring", "ch.rasc.musicsearch" })
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) throws Exception {
		System.setProperty("spring.profiles.active",
				System.getProperty("spring.profiles.active", "development"));
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public Filter characterEncodingFilter() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding(StandardCharsets.UTF_8.name());
		characterEncodingFilter.setForceEncoding(false);
		return characterEncodingFilter;
	}

	@Bean
	public ServletContextInitializer servletContextInitializer(
			final Environment environment) {
		return servletContext -> {
			try {
				boolean isDefaultProfileActive = environment.acceptsProfiles("default");
				WebResourceProcessor processor = new WebResourceProcessor(servletContext,
						isDefaultProfileActive);
				processor.process();

				ClassPathResource cpr = new ClassPathResource("index.html");
				String indexHtml = StreamUtils.copyToString(cpr.getInputStream(),
						StandardCharsets.UTF_8);
				indexHtml = indexHtml.replace("application.app_css",
						(String) servletContext.getAttribute("app_css"));
				indexHtml = indexHtml.replace("application.app_js",
						(String) servletContext.getAttribute("app_js"));
				indexHtml = indexHtml.replace("application.getContextPath()",
						servletContext.getContextPath());
				servletContext.setAttribute("index.html", indexHtml);
			}
			catch (IOException e) {
				LoggerFactory.getLogger(Application.class).error("read index.html", e);
			}
		};
	}

}

@@ -120,24 +120,80 @@
 et.Filter;%0A%0A
+import org.apache.coyote.http11.AbstractHttp11Protocol;%0A
 import org.s
@@ -453,33 +453,213 @@
 ded.
-ServletContextInitializer
+EmbeddedServletContainerCustomizer;%0Aimport org.springframework.boot.context.embedded.ServletContextInitializer;%0Aimport org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
 ;%0Aim
@@ -1007,16 +1007,59 @@
 source;%0A
+import org.springframework.http.MediaType;%0A
 import o
@@ -1623,120 +1623,35 @@
 %0A%09%09S
-ystem.setProperty(%22spring.profiles.active%22,%0A%09%09%09%09System.getProperty(%22spring.profiles.active%22, %22development%22));%0A%09%09
+pringApplication app = new 
 Spri
@@ -1667,12 +1667,8 @@
 tion
-.run
 (App
@@ -1681,18 +1681,57 @@
 on.class
-, 
+);%0A%09%09app.setShowBanner(false);%0A%09%09app.run(
 args);%0A%09
@@ -3055,10 +3055,708 @@
 %09%7D;%0A%09%7D%0A%0A
+%09@Bean%0A%09public EmbeddedServletContainerCustomizer servletContainerCustomizer() %7B%0A%09%09return servletContainer -%3E ((TomcatEmbeddedServletContainerFactory) servletContainer)%0A%09%09%09%09.addConnectorCustomizers(connector -%3E %7B%0A%09%09%09%09%09AbstractHttp11Protocol%3C?%3E httpProtocol = (AbstractHttp11Protocol%3C?%3E) connector%0A%09%09%09%09%09%09%09.getProtocolHandler();%0A%09%09%09%09%09httpProtocol.setCompression(%22on%22);%0A%09%09%09%09%09httpProtocol.setCompressionMinSize(512);%0A%09%09%09%09%09String mimeTypes = httpProtocol.getCompressableMimeTypes();%0A%09%09%09%09%09String additionalMimeTypes = mimeTypes + %22,%22%0A%09%09%09%09%09%09%09+ MediaType.APPLICATION_JSON_VALUE + %22,%22%0A%09%09%09%09%09%09%09+ %22application/javascript,text/css%22;%0A%0A%09%09%09%09%09httpProtocol.setCompressableMimeTypes(additionalMimeTypes);%0A%09%09%09%09%7D);%0A%09%7D%0A%0A
 %7D%0A

Enable compression. Disable banner