From fe7976d8e9806d6ad27581e71707a790bc46498e 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:43:51 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=9A=20Move=20Kotlin=20files=20to=20corr?= =?utf8?q?ect=20source=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/page/PageToadlet.kt | 81 -------------------- .../net/pterodactylus/sone/web/page/PageToadlet.kt | 81 ++++++++++++++++++++ .../pterodactylus/sone/core/SoneUriCreatorTest.kt | 88 ---------------------- .../pterodactylus/sone/core/SoneUriCreatorTest.kt | 88 ++++++++++++++++++++++ 4 files changed, 169 insertions(+), 169 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/page/PageToadlet.kt delete mode 100644 src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/core/SoneUriCreatorTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt deleted file mode 100644 index a1cddfe..0000000 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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]" - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadlet.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadlet.kt new file mode 100644 index 0000000..a1cddfe --- /dev/null +++ b/src/main/kotlin/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]" + +} diff --git a/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt b/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt deleted file mode 100644 index ae8f25b..0000000 --- a/src/test/java/net/pterodactylus/sone/core/SoneUriCreatorTest.kt +++ /dev/null @@ -1,88 +0,0 @@ -package net.pterodactylus.sone.core - -import com.google.inject.Guice -import net.pterodactylus.sone.data.impl.IdOnlySone -import net.pterodactylus.sone.freenet.wot.DefaultIdentity -import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity -import net.pterodactylus.sone.test.createInsertUri -import net.pterodactylus.sone.test.createRequestUri -import net.pterodactylus.sone.test.getInstance -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.emptyArray -import org.hamcrest.Matchers.equalTo -import org.hamcrest.Matchers.notNullValue -import org.hamcrest.Matchers.nullValue -import kotlin.test.Test - -/** - * Unit test for [SoneUriCreator]. - */ -class SoneUriCreatorTest { - - private val soneUriCreator = SoneUriCreator() - - private val requestUri = soneUriCreator.getRequestUri(sone) - private val insertUri = soneUriCreator.getInsertUri(sone) - - @Test - fun `generated request URI is a USK`() { - assertThat(requestUri.keyType, equalTo("USK")) - } - - @Test - fun `generated request URI has correct doc name`() { - assertThat(requestUri.docName, equalTo("Sone")) - } - - @Test - fun `generated request URI has no meta strings`() { - assertThat(requestUri.allMetaStrings, emptyArray()) - } - - @Test - fun `generated request URI has correct edition`() { - assertThat(requestUri.suggestedEdition, equalTo(123L)) - } - - @Test - fun `insert URI is null if sone’s identity is not an own identity`() { - val remoteSone = object : IdOnlySone("id") { - override fun getIdentity() = DefaultIdentity("id", "name", createRequestUri.toString()) - } - assertThat(soneUriCreator.getInsertUri(remoteSone), nullValue()) - } - - @Test - fun `generated insert URI is a USK`() { - assertThat(insertUri!!.keyType, equalTo("USK")) - } - - @Test - fun `generated insert URI has correct doc name`() { - assertThat(insertUri!!.docName, equalTo("Sone")) - } - - @Test - fun `generated insert URI has no meta strings`() { - assertThat(insertUri!!.allMetaStrings, emptyArray()) - } - - @Test - fun `generated insert URI has correct edition`() { - assertThat(insertUri!!.suggestedEdition, equalTo(123L)) - } - - @Test - fun `creator can be created by guice`() { - val injector = Guice.createInjector() - assertThat(injector.getInstance(), notNullValue()) - } - -} - -private val sone = object : IdOnlySone("id") { - override fun getIdentity() = - DefaultOwnIdentity("id", "name", createRequestUri.toString(), createInsertUri.toString()) - - override fun getLatestEdition() = 123L -} diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneUriCreatorTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneUriCreatorTest.kt new file mode 100644 index 0000000..ae8f25b --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/SoneUriCreatorTest.kt @@ -0,0 +1,88 @@ +package net.pterodactylus.sone.core + +import com.google.inject.Guice +import net.pterodactylus.sone.data.impl.IdOnlySone +import net.pterodactylus.sone.freenet.wot.DefaultIdentity +import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity +import net.pterodactylus.sone.test.createInsertUri +import net.pterodactylus.sone.test.createRequestUri +import net.pterodactylus.sone.test.getInstance +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.emptyArray +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.notNullValue +import org.hamcrest.Matchers.nullValue +import kotlin.test.Test + +/** + * Unit test for [SoneUriCreator]. + */ +class SoneUriCreatorTest { + + private val soneUriCreator = SoneUriCreator() + + private val requestUri = soneUriCreator.getRequestUri(sone) + private val insertUri = soneUriCreator.getInsertUri(sone) + + @Test + fun `generated request URI is a USK`() { + assertThat(requestUri.keyType, equalTo("USK")) + } + + @Test + fun `generated request URI has correct doc name`() { + assertThat(requestUri.docName, equalTo("Sone")) + } + + @Test + fun `generated request URI has no meta strings`() { + assertThat(requestUri.allMetaStrings, emptyArray()) + } + + @Test + fun `generated request URI has correct edition`() { + assertThat(requestUri.suggestedEdition, equalTo(123L)) + } + + @Test + fun `insert URI is null if sone’s identity is not an own identity`() { + val remoteSone = object : IdOnlySone("id") { + override fun getIdentity() = DefaultIdentity("id", "name", createRequestUri.toString()) + } + assertThat(soneUriCreator.getInsertUri(remoteSone), nullValue()) + } + + @Test + fun `generated insert URI is a USK`() { + assertThat(insertUri!!.keyType, equalTo("USK")) + } + + @Test + fun `generated insert URI has correct doc name`() { + assertThat(insertUri!!.docName, equalTo("Sone")) + } + + @Test + fun `generated insert URI has no meta strings`() { + assertThat(insertUri!!.allMetaStrings, emptyArray()) + } + + @Test + fun `generated insert URI has correct edition`() { + assertThat(insertUri!!.suggestedEdition, equalTo(123L)) + } + + @Test + fun `creator can be created by guice`() { + val injector = Guice.createInjector() + assertThat(injector.getInstance(), notNullValue()) + } + +} + +private val sone = object : IdOnlySone("id") { + override fun getIdentity() = + DefaultOwnIdentity("id", "name", createRequestUri.toString(), createInsertUri.toString()) + + override fun getLatestEdition() = 123L +} -- 2.7.4