Add session provider interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 8 Apr 2017 14:23:59 +0000 (16:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 8 Apr 2017 14:23:59 +0000 (16:23 +0200)
12 files changed:
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt

index ee309df..946e6d6 100644 (file)
@@ -223,7 +223,7 @@ import com.google.inject.Inject;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-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");
index 2e53761..c8ee0d0 100644 (file)
@@ -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);
        }
index 5aef1f9..8864257 100644 (file)
@@ -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<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
                if ((currentSone != null) && "followed".equals(filter)) {
                        knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
index 1249086..1f2261c 100644 (file)
@@ -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);
        }
 
 }
index 411f40f..b5c6e11 100644 (file)
@@ -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<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
-               for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) {
+               Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false)));
+               for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) {
                        posts.add(reply.getPost().get());
                }
 
index 93ef9e1..b2ce5d1 100644 (file)
@@ -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))) {
index 46c8318..adc4a3a 100644 (file)
@@ -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 (file)
index 0000000..463ddaa
--- /dev/null
@@ -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?)
+
+}
index e0dc4be..890ac74 100644 (file)
@@ -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) {
index 695816f..0d839db 100644 (file)
@@ -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
        }
 
 }
index 3c6b1d4..532c09a 100644 (file)
@@ -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.<Notification>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);
        }
index 42d6676..2eb77e2 100644 (file)
@@ -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<Sone>().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))
                }
        }