From 2000b0561c6a7e4b1fbc6e452b89c9504b3b5d33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 26 May 2017 19:32:19 +0200 Subject: [PATCH] Replace rescue page with Kotlin version --- .../pterodactylus/sone/web/pages/RescuePage.java | 74 ---------------------- .../net/pterodactylus/sone/web/pages/RescuePage.kt | 32 ++++++++++ .../pterodactylus/sone/web/pages/RescuePageTest.kt | 2 +- 3 files changed, 33 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.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 index b2ce5d1..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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 index 0000000..b882ee6 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/RescuePage.kt @@ -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") + } + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt index 300786a..1f62a92 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/RescuePageTest.kt @@ -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) -- 2.7.4