package com.wesabe.bouncer;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.client.HttpClient;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.servlet.GzipFilter;
import org.mortbay.thread.QueuedThreadPool;

import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PooledDataSource;
import com.wesabe.bouncer.auth.Authenticator;
import com.wesabe.bouncer.auth.WesabeAuthenticator;
import com.wesabe.bouncer.proxy.ProxyHttpExchangeFactory;
import com.wesabe.bouncer.servlets.AuthenticationFilter;
import com.wesabe.bouncer.servlets.ProxyServlet;
import com.wesabe.servlet.ErrorReporterFilter;
import com.wesabe.servlet.SafeFilter;
import com.wesabe.servlet.errors.DebugErrorReporter;
import com.wesabe.servlet.errors.ErrorReporter;

public class Runner {
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			System.err.println("Usage: java -jar <bouncer.jar> <config file> <port>");
			System.exit(-1);
		}
		
		final Configuration config = new Configuration(args[0]);
		final int port = Integer.valueOf(args[1]);
		
		final Server server = new Server(port);
		server.setGracefulShutdown(5000);
		server.setSendServerVersion(false);
		server.setStopAtShutdown(true);
		
		final Context context = new Context();
		context.addFilter(SafeFilter.class, "/*", 0);
		
		final ErrorReporter reporter = new DebugErrorReporter("Exception Notifier <support@wesabe.com>", "coda@wesabe.com", "bouncer");
		final FilterHolder errorHolder = new FilterHolder(new ErrorReporterFilter(reporter, "thanks for helping"));
		context.addFilter(errorHolder, "/*", 0);
		
		if (config.isHttpCompressionEnabled()) {
			final FilterHolder gzipHolder = new FilterHolder(GzipFilter.class);
			gzipHolder.setInitParameter("minGzipSize", config.getHttpCompressionMinimumSize().toString());
			gzipHolder.setInitParameter("mimeTypes", config.getHttpCompressableMimeTypes());
			context.addFilter(gzipHolder, "/*", 0);
		}
		
		final PooledDataSource dataSource = (PooledDataSource) DataSources.pooledDataSource(
				DataSources.unpooledDataSource(
						config.getJdbcUri().toASCIIString(),
						config.getJdbcUsername(),
						config.getJdbcPassword()
				),
				config.getC3P0Properties()
		);
		
		final Authenticator authenticator = new WesabeAuthenticator(dataSource);
		
		context.addFilter(new FilterHolder(
			new AuthenticationFilter(authenticator, config.getAuthenticationRealm(), config.getAuthenticationErrorMessage())
		), "/*", 0);
		
		final HttpClient client = new HttpClient();
		client.setThreadPool(new QueuedThreadPool(20));
		client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
		final ProxyHttpExchangeFactory factory = new ProxyHttpExchangeFactory(config.getBackendUri());
		final ServletHolder proxyHolder = new ServletHolder(new ProxyServlet(client, factory));
		context.addServlet(proxyHolder, "/*");
		
		server.addHandler(context);
		
		server.start();
		server.join();
	}
}

@@ -852,16 +852,72 @@
 eporter;
+%0Aimport com.wesabe.servlet.errors.SendmailErrorReporter;
 %0A%0Apublic
@@ -1496,16 +1496,55 @@
 eporter 
+reporter;%0A%09%09if (config.isDebug()) %7B%0A%09%09%09
 reporter
@@ -1617,143 +1617,394 @@
 %22, %22
-coda@wesabe.com%22, %22bouncer%22);%0A%09%09final FilterHolder errorHolder = new FilterHolder(new ErrorReporterFilter(reporter, %22thanks for helping
+you@wesabe.com%22, %22bouncer%22);%0A%09%09%7D else %7B%0A%09%09%09reporter = new SendmailErrorReporter(%22Exception Notifier %3Csupport@wesabe.com%3E%22, %22eng@wesabe.com%22, %22bouncer%22, %22/usr/sbin/sendmail%22);%0A%09%09%7D%0A%09%09%0A%09%09final FilterHolder errorHolder = new FilterHolder(new ErrorReporterFilter(reporter, %22Wesabe engineers have been alerted to this error. If you have further questions, please contact %3Csupport@wesabe.com%3E.
 %22));

Add specific reporters for debug and production.