/**
 * 
 */
package traffic;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * @author alan
 *
 */
public class TrafficRequestHandler implements HttpHandler {
	private Checker trafficChecker;
	private Splitter andSplitter = Splitter.on("&");
	
	private TrafficRequestHandler(Checker trafficChecker) {
		super();
		this.trafficChecker = trafficChecker;
	}
	
	public static HttpHandler create(Checker trafficChecker) {
		return new TrafficRequestHandler(trafficChecker);
	}
	/* (non-Javadoc)
	 * @see com.sun.net.httpserver.HttpHandler#handle(com.sun.net.httpserver.HttpExchange)
	 */
	@Override
	public void handle(HttpExchange t) throws IOException {
		t.getRequestBody();
		String response = dispatch(trafficChecker, parseGetParams(t)); 
		t.sendResponseHeaders(200, response.length());
		OutputStream os = t.getResponseBody();
		os.write(response.getBytes());
		os.close();
	}
	
	private Command parseGetParams(HttpExchange exchange) {
		String query = exchange.getRequestURI().getQuery();
		Iterable<String> keyValPairs = andSplitter.split(query);
		String first = keyValPairs.iterator().next().toUpperCase();
		return Command.fromString(first);
	}

	private String dispatch(Checker trafficChecker, Command cmd) {
		if (cmd != null) {
			return cmd.doCommand(trafficChecker);
		} else {
			return "NO COMMAND";
		}
	}

	private static enum Command {
		CHECK {
			@Override 
			public String doCommand(Checker c) {
				return c.retrieve().toString();
			}
		},
		FORCE {
			@Override
			public String doCommand(Checker c) {
				return c.force().toString();
			}
		},
		UNKNOWN {
			@Override
			public String doCommand(Checker c) {
				return this.name();
			}
		};
		
		public abstract String doCommand(Checker c);
		
		private static final Set<String> commandSet = initializeCommandNameSet();
		
		private static Set<String> initializeCommandNameSet() {
			ImmutableSet.Builder<String> setBuilder = new ImmutableSet.Builder<String>();
			for (Command c : Command.values()) {
				setBuilder.add(c.name());
			}
			return setBuilder.build();
		}
		
		public static Command fromString(String name) {
			if (Command.commandSet.contains(name)) {
				return Command.valueOf(name);
			} else {
				return Command.UNKNOWN;
			}
		}
	}
}

@@ -190,16 +190,55 @@
 bleSet;%0A
+import com.sun.net.httpserver.Headers;%0A
 import c
@@ -273,16 +273,16 @@
 change;%0A
-
 import c
@@ -918,16 +918,55 @@
 Body();%0A
+%09%09Command command = parseGetParams(t);%0A
 %09%09String
@@ -1006,28 +1006,172 @@
 er, 
-parseGetParams(t)); 
+command); %0A%09%09if (command.doSetJsContentType()) %7B%0A%09%09%09Headers responseHeaders = t.getResponseHeaders();%0A%09%09%09responseHeaders.set(%22Content-Type%22, %22text/javascript%22);%0A%09%09%7D
 %0A%09%09t
@@ -1891,16 +1891,203 @@
 %09%7D%0A%09%09%7D,%0A
+%09%09CHECKJS %7B%0A%09%09%09@Override%0A%09%09%09public String doCommand(Checker c) %7B%0A%09%09%09%09return c.retrieve().toString();%0A%09%09%09%7D%0A%09%09%09@Override%0A%09%09%09public boolean doSetJsContentType() %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D,%0A
 %09%09FORCE 
@@ -2282,24 +2282,24 @@
 %09%09%7D%0A%09%09%7D;%0A%09%09%0A
-
 %09%09public abs
@@ -2333,16 +2333,72 @@
 ker c);%0A
+%09%09public boolean doSetJsContentType() %7B return false; %7D%0A
 %09%09%0A%09%09pri

Add the CHECKJS query param which returns the traffic info json with content-type text/javascript