From aa9f039132c8b5cd46e35b9775be7e2cda8abcdf Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 25 May 2017 21:44:56 +0200 Subject: [PATCH] Replace reloading page with Kotlin version --- .../sone/web/pages/ReloadingPage.java | 83 ---------------------- .../pterodactylus/sone/web/pages/ReloadingPage.kt | 36 ++++++++++ .../sone/web/pages/ReloadingPageTest.kt | 4 +- 3 files changed, 39 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/ReloadingPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/ReloadingPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/pages/ReloadingPage.java b/src/main/java/net/pterodactylus/sone/web/pages/ReloadingPage.java deleted file mode 100644 index a36c3fd..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/ReloadingPage.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Sone - ReloadingPage.java - Copyright © 2010–2016 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.pages; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -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); - try (InputStream fileInputStream = new FileInputStream(new File(filesystemPath, filename)); - OutputStream contentOutputStream = response.getContent()) { - StreamCopier.copy(fileInputStream, contentOutputStream); - } catch (FileNotFoundException fnfe1) { - return response.setStatusCode(404).setStatusText("Not found."); - } - return response.setStatusCode(200).setStatusText("OK").setContentType(mimeType); - } -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ReloadingPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ReloadingPage.kt new file mode 100644 index 0000000..697d173 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ReloadingPage.kt @@ -0,0 +1,36 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.util.web.Page +import net.pterodactylus.util.web.Request +import net.pterodactylus.util.web.Response +import java.io.File + +/** + * [Page] implementation that delivers static files from the filesystem. + */ +class ReloadingPage(private val prefix: String, private val path: String, private val mimeType: String): Page { + + override fun isPrefixPage() = true + + override fun getPath() = prefix + + override fun handleRequest(request: R, response: Response): Response { + val filename = request.uri.path.split("/").last() + File(path, filename).also { file -> + if (file.exists()) { + response.content.use { output -> + file.forEachBlock { buffer, bytesRead -> + output.write(buffer, 0, bytesRead) + } + } + response.statusCode = 200 + response.contentType = mimeType + } else { + response.statusCode = 404 + response.statusText = "Not found" + } + } + return response + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ReloadingPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ReloadingPageTest.kt index 6dbcdc0..a281851 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ReloadingPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ReloadingPageTest.kt @@ -16,7 +16,7 @@ import kotlin.text.Charsets.UTF_8 class ReloadingPageTest : WebPageTest() { @Rule @JvmField val tempFolder = TemporaryFolder() - private val folder by lazy { tempFolder.newFolder() } + private val folder by lazy { tempFolder.newFolder()!! } private val page by lazy { ReloadingPage("/prefix/", folder.path, "text/plain") } @Test @@ -34,6 +34,7 @@ class ReloadingPageTest : WebPageTest() { request("/prefix/path/file.txt") page.handleRequest(freenetRequest, response) assertThat(response.statusCode, equalTo(404)) + assertThat(response.statusText, equalTo("Not found")) } @Test @@ -42,6 +43,7 @@ class ReloadingPageTest : WebPageTest() { request("/prefix/path/file.txt") page.handleRequest(freenetRequest, response) assertThat(response.statusCode, equalTo(200)) + assertThat(response.statusText, equalTo("OK")) assertThat(response.contentType, equalTo("text/plain")) assertThat(responseBytes, equalTo("Hello\nWorld\n".toByteArray())) } -- 2.7.4