From: David ‘Bombe’ Roden <bombe@pterodactylus.net>
Date: Fri, 15 Oct 2010 15:24:53 +0000 (+0200)
Subject: Create a static file delivery page, use it to deliver CSS, too.
X-Git-Tag: 0.1-RC1~327
X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ac137128a3ac11736882d7bb2a9ea2bcb12576f0;p=Sone.git

Create a static file delivery page, use it to deliver CSS, too.
---

diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java
index 96feb71..ad7e510 100644
--- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java
+++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java
@@ -33,9 +33,9 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.sone.template.SoneAccessor;
-import net.pterodactylus.sone.web.page.CSSPage;
 import net.pterodactylus.sone.web.page.PageToadlet;
 import net.pterodactylus.sone.web.page.PageToadletFactory;
+import net.pterodactylus.sone.web.page.StaticPage;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.template.DateFilter;
@@ -184,7 +184,8 @@ public class WebInterface extends AbstractService {
 		pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
 		pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
 		pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
-		pageToadlets.add(pageToadletFactory.createPageToadlet(new CSSPage("css/", "/static/css/")));
+		pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
+		pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
 
 		ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
 		toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
diff --git a/src/main/java/net/pterodactylus/sone/web/page/CSSPage.java b/src/main/java/net/pterodactylus/sone/web/page/CSSPage.java
deleted file mode 100644
index 9688f49..0000000
--- a/src/main/java/net/pterodactylus/sone/web/page/CSSPage.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * shortener - CSSPage.java - Copyright © 2010 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.page;
-
-import java.io.InputStream;
-
-/**
- * {@link Page} implementation that delivers CSS files from the class path.
- * 
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class CSSPage implements Page {
-
-	/** The prefix for {@link #getPath()}. */
-	private final String pathPrefix;
-
-	/** The path used as prefix when loading resources. */
-	private final String resourcePathPrefix;
-
-	/**
-	 * Creates a new CSS page.
-	 * 
-	 * @param pathPrefix
-	 *            The prefix for {@link #getPath()}
-	 * @param resourcePathPrefix
-	 *            The path prefix when loading resources
-	 */
-	public CSSPage(String pathPrefix, String resourcePathPrefix) {
-		this.pathPrefix = pathPrefix;
-		this.resourcePathPrefix = resourcePathPrefix;
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public String getPath() {
-		return pathPrefix;
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public Response handleRequest(Request request) {
-		String path = request.getURI().getPath();
-		int lastSlash = path.lastIndexOf('/');
-		String cssFilename = path.substring(lastSlash + 1);
-		InputStream cssInputStream = getClass().getResourceAsStream(resourcePathPrefix + cssFilename);
-		if (cssInputStream == null) {
-			return new Response(404, "Not found.", null, (String) null);
-		}
-		return new Response(200, "OK", "text/css", null, cssInputStream);
-	}
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/web/page/StaticPage.java b/src/main/java/net/pterodactylus/sone/web/page/StaticPage.java
new file mode 100644
index 0000000..7bff1e1
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sone/web/page/StaticPage.java
@@ -0,0 +1,77 @@
+/*
+ * shortener - CSSPage.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.page;
+
+import java.io.InputStream;
+
+/**
+ * {@link Page} implementation that delivers static files from the class path.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class StaticPage implements Page {
+
+	/** The prefix for {@link #getPath()}. */
+	private final String pathPrefix;
+
+	/** The path used as prefix when loading resources. */
+	private final String resourcePathPrefix;
+
+	/** The MIME type for the files this path contains. */
+	private final String mimeType;
+
+	/**
+	 * Creates a new CSS page.
+	 *
+	 * @param pathPrefix
+	 *            The prefix for {@link #getPath()}
+	 * @param resourcePathPrefix
+	 *            The path prefix when loading resources
+	 * @param mimeType
+	 *            The MIME type of the files this path contains
+	 */
+	public StaticPage(String pathPrefix, String resourcePathPrefix, String mimeType) {
+		this.pathPrefix = pathPrefix;
+		this.resourcePathPrefix = resourcePathPrefix;
+		this.mimeType = mimeType;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public String getPath() {
+		return pathPrefix;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public Response handleRequest(Request request) {
+		String path = request.getURI().getPath();
+		int lastSlash = path.lastIndexOf('/');
+		String filename = path.substring(lastSlash + 1);
+		InputStream fileInputStream = getClass().getResourceAsStream(resourcePathPrefix + filename);
+		if (fileInputStream == null) {
+			return new Response(404, "Not found.", null, (String) null);
+		}
+		return new Response(200, "OK", mimeType, null, fileInputStream);
+	}
+
+}