Rename session-related Methode in web interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 6 Feb 2017 06:31:06 +0000 (07:31 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 6 Feb 2017 06:31:06 +0000 (07:31 +0100)
18 files changed:
src/main/java/net/pterodactylus/sone/web/CreateSonePage.java
src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java
src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java
src/main/java/net/pterodactylus/sone/web/LoginPage.java
src/main/java/net/pterodactylus/sone/web/LogoutPage.java
src/main/java/net/pterodactylus/sone/web/NewPage.java
src/main/java/net/pterodactylus/sone/web/OptionsPage.java
src/main/java/net/pterodactylus/sone/web/RescuePage.java
src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java
src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/SoneTemplatePageTest.kt

index a74204c..2484e3a 100644 (file)
@@ -136,7 +136,7 @@ public class CreateSonePage extends SoneTemplatePage {
                if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
                        return false;
                }
-               return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1);
+               return (getCurrentSoneWithoutCreatingSession(toadletContext) == null) || (webInterface.getCore().getLocalSones().size() == 1);
        }
 
 }
index acc6354..7a222d2 100644 (file)
@@ -101,7 +101,7 @@ public class ImageBrowserPage extends SoneTemplatePage {
                        templateContext.set("albums", albumPagination.getItems());
                        return;
                }
-               Sone sone = getCurrentSone(request.getToadletContext(), false);
+               Sone sone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                templateContext.set("soneRequested", true);
                templateContext.set("sone", sone);
        }
index da93a02..c74f4b9 100644 (file)
@@ -72,7 +72,7 @@ public class KnownSonesPage extends SoneTemplatePage {
                templateContext.set("sort", sortField);
                templateContext.set("order", sortOrder);
                templateContext.set("filter", filter);
-               final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                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 f9bd371..65ab0cf 100644 (file)
@@ -89,7 +89,7 @@ public class LoginPage extends SoneTemplatePage {
         */
        @Override
        protected String getRedirectTarget(FreenetRequest request) {
-               if (getCurrentSone(request.getToadletContext(), false) != null) {
+               if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) != null) {
                        return "index.html";
                }
                return null;
@@ -107,7 +107,7 @@ public class LoginPage extends SoneTemplatePage {
                if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
                        return false;
                }
-               return getCurrentSone(toadletContext, false) == null;
+               return getCurrentSoneWithoutCreatingSession(toadletContext) == null;
        }
 
 }
index f7a254c..3ec62a3 100644 (file)
@@ -60,7 +60,7 @@ public class LogoutPage extends SoneTemplatePage {
                if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
                        return false;
                }
-               return (getCurrentSone(toadletContext, false) != null) && (webInterface.getCore().getLocalSones().size() != 1);
+               return (getCurrentSoneWithoutCreatingSession(toadletContext) != null) && (webInterface.getCore().getLocalSones().size() != 1);
        }
 
 }
index fc8bb56..9eab69a 100644 (file)
@@ -64,8 +64,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(getCurrentSone(request.getToadletContext(), false)));
-               for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) {
+               Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
+               for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) {
                        posts.add(reply.getPost().get());
                }
 
