Replace rescue page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 May 2017 17:32:19 +0000 (19:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 May 2017 17:32:19 +0000 (19:32 +0200)
src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java b/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
deleted file mode 100644 (file)
index b2ce5d1..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Sone - RescuePage.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 static net.pterodactylus.sone.utils.NumberParsers.parseLong;
-
-import net.pterodactylus.sone.core.SoneRescuer;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.web.Method;
-
-/**
- * Page that lets the user control the rescue mode for a Sone.
- *
- * @see SoneRescuer
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class RescuePage extends SoneTemplatePage {
-
-       /**
-        * Creates a new rescue page.
-        *
-        * @param template
-        *            The template to render
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public RescuePage(Template template, WebInterface webInterface) {
-               super("rescue.html", template, "Page.Rescue.Title", webInterface, true);
-       }
-
-       //
-       // SONETEMPLATEPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
-               SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
-               if (request.getMethod() == Method.POST) {
-                       if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
-                               long edition = parseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L);
-                               if (edition > -1) {
-                                       soneRescuer.setEdition(edition);
-                               }
-                               soneRescuer.startNextFetch();
-                       }
-                       throw new RedirectException("rescue.html");
-               }
-               templateContext.set("soneRescuer", soneRescuer);
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt
new file mode 100644 (file)
index 0000000..b882ee6
--- /dev/null
@@ -0,0 +1,32 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.utils.isPOST
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+
+/**
+ * Page that lets the user control the rescue mode for a Sone.
+ */
+class RescuePage(template: Template, webInterface: WebInterface):
+               SoneTemplatePage("rescue.html", template, "Page.Rescue.Title", webInterface, true) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+               val soneRescuer = webInterface.core.getSoneRescuer(getCurrentSone(request.toadletContext)!!)
+               templateContext["soneRescuer"] = soneRescuer
+               if (request.isPOST) {
+                       request.parameters["edition", 9]?.toIntOrNull()?.also {
+                               if (it > -1) {
+                                       soneRescuer.setEdition(it.toLong())
+                               }
+                       }
+                       if (request.parameters["fetch", 8] == "true") {
+                               soneRescuer.startNextFetch()
+                       }
+                       throw RedirectException("rescue.html")
+               }
+       }
+
+}
index 300786a..1f62a92 100644 (file)
@@ -15,7 +15,7 @@ import org.mockito.Mockito.verify
 /**
  * Unit test for [RescuePage].
  */
-class RescuePageTest : WebPageTest() {
+class RescuePageTest: WebPageTest() {
 
        private val page = RescuePage(template, webInterface)