From c28013c8a4bcb9776a1e2d82ffd6c4c8297ffb62 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 9 May 2019 19:26:55 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Copy=20session-handling=20c?= =?utf8?q?ode=20to=20FreenetRequest?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 2 +- .../pterodactylus/sone/web/page/PageToadlet.java | 10 +++++-- .../pterodactylus/sone/web/page/FreenetRequest.kt | 16 +++++++++-- .../sone/web/page/PageToadletFactory.kt | 5 ++-- .../net/pterodactylus/sone/web/page/SoneRequest.kt | 6 ++-- .../sone/web/pages/SoneTemplatePage.kt | 4 +-- .../pterodactylus/sone/main/DebugLoadersTest.java | 4 ++- .../sone/main/DefaultLoadersTest.java | 4 ++- .../sone/web/page/FreenetRequestTest.kt | 32 ++++++++++++++++++++-- .../sone/web/page/PageToadletFactoryTest.kt | 4 ++- .../pterodactylus/sone/web/page/SoneRequestTest.kt | 7 +++-- .../pterodactylus/sone/web/pages/WebPageTest.kt | 5 +++- 12 files changed, 78 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1d9087f..a0e3033 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -638,7 +638,7 @@ public class WebInterface implements SessionProvider { Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html"); Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml"); - PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/"); + PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), getSessionManager(), "/Sone/"); pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this, postVisibilityFilter), "Index")); pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New")); diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java index 6209d32..2b22377 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java +++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java @@ -30,6 +30,7 @@ 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; @@ -42,6 +43,8 @@ import freenet.support.api.HTTPRequest; */ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet { + private final SessionManager sessionManager; + /** The name of the menu item. */ private final String menuName; @@ -64,8 +67,9 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * Prefix that is prepended to all {@link Page#getPath()} return * values */ - protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page page, String pathPrefix) { + 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; @@ -103,7 +107,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * 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, NodeL10n.getBase())); + handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager)); } /** @@ -121,7 +125,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * 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, NodeL10n.getBase())); + handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager)); } /** diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt index 03985a8..69843f7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt @@ -18,13 +18,25 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.clients.http.SessionManager.* import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.util.web.* import java.net.* +import java.util.UUID.* open class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext, - val l10n: BaseL10n -) : Request(uri, method) + val l10n: BaseL10n, + val sessionManager: SessionManager +) : Request(uri, method) { + + val session: Session + get() = + sessionManager.useSession(toadletContext) + ?: sessionManager.createSession(randomUUID().toString(), toadletContext) + + val existingSession: Session? get() = sessionManager.useSession(toadletContext) + +} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt index 0d21212..7a552db 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/PageToadletFactory.kt @@ -18,13 +18,14 @@ package net.pterodactylus.sone.web.page import freenet.client.* +import freenet.clients.http.* import net.pterodactylus.util.web.* -class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClient, private val pathPrefix: String) { +class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClient, private val sessionManager: SessionManager, private val pathPrefix: String) { @JvmOverloads fun createPageToadlet(page: Page, menuName: String? = null) = - PageToadlet(highLevelSimpleClient, menuName ?: page.menuName, page, pathPrefix) + PageToadlet(highLevelSimpleClient, sessionManager, menuName ?: page.menuName, page, pathPrefix) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt index 76b4843..d3eed37 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt @@ -8,9 +8,9 @@ import net.pterodactylus.sone.web.* import net.pterodactylus.util.web.* import java.net.* -class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n, +class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n, sessionManager: SessionManager, val core: Core, val webInterface: WebInterface -) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n) +) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) -fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface) +fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index abc58f5..ac1e15f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -26,7 +26,7 @@ open class SoneTemplatePage @JvmOverloads constructor( ) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") { private val core = webInterface.core - protected val sessionProvider: SessionProvider = webInterface + private val sessionProvider: SessionProvider = webInterface protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = sessionProvider.getCurrentSone(toadletContext, createSession) @@ -92,7 +92,7 @@ open class SoneTemplatePage @JvmOverloads constructor( private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") override fun isEnabled(toadletContext: ToadletContext) = - isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, core, webInterface)) + isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface)) open fun isEnabled(soneRequest: SoneRequest) = when { requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false diff --git a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java index 6eade58..7a84978 100644 --- a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java @@ -21,6 +21,7 @@ import net.pterodactylus.util.web.Method; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; +import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContext; import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; @@ -70,7 +71,8 @@ public class DebugLoadersTest { Method method = Method.GET; HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n); + SessionManager sessionManager = mock(SessionManager.class); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); page.handleRequest(request, response); diff --git a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java index d5e5522..dc51479 100644 --- a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java @@ -21,6 +21,7 @@ import net.pterodactylus.util.web.Method; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; +import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContext; import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; @@ -51,7 +52,8 @@ public class DefaultLoadersTest { Method method = Method.GET; HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n); + SessionManager sessionManager = mock(SessionManager.class); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); staticPage.handleRequest(request, response); diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt index 391991c..e05ad45 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.clients.http.SessionManager.* import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.test.* @@ -8,8 +9,8 @@ import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* +import org.mockito.* import org.mockito.Mockito.* -import org.mockito.Mockito.mock import java.net.* class FreenetRequestTest { @@ -19,7 +20,8 @@ class FreenetRequestTest { private val httpRequest = mock(HTTPRequest::class.java) private val toadletContext = mock(ToadletContext::class.java) private val l10n = mock() - private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n) + private val sessionManager = mock() + private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) @Test fun `uri is retained correctly`() { @@ -46,4 +48,30 @@ class FreenetRequestTest { assertThat(request.l10n, equalTo(l10n)) } + @Test + fun `null is returned if no session exists`() { + assertThat(request.existingSession, nullValue()) + } + + @Test + fun `existing session can be retrieved`() { + val session = mock() + whenever(sessionManager.useSession(toadletContext)).thenReturn(session) + assertThat(request.existingSession, sameInstance(session)) + } + + @Test + fun `existing session is returned if it exists`() { + val session = mock() + whenever(sessionManager.useSession(toadletContext)).thenReturn(session) + assertThat(request.session, sameInstance(session)) + } + + @Test + fun `new session is returned if none exists`() { + val session = mock() + whenever(sessionManager.createSession(anyString(), ArgumentMatchers.eq(toadletContext))).thenReturn(session) + assertThat(request.session, sameInstance(session)) + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt index bf69e91..c9ecafe 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/PageToadletFactoryTest.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.page import freenet.client.* +import freenet.clients.http.* import net.pterodactylus.sone.test.* import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* @@ -8,11 +9,12 @@ import org.hamcrest.Matchers.* import org.junit.* private val highLevelSimpleClient = mock() +private val sessionManager = mock() private const val pathPrefix = "/some/prefix/" class PageToadletFactoryTest { - private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, pathPrefix) + private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, sessionManager, pathPrefix) @Test fun `page toadlet without menu name is created without menu name`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt index 849d4ed..50c6ce7 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt @@ -20,9 +20,10 @@ class SoneRequestTest { private val httpRequest = Mockito.mock(HTTPRequest::class.java) private val toadletContext = Mockito.mock(ToadletContext::class.java) private val l10n = mock() + private val sessionManager = mock() private val core = mock() private val webInterface = mock() - private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface) + private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface) @Test fun `freenet request properties are retained correctly`() { @@ -31,6 +32,7 @@ class SoneRequestTest { assertThat(soneRequest.httpRequest, equalTo(httpRequest)) assertThat(soneRequest.toadletContext, equalTo(toadletContext)) assertThat(soneRequest.l10n, equalTo(l10n)) + assertThat(soneRequest.sessionManager, equalTo(sessionManager)) } @Test @@ -45,13 +47,14 @@ class SoneRequestTest { @Test fun `freenet request is wrapped correctly`() { - val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n) + val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager) val wrappedSoneRequest = freenetRequest.toSoneRequest(core, webInterface) assertThat(wrappedSoneRequest.uri, equalTo(uri)) assertThat(wrappedSoneRequest.method, equalTo(method)) assertThat(wrappedSoneRequest.httpRequest, equalTo(httpRequest)) assertThat(wrappedSoneRequest.toadletContext, equalTo(toadletContext)) assertThat(wrappedSoneRequest.l10n, equalTo(l10n)) + assertThat(wrappedSoneRequest.sessionManager, equalTo(sessionManager)) assertThat(wrappedSoneRequest.core, sameInstance(core)) assertThat(wrappedSoneRequest.webInterface, sameInstance(webInterface)) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt index e3d3410..d48f5fd 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt @@ -1,7 +1,7 @@ package net.pterodactylus.sone.web.pages import com.google.common.eventbus.EventBus -import freenet.clients.http.ToadletContext +import freenet.clients.http.* import freenet.support.SimpleReadOnlyArrayBucket import freenet.support.api.HTTPRequest import freenet.support.api.HTTPUploadedFile @@ -51,12 +51,14 @@ open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePag val eventBus = mock() val preferences = Preferences(eventBus) val l10n = webInterface.l10n!! + val sessionManager = mock() val page by lazy { pageSupplier(template, webInterface) } val httpRequest = mock() val freenetRequest = mock() init { whenever(freenetRequest.l10n).thenReturn(l10n) + whenever(freenetRequest.sessionManager).thenReturn(sessionManager) whenever(freenetRequest.uri).thenReturn(mock()) } val soneRequest by lazy { freenetRequest.toSoneRequest(core, webInterface) } @@ -107,6 +109,7 @@ open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePag } private fun setupWebInterface() { + whenever(webInterface.sessionManager).thenReturn(sessionManager) whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone) whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone) whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone) -- 2.7.4