index 341bc0c..7cf7246 100644 (file)
@@ -60,7 +60,7 @@ public class OptionsPage extends SoneTemplatePage {
        @Override
        protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                Preferences preferences = webInterface.getCore().getPreferences();
-               Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = webInterface.getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                if (request.getMethod() == Method.POST) {
                        List<String> fieldErrors = new ArrayList<String>();
                        if (currentSone != null) {
index 78a2b9e..0f94563 100644 (file)
@@ -55,7 +55,7 @@ public class RescuePage extends SoneTemplatePage {
         */
        @Override
        protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
                if (request.getMethod() == Method.POST) {
                        if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
index 27d4bf7..2b16dba 100644 (file)
@@ -126,22 +126,11 @@ public class SoneTemplatePage extends FreenetTemplatePage {
         *         currently logged in
         */
        protected Sone getCurrentSone(ToadletContext toadletContext) {
-               return webInterface.getCurrentSone(toadletContext);
+               return webInterface.getCurrentSoneCreatingSession(toadletContext);
        }
 
-       /**
-        * Returns the currently logged in Sone.
-        *
-        * @param toadletContext
-        *            The toadlet context
-        * @param create
-        *            {@code true} to create a new session if no session exists,
-        *            {@code false} to not create a new session
-        * @return The currently logged in Sone, or {@code null} if no Sone is
-        *         currently logged in
-        */
-       protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
-               return webInterface.getCurrentSone(toadletContext, create);
+       protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
+               return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext);
        }
 
        /**
@@ -211,7 +200,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
        @Override
        protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                templateContext.set("core", webInterface.getCore());
                templateContext.set("currentSone", currentSone);
                templateContext.set("localSones", webInterface.getCore().getLocalSones());
@@ -236,7 +225,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
         */
        @Override
        protected String getRedirectTarget(FreenetRequest request) {
-               if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
+               if (requiresLogin() && (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null)) {
                        HTTPRequest httpRequest = request.getHttpRequest();
                        String originalUrl = httpRequest.getPath();
                        if (httpRequest.hasParameters()) {
@@ -283,7 +272,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
                        return false;
                }
                if (requiresLogin()) {
-                       return getCurrentSone(toadletContext, false) != null;
+                       return getCurrentSoneWithoutCreatingSession(toadletContext) != null;
                }
                return true;
        }
index 1ee7292..30613af 100644 (file)
@@ -375,68 +375,32 @@ public class WebInterface {
                return templateContextFactory;
        }
 
-       /**
-        * Returns the current session, creating a new session if there is no
-        * current session.
-        *
-        * @param toadletContenxt
-        *            The toadlet context
-        * @return The current session, or {@code null} if there is no current
-        *         session
-        */
-       private Session getCurrentSession(ToadletContext toadletContenxt) {
-               return getCurrentSession(toadletContenxt, true);
+       private Session getCurrentSessionWithoutCreation(ToadletContext toadletContenxt) {
+               return getSessionManager().useSession(toadletContenxt);
        }
 
-       /**
-        * Returns the current session, creating a new session if there is no
-        * current session and {@code create} is {@code true}.
-        *
-        * @param toadletContenxt
-        *            The toadlet context
-        * @param create
-        *            {@code true} to create a new session if there is no current
-        *            session, {@code false} otherwise
-        * @return The current session, or {@code null} if there is no current
-        *         session
-        */
-       private Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
-               Session session = getSessionManager().useSession(toadletContenxt);
-               if (create && (session == null)) {
+       private Session getOrCreateCurrentSession(ToadletContext toadletContenxt) {
+               Session session = getCurrentSessionWithoutCreation(toadletContenxt);
+               if (session == null) {
                        session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
                }
                return session;
        }
 
-       /**
-        * 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
-        */
-       public Sone getCurrentSone(ToadletContext toadletContext) {
-               return getCurrentSone(toadletContext, true);
+       public Sone getCurrentSoneCreatingSession(ToadletContext toadletContext) {
+               Collection<Sone> localSones = getCore().getLocalSones();
+               if (localSones.size() == 1) {
+                       return localSones.iterator().next();
+               }
+               return getCurrentSone(getOrCreateCurrentSession(toadletContext));
        }
 
-       /**
-        * Returns the currently logged in Sone.
-        *
-        * @param toadletContext
-        *            The toadlet context
-        * @param create
-        *            {@code true} to create a new session if no session exists,
-        *            {@code false} to not create a new session
-        * @return The currently logged in Sone, or {@code null} if no Sone is
-        *         currently logged in
-        */
-       public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
+       public Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
                Collection<Sone> localSones = getCore().getLocalSones();
                if (localSones.size() == 1) {
                        return localSones.iterator().next();
                }
-               return getCurrentSone(getCurrentSession(toadletContext, create));
+               return getCurrentSone(getCurrentSessionWithoutCreation(toadletContext));
        }
 
        /**
@@ -447,7 +411,7 @@ public class WebInterface {
         * @return The currently logged in Sone, or {@code null} if no Sone is
         *         currently logged in
         */
-       public Sone getCurrentSone(Session session) {
+       private Sone getCurrentSone(Session session) {
                if (session == null) {
                        return null;
                }
@@ -467,7 +431,7 @@ public class WebInterface {
         *            The Sone to set as currently logged in
         */
        public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
-               Session session = getCurrentSession(toadletContext);
+               Session session = getOrCreateCurrentSession(toadletContext);
                if (sone == null) {
                        session.removeAttribute("Sone.CurrentSone");
                } else {
index 26a1207..b43d837 100644 (file)
@@ -47,7 +47,7 @@ public class DistrustAjaxPage extends JsonPage {
         */
        @Override
        protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                if (currentSone == null) {
                        return createErrorJsonObject("auth-required");
                }
index 65d3943..59c6b2f 100644 (file)
@@ -79,7 +79,7 @@ public class GetNotificationsAjaxPage extends JsonPage {
         */
        @Override
        protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
                Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
                ArrayNode jsonNotifications = new ArrayNode(instance);
@@ -110,7 +110,7 @@ public class GetNotificationsAjaxPage extends JsonPage {
                        if (notification instanceof TemplateNotification) {
                                TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
                                templateContext.set("core", webInterface.getCore());
-                               templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
+                               templateContext.set("currentSone", webInterface.getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
                                templateContext.set("localSones", webInterface.getCore().getLocalSones());
                                templateContext.set("request", request);
                                templateContext.set("currentVersion", SonePlugin.getPluginVersion());
index 7a4c391..efb6716 100644 (file)
@@ -74,9 +74,9 @@ public class GetStatusAjaxPage extends JsonPage {
         */
        @Override
        protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                /* load Sones. always return the status of the current Sone. */
-               Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
+               Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
                String loadSoneIds = request.getHttpRequest().getParam("soneIds");
                if (loadSoneIds.length() > 0) {
                        String[] soneIds = loadSoneIds.split(",");
@@ -96,7 +96,7 @@ public class GetStatusAjaxPage extends JsonPage {
                List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
                Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
                /* load new posts. */
-               Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false));
+               Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
 
                ArrayNode jsonPosts = new ArrayNode(instance);
                for (Post post : newPosts) {
@@ -108,7 +108,7 @@ public class GetStatusAjaxPage extends JsonPage {
                        jsonPosts.add(jsonPost);
                }
                /* load new replies. */
-               Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false));
+               Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
 
                ArrayNode jsonReplies = new ArrayNode(instance);
                for (PostReply reply : newReplies) {
index 1050eee..eee28ec 100644 (file)
@@ -85,22 +85,11 @@ public abstract class JsonPage implements FreenetPage {
         *         currently logged in
         */
        protected Sone getCurrentSone(ToadletContext toadletContext) {
-               return webInterface.getCurrentSone(toadletContext);
+               return webInterface.getCurrentSoneCreatingSession(toadletContext);
        }
 
-       /**
-        * Returns the currently logged in Sone.
-        *
-        * @param toadletContext
-        *            The toadlet context
-        * @param create
-        *            {@code true} to create a new session if no session exists,
-        *            {@code false} to not create a new session
-        * @return The currently logged in Sone, or {@code null} if no Sone is
-        *         currently logged in
-        */
-       protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
-               return webInterface.getCurrentSone(toadletContext, create);
+       protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
+               return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext);
        }
 
        //
@@ -199,7 +188,7 @@ public abstract class JsonPage implements FreenetPage {
                        }
                }
                if (requiresLogin()) {
-                       if (getCurrentSone(request.getToadletContext(), false) == null) {
+                       if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null) {
                                return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
                        }
                }
index a186d46..586366e 100644 (file)
@@ -47,7 +47,7 @@ public class TrustAjaxPage extends JsonPage {
         */
        @Override
        protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                if (currentSone == null) {
                        return createErrorJsonObject("auth-required");
                }
index 644dfd6..bd79b98 100644 (file)
@@ -47,7 +47,7 @@ public class UntrustAjaxPage extends JsonPage {
         */
        @Override
        protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
                if (currentSone == null) {
                        return createErrorJsonObject("auth-required");
                }
index a1af8cc..8e77945 100644 (file)
@@ -265,8 +265,8 @@ public abstract class WebPageTest {
 
        @Before
        public final void setupWebInterface() {
-               when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
-               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());
                when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
        }
@@ -281,8 +281,8 @@ public abstract class WebPageTest {
        }
 
        protected void unsetCurrentSone() {
-               when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
-               when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
+               when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
+               when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
        }
 
        protected void request(String uri, Method method) {
index c16fdef..b4a6a55 100644 (file)
@@ -33,18 +33,10 @@ class SoneTemplatePageTest : WebPageTest() {
        }
 
        @Test
-       fun `retrieving current sone with creation is forwarded to web interface`() {
-               mock<Sone>().let {
-                       whenever(webInterface.getCurrentSone(toadletContext, true)).thenReturn(it)
-                       assertThat(page.getCurrentSone(toadletContext, true), equalTo(it))
-               }
-       }
-
-       @Test
        fun `retrieving current sone without creation is forwarded to web interface`() {
                mock<Sone>().let {
-                       whenever(webInterface.getCurrentSone(toadletContext, false)).thenReturn(it)
-                       assertThat(page.getCurrentSone(toadletContext, false), equalTo(it))
+                       whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(it)
+                       assertThat(page.getCurrentSoneWithoutCreatingSession(toadletContext), equalTo(it))
                }
        }