From 9235105b5767441ce15d7ede26d6ef963a38e51d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 7 Apr 2020 10:11:25 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20page=20toadlet=20with?= =?utf8?q?=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/web/page/PageToadlet.java | 187 --------------------- .../net/pterodactylus/sone/web/page/PageToadlet.kt | 81 +++++++++ 2 files changed, 81 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java create mode 100644 src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java deleted file mode 100644 index 45d6fff..0000000 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Sone - PageToadlet.java - Copyright © 2010–2020 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.page; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URI; - -import net.pterodactylus.sone.utils.AutoCloseableBucket; -import net.pterodactylus.util.web.Header; -import net.pterodactylus.util.web.Method; -import net.pterodactylus.util.web.Page; -import net.pterodactylus.util.web.Response; - -import freenet.client.HighLevelSimpleClient; -import freenet.clients.http.LinkEnabledCallback; -import freenet.clients.http.LinkFilterExceptedToadlet; -import freenet.clients.http.SessionManager; -import freenet.clients.http.Toadlet; -import freenet.clients.http.ToadletContext; -import freenet.clients.http.ToadletContextClosedException; -import freenet.support.MultiValueTable; -import freenet.support.api.HTTPRequest; - -/** - * {@link Toadlet} implementation that is wrapped around a {@link Page}. - */ -public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet { - - private final SessionManager sessionManager; - - /** The name of the menu item. */ - private final String menuName; - - /** The page that handles processing. */ - private final Page page; - - /** The path prefix for the page. */ - private final String pathPrefix; - - /** - * Creates a new toadlet that hands off processing to a {@link Page}. - * - * @param highLevelSimpleClient - * The high-level simple client - * @param menuName - * The name of the menu item - * @param page - * The page to handle processing - * @param pathPrefix - * Prefix that is prepended to all {@link Page#getPath()} return - * values - */ - protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, SessionManager sessionManager, String menuName, Page page, String pathPrefix) { - super(highLevelSimpleClient); - this.sessionManager = sessionManager; - this.menuName = menuName; - this.page = page; - this.pathPrefix = pathPrefix; - } - - /** - * Returns the name to display in the menu. - * - * @return The name in the menu - */ - public String getMenuName() { - return menuName; - } - - /** - * {@inheritDoc} - */ - @Override - public String path() { - return pathPrefix + page.getPath(); - } - - /** - * Handles a HTTP GET request. - * - * @param uri - * The URI of the request - * @param httpRequest - * The HTTP request - * @param toadletContext - * The toadlet context - * @throws IOException - * if an I/O error occurs - * @throws ToadletContextClosedException - * if the toadlet context is closed - */ - public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, sessionManager)); - } - - /** - * Handles a HTTP POST request. - * - * @param uri - * The URI of the request - * @param httpRequest - * The HTTP request - * @param toadletContext - * The toadlet context - * @throws IOException - * if an I/O error occurs - * @throws ToadletContextClosedException - * if the toadlet context is closed - */ - public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, sessionManager)); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return getClass().getName() + "[path=" + path() + ",page=" + page + "]"; - } - - /** - * Handles a HTTP request. - * - * @param pageRequest - * The request to handle - * @throws IOException - * if an I/O error occurs - * @throws ToadletContextClosedException - * if the toadlet context is closed - */ - private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException { - try (AutoCloseableBucket pageBucket = new AutoCloseableBucket(pageRequest.getToadletContext().getBucketFactory().makeBucket(-1)); - OutputStream pageBucketOutputStream = pageBucket.getBucket().getOutputStream()) { - Response pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream)); - MultiValueTable headers = new MultiValueTable<>(); - if (pageResponse.getHeaders() != null) { - for (Header header : pageResponse.getHeaders()) { - for (String value : header) { - headers.put(header.getName(), value); - } - } - } - writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket.getBucket()); - } - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isEnabled(ToadletContext toadletContext) { - if (page instanceof LinkEnabledCallback) { - return ((LinkEnabledCallback) page).isEnabled(toadletContext); - } - return true; - } - - // - // LINKFILTEREXCEPTEDTOADLET METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public boolean isLinkExcepted(URI link) { - return (page instanceof FreenetPage) && ((FreenetPage) page).isLinkExcepted(link); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt new file mode 100644 index 0000000..a1cddfe --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt @@ -0,0 +1,81 @@ +/* + * Sone - PageToadlet.kt - Copyright © 2010–2020 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.page + +import freenet.client.HighLevelSimpleClient +import freenet.clients.http.LinkEnabledCallback +import freenet.clients.http.LinkFilterExceptedToadlet +import freenet.clients.http.SessionManager +import freenet.clients.http.Toadlet +import freenet.clients.http.ToadletContext +import freenet.support.MultiValueTable +import freenet.support.api.HTTPRequest +import net.pterodactylus.sone.utils.use +import net.pterodactylus.util.web.Method +import net.pterodactylus.util.web.Page +import net.pterodactylus.util.web.Response +import java.net.URI + +/** + * [Toadlet] implementation that is wrapped around a [Page]. + */ +class PageToadlet( + highLevelSimpleClient: HighLevelSimpleClient, + private val sessionManager: SessionManager, + val menuName: String?, + private val page: Page, + private val pathPrefix: String +) : Toadlet(highLevelSimpleClient), LinkEnabledCallback, LinkFilterExceptedToadlet { + + override fun path() = pathPrefix + page.path + + override fun handleMethodGET(uri: URI, httpRequest: HTTPRequest, toadletContext: ToadletContext) = + handleRequest(FreenetRequest(uri, Method.GET, httpRequest, toadletContext, sessionManager)) + + fun handleMethodPOST(uri: URI?, httpRequest: HTTPRequest?, toadletContext: ToadletContext?) = + handleRequest(FreenetRequest(uri!!, Method.POST, httpRequest!!, toadletContext!!, sessionManager)) + + private fun handleRequest(pageRequest: FreenetRequest) { + pageRequest.toadletContext.bucketFactory.makeBucket(-1).use { pageBucket -> + pageBucket.outputStream.use { pageBucketOutputStream -> + val pageResponse = page.handleRequest(pageRequest, Response(pageBucketOutputStream)) + // according to the javadoc, headers is allowed to return null but that’s stupid and it doesn’t do that. + val headers = pageResponse.headers.fold(MultiValueTable()) { headers, header -> + headers.apply { + header.forEach { put(header.name, it) } + } + } + with(pageResponse) { + writeReply(pageRequest.toadletContext, statusCode, contentType, statusText, headers, pageBucket) + } + } + } + } + + override fun isEnabled(toadletContext: ToadletContext) = + if (page is LinkEnabledCallback) { + page.isEnabled(toadletContext) + } else + true + + override fun isLinkExcepted(link: URI) = + page is FreenetPage && page.isLinkExcepted(link) + + override fun toString() = "${javaClass.name}[path=${path()},page=$page]" + +} -- 2.7.4