From: David ‘Bombe’ Roden Date: Sat, 8 Apr 2017 14:23:59 +0000 (+0200) Subject: Add session provider interface X-Git-Tag: 0.9.7^2~259 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fdc047dd37e982776d55d827ca405868efe32c5a;p=Sone.git Add session provider interface --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index ee309df..946e6d6 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -223,7 +223,7 @@ import com.google.inject.Inject; * * @author David ‘Bombe’ Roden */ -public class WebInterface { +public class WebInterface implements SessionProvider { /** The logger. */ private static final Logger logger = getLogger(WebInterface.class.getName()); @@ -469,6 +469,12 @@ public class WebInterface { return getCore().getLocalSone(soneId); } + @Override + @Nullable + public Sone getCurrentSone(@Nonnull ToadletContext toadletContext, boolean createSession) { + return createSession ? getCurrentSoneCreatingSession(toadletContext) : getCurrentSoneWithoutCreatingSession(toadletContext); + } + /** * Sets the currently logged in Sone. * @@ -477,7 +483,8 @@ public class WebInterface { * @param sone * The Sone to set as currently logged in */ - public void setCurrentSone(ToadletContext toadletContext, Sone sone) { + @Override + public void setCurrentSone(@Nonnull ToadletContext toadletContext, @Nullable Sone sone) { Session session = getOrCreateCurrentSession(toadletContext); if (sone == null) { session.removeAttribute("Sone.CurrentSone"); diff --git a/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java index 2e53761..c8ee0d0 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java @@ -102,7 +102,7 @@ public class ImageBrowserPage extends SoneTemplatePage { templateContext.set("albums", albumPagination.getItems()); return; } - Sone sone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone sone = getCurrentSone(request.getToadletContext(), false); templateContext.set("soneRequested", true); templateContext.set("sone", sone); } diff --git a/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java index 5aef1f9..8864257 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java @@ -73,7 +73,7 @@ public class KnownSonesPage extends SoneTemplatePage { templateContext.set("sort", sortField); templateContext.set("order", sortOrder); templateContext.set("filter", filter); - final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + final Sone currentSone = getCurrentSone(request.getToadletContext(), false); Collection knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER); if ((currentSone != null) && "followed".equals(filter)) { knownSones = Collections2.filter(knownSones, new Predicate() { diff --git a/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java b/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java index 1249086..1f2261c 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java @@ -61,7 +61,7 @@ public class LogoutPage extends SoneTemplatePage { if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) { return false; } - return (getCurrentSoneWithoutCreatingSession(toadletContext) != null) && (webInterface.getCore().getLocalSones().size() != 1); + return (getCurrentSone(toadletContext, false) != null) && (webInterface.getCore().getLocalSones().size() != 1); } } diff --git a/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java b/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java index 411f40f..b5c6e11 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java @@ -65,8 +65,8 @@ public class NewPage extends SoneTemplatePage { @Override protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { /* collect new elements from notifications. */ - Set posts = new HashSet(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))); - for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) { + Set posts = new HashSet(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false))); + for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) { posts.add(reply.getPost().get()); } diff --git a/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java b/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java index 93ef9e1..b2ce5d1 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java @@ -56,7 +56,7 @@ public class RescuePage extends SoneTemplatePage { */ @Override protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + 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))) { diff --git a/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java index 46c8318..adc4a3a 100644 --- a/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java @@ -27,9 +27,11 @@ import java.util.List; import java.util.Map; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.main.SonePlugin; +import net.pterodactylus.sone.web.SessionProvider; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.sone.web.page.FreenetTemplatePage; @@ -37,7 +39,6 @@ import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import freenet.clients.http.SessionManager.Session; import freenet.clients.http.ToadletContext; import freenet.support.api.HTTPRequest; @@ -53,6 +54,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { /** The Sone core. */ protected final WebInterface webInterface; + protected final SessionProvider sessionProvider; /** The page title l10n key. */ private final String pageTitleKey; @@ -111,6 +113,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html"); this.pageTitleKey = pageTitleKey; this.webInterface = webInterface; + this.sessionProvider = webInterface; this.requireLogin = requireLogin; } @@ -118,32 +121,18 @@ public class SoneTemplatePage extends FreenetTemplatePage { // PROTECTED METHODS // - /** - * Returns the currently logged in Sone. - * - * @param toadletContext - * The toadlet context - * @return The currently logged in Sone, or {@code null} if no Sone is - * currently logged in - */ - protected Sone getCurrentSone(ToadletContext toadletContext) { - return webInterface.getCurrentSoneCreatingSession(toadletContext); + @Nullable + protected Sone getCurrentSone(@Nonnull ToadletContext toadletContext) { + return getCurrentSone(toadletContext, true); } - protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) { - return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext); + @Nullable + protected Sone getCurrentSone(@Nonnull ToadletContext toadletContext, boolean createSession) { + return sessionProvider.getCurrentSone(toadletContext, createSession); } - /** - * Sets the currently logged in Sone. - * - * @param toadletContext - * The toadlet context - * @param sone - * The Sone to set as currently logged in - */ - protected void setCurrentSone(ToadletContext toadletContext, Sone sone) { - webInterface.setCurrentSone(toadletContext, sone); + protected void setCurrentSone(@Nonnull ToadletContext toadletContext, @Nullable Sone sone) { + sessionProvider.setCurrentSone(toadletContext, sone); } // @@ -201,7 +190,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { @Override protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext(), false); templateContext.set("preferences", webInterface.getCore().getPreferences()); templateContext.set("currentSone", currentSone); templateContext.set("localSones", webInterface.getCore().getLocalSones()); @@ -226,7 +215,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { */ @Override protected String getRedirectTarget(FreenetRequest request) { - if (requiresLogin() && (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null)) { + if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) { HTTPRequest httpRequest = request.getHttpRequest(); String originalUrl = httpRequest.getPath(); if (httpRequest.hasParameters()) { @@ -273,7 +262,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { return false; } if (requiresLogin()) { - return getCurrentSoneWithoutCreatingSession(toadletContext) != null; + return getCurrentSone(toadletContext, false) != null; } return true; } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt b/src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt new file mode 100644 index 0000000..463ddaa --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt @@ -0,0 +1,14 @@ +package net.pterodactylus.sone.web + +import freenet.clients.http.ToadletContext +import net.pterodactylus.sone.data.Sone + +/** + * Provides access to the currently logged-in [Sone]. + */ +interface SessionProvider { + + fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true): Sone? + fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) + +} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt index e0dc4be..890ac74 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt @@ -14,7 +14,7 @@ class DeleteProfileFieldPage(template: Template, webInterface: WebInterface): SoneTemplatePage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface, true) { override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { - val currentSone = getCurrentSone(request.toadletContext) + val currentSone = getCurrentSone(request.toadletContext)!! val field = currentSone.profile.getFieldById(request.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html") templateContext["field"] = field if (request.isPOST) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt index 695816f..0d839db 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt @@ -33,7 +33,7 @@ class LoginPage(template: Template, webInterface: WebInterface): override fun isEnabled(toadletContext: ToadletContext) = when { webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false - else -> getCurrentSoneWithoutCreatingSession(toadletContext) == null + else -> getCurrentSone(toadletContext, false) == null } } diff --git a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java index 3c6b1d4..532c09a 100644 --- a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java @@ -274,6 +274,7 @@ public abstract class WebPageTest { @Before public final void setupWebInterface() { + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone); when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone); when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone); when(webInterface.getNotification(anyString())).thenReturn(Optional.absent()); @@ -290,6 +291,7 @@ public abstract class WebPageTest { } protected void unsetCurrentSone() { + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null); when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null); when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null); } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt index 42d6676..2eb77e2 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt @@ -4,7 +4,6 @@ import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.main.SonePlugin import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.pages.SoneTemplatePage import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.TemplateContext @@ -36,8 +35,8 @@ class SoneTemplatePageTest : WebPageTest() { @Test fun `retrieving current sone without creation is forwarded to web interface`() { mock().let { - whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(it) - assertThat(page.getCurrentSoneWithoutCreatingSession(toadletContext), equalTo(it)) + whenever(webInterface.getCurrentSone(toadletContext, false)).thenReturn(it) + assertThat(page.getCurrentSone(toadletContext, false), equalTo(it)) } }