package greensopinion.finance.services.web;

import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;

import com.google.common.util.concurrent.UncaughtExceptionHandlers;

public class WebServerRunner {

	private static final int PORT = 8080;

	public static void main(String[] args) throws Exception {
		Thread.currentThread().setUncaughtExceptionHandler(UncaughtExceptionHandlers.systemExit());

		Server server = new Server(PORT);

		ServletContextHandler contextHandler = new ServletContextHandler(server, "/");
		addDefaultServlet(contextHandler);

		addJerseyServlet(contextHandler);
		addCorHeadersFilter(contextHandler);

		server.start();
		server.join();
	}

	protected static void addDefaultServlet(ServletContextHandler contextHandler) {
		ServletHolder servletHolder = contextHandler.addServlet(DefaultServlet.class, "/");
		servletHolder.setInitParameter("resourceBase", "../greensopinion.finance.ui/dist/");
	}

	protected static void addJerseyServlet(ServletContextHandler contextHandler) {
		ServletHolder jerseyServletHolder = new ServletHolder(new ServletContainer());
		jerseyServletHolder.setInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,
				Application.class.getCanonicalName());
		contextHandler.addServlet(jerseyServletHolder, "/api/*");
	}

	private static void addCorHeadersFilter(ServletContextHandler contextHandler) {
		FilterHolder filterHolder = new FilterHolder(new CorHeaderFilter());
		contextHandler.addFilter(filterHolder, "/api/*", EnumSet.of(DispatcherType.REQUEST));
	}

}

@@ -522,16 +522,66 @@
 dlers;%0A%0A
+import greensopinion.finance.services.demo.Demo;%0A%0A
 public c
@@ -794,24 +794,46 @@
 emExit());%0A%0A
+%09%09maybeEnableDemo();%0A%0A
 %09%09Server ser
@@ -1086,39 +1086,135 @@
 .join();%0A%09%7D%0A%0A%09pr
-otected
+ivate static void maybeEnableDemo() %7B%0A%09%09if (Demo.isEnabled()) %7B%0A%09%09%09new Demo().setup();%0A%09%09%7D%0A%09%7D%0A%0A%09private
  static void add
@@ -1452,15 +1452,13 @@
 %0A%09pr
-otec
+iva
 te
-d
  sta

support demo mode in the web server