X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FReloadingPage.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FReloadingPage.java;h=0c8f51637a7f760b28d783fdd69dbb6be493de91;hp=0000000000000000000000000000000000000000;hb=70d761ab3aeba06a3acfd0a8bccdde49a5d8b9b4;hpb=2f21e4a2402f1c7a55175f223118521dbd2d27d8
diff --git a/src/main/java/net/pterodactylus/sone/web/ReloadingPage.java b/src/main/java/net/pterodactylus/sone/web/ReloadingPage.java
new file mode 100644
index 0000000..0c8f516
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sone/web/ReloadingPage.java
@@ -0,0 +1,88 @@
+/*
+ * Sone - ReloadingPage.java - Copyright © 2010â2015 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 .
+ */
+
+package net.pterodactylus.sone.web;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.io.StreamCopier;
+import net.pterodactylus.util.web.Page;
+import net.pterodactylus.util.web.Request;
+import net.pterodactylus.util.web.Response;
+
+/**
+ * {@link Page} implementation that delivers static files from the filesystem.
+ *
+ * @param
+ * The type of the request
+ * @author David âBombeâ Roden
+ */
+public class ReloadingPage implements Page {
+
+ private final String pathPrefix;
+ private final String filesystemPath;
+ private final String mimeType;
+
+ public ReloadingPage(String pathPrefix, String filesystemPathPrefix, String mimeType) {
+ this.pathPrefix = pathPrefix;
+ this.filesystemPath = filesystemPathPrefix;
+ this.mimeType = mimeType;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getPath() {
+ return pathPrefix;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isPrefixPage() {
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Response handleRequest(REQ request, Response response) throws IOException {
+ String path = request.getUri().getPath();
+ int lastSlash = path.lastIndexOf('/');
+ String filename = path.substring(lastSlash + 1);
+ InputStream fileInputStream = new FileInputStream(new File(filesystemPath, filename));
+ if (fileInputStream == null) {
+ return response.setStatusCode(404).setStatusText("Not found.");
+ }
+ OutputStream contentOutputStream = response.getContent();
+ try {
+ StreamCopier.copy(fileInputStream, contentOutputStream);
+ } finally {
+ Closer.close(fileInputStream);
+ Closer.close(contentOutputStream);
+ }
+ return response.setStatusCode(200).setStatusText("OK").setContentType(mimeType);
+ }
+}