/*
 * Copyright 2011 Jonathan Anderson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.footlights.ui.web;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.regex.Pattern;

import com.google.common.collect.Maps;

import me.footlights.core.plugin.PluginWrapper;


/** Code to serve static plugin content. */
class StaticContentServer implements WebServer
{
	StaticContentServer(Map<String,PluginWrapper> plugins)
	{
		paths = Maps.newHashMap();

		for (Map.Entry<String,PluginWrapper> plugin : plugins.entrySet())
			paths.put(plugin.getKey(), plugin.getValue().getClass());

		paths.put("footlights", getClass());

		// TODO: this is a temporary cheat for testing.
		paths.put("sandbox", getClass());
	}

	@Override public String name() { return "static content server"; }

	@Override public Response handle(final Request request) throws SecurityException
	{
		if(request.path().contains(".."))
			return Response.error(new SecurityException("'..' present in " + request));

		Response.Builder response = Response.newBuilder();
		if (request.path().isEmpty())
			response.setResponse(
				mimeType("index.html"),
				this.getClass().getResourceAsStream("index.html"));

		else
		{
			Class<?> c = paths.get(request.prefix());
			if (c == null)
				return Response.error(
					new FileNotFoundException("No such directory '" + request.prefix() + "'"));

			String path = request.shift().path();
			InputStream data = c.getResourceAsStream(path);
			if (data == null)
				return Response.error(new FileNotFoundException(path));

			response.setResponse(mimeType(path), data);
		}

		return response.build();
	}

	/** Guess the MIME type for static content. */
	private String mimeType(String path)
	{
		if(Pattern.matches(".*\\.css", path))
			return "text/css";

		else if(Pattern.matches(".*\\.gif", path))
			return "image/gif";

		else if(Pattern.matches(".*\\.html", path))
			return "text/html";

		else if(Pattern.matches(".*\\.jpe?g", path))
			return "image/jpeg";

		else if(Pattern.matches(".*\\.js", path))
			return "text/javascript";

		else if(Pattern.matches(".*\\.png", path))
			return "image/png";

		return "text/xml";
	}

	private final Map<String,Class<?>> paths;
}

@@ -679,26 +679,75 @@
 ava.io.I
-nputStream
+OException;%0Aimport java.io.InputStream;%0Aimport java.net.URL
 ;%0Aimport
@@ -2023,56 +2023,352 @@
 %0A%09%09%09
-InputStream data = c.getResourceAsStream(path
+URL resource = c.getResource(request.shift().path());%0A%09%09%09if (resource == null)%0A%09%09%09%09return Response.error(new FileNotFoundException(path));%0A%0A%09%09%09java.io.File file = new java.io.File(resource.getFile());%0A%09%09%09if (!file.isFile())%0A%09%09%09%09return Response.error(new FileNotFoundException(path));%0A%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09InputStream data = resource.openStream(
 );%0A
+%09
 %09%09%09i
@@ -2376,32 +2376,33 @@
  (data == null)%0A
+%09
 %09%09%09%09return Respo
@@ -2446,16 +2446,17 @@
 ath));%0A%0A
+%09
 %09%09%09respo
@@ -2494,16 +2494,106 @@
  data);%0A
+%09%09%09%7D%0A%09%09%09catch (IOException e) %7B return Response.error(new FileNotFoundException(path)); %7D%0A
 %09%09%7D%0A%0A%09%09r

Return better 404s from StaticContentServer.