Replace get image page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 27 Apr 2017 20:11:18 +0000 (22:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 27 Apr 2017 20:11:18 +0000 (22:11 +0200)
src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java b/src/main/java/net/pterodactylus/sone/web/pages/GetImagePage.java
deleted file mode 100644 (file)
index bcf0eda..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Sone - GetImagePage.java - Copyright © 2011–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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.pages;
-
-import java.io.IOException;
-import java.net.URI;
-
-import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetPage;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.web.Response;
-
-/**
- * Page that delivers a {@link TemporaryImage} to the browser.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class GetImagePage implements FreenetPage {
-
-       /** The Sone web interface. */
-       private final WebInterface webInterface;
-
-       /**
-        * Creates a new “get image” page.
-        *
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public GetImagePage(WebInterface webInterface) {
-               this.webInterface = webInterface;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String getPath() {
-               return "getImage.html";
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isPrefixPage() {
-               return false;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public Response handleRequest(FreenetRequest request, Response response) throws IOException {
-               String imageId = request.getHttpRequest().getParam("image");
-               TemporaryImage temporaryImage = webInterface.getCore().getTemporaryImage(imageId);
-               if (temporaryImage == null) {
-                       return response.setStatusCode(404).setStatusText("Not found.").setContentType("text/html; charset=utf-8");
-               }
-               String contentType= temporaryImage.getMimeType();
-               return response.setStatusCode(200).setStatusText("OK").setContentType(contentType).addHeader("Content-Disposition", "attachment; filename=" + temporaryImage.getId() + "." + contentType.substring(contentType.lastIndexOf('/') + 1)).write(temporaryImage.getImageData());
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isLinkExcepted(URI link) {
-               return false;
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/GetImagePage.kt
new file mode 100644 (file)
index 0000000..3e9eed0
--- /dev/null
@@ -0,0 +1,42 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetPage
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.web.Response
+import java.net.URI
+
+/**
+ * Page that delivers a {@link TemporaryImage} to the browser.
+ */
+class GetImagePage(webInterface: WebInterface): FreenetPage {
+
+       private val core = webInterface.core!!
+
+       override fun getPath(): String {
+               return "getImage.html"
+       }
+
+       override fun isPrefixPage(): Boolean {
+               return false
+       }
+
+       override fun handleRequest(request: FreenetRequest, response: Response): Response {
+               val image = core.getTemporaryImage(request.httpRequest.getParam("image")) ?: return response.apply {
+                       statusCode = 404
+                       statusText = "Not found."
+                       contentType = "text/html; charset=utf-8"
+               }
+               return response.apply {
+                       statusCode = 200
+                       contentType = image.mimeType
+                       content.write(image.imageData)
+                       addHeader("Content-Disposition", "attachment; filename=${image.id}.${image.mimeType.split('/')[1]}")
+               }
+       }
+
+       override fun isLinkExcepted(link: URI?): Boolean {
+               return false
+       }
+
+}
index fb96a4c..151f1ae 100644 (file)
@@ -1,9 +1,8 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.TemporaryImage
-import net.pterodactylus.sone.web.pages.GetImagePage
-import net.pterodactylus.sone.web.pages.WebPageTest
 import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
 import org.hamcrest.Matchers.equalTo
 import org.junit.Test
 
@@ -33,6 +32,8 @@ class GetImagePageTest : WebPageTest() {
        fun `invalid image returns 404 response`() {
                page.handleRequest(freenetRequest, response)
                assertThat(response.statusCode, equalTo(404))
+               assertThat(response.statusText, equalTo("Not found."))
+               assertThat(response.contentType, equalTo("text/html; charset=utf-8"))
                assertThat(responseBytes, equalTo(ByteArray(0)))
        }
 
@@ -48,6 +49,10 @@ class GetImagePageTest : WebPageTest() {
                assertThat(response.statusCode, equalTo(200))
                assertThat(response.contentType, equalTo("image/test"))
                assertThat(responseBytes, equalTo(ByteArray(5, Int::toByte)))
+               println(response.headers.map { it.name to it.iterator().asSequence().toList() })
+               assertThat(response.headers.map { it.name to it.iterator().asSequence().toList() }, contains(
+                               "Content-Disposition" to listOf("attachment; filename=temp-id.test")
+               ))
        }
 
 }