From: David ‘Bombe’ Roden Date: Mon, 6 Feb 2017 06:31:06 +0000 (+0100) Subject: Rename session-related Methode in web interface X-Git-Tag: 0.9.7^2~306 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=128580ddb154ce7d4f12108a8a70a8fe12488ffe;p=Sone.git Rename session-related Methode in web interface --- diff --git a/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java b/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java index a74204c..2484e3a 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java @@ -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); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java index acc6354..7a222d2 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -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); } diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index da93a02..c74f4b9 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -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 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/LoginPage.java b/src/main/java/net/pterodactylus/sone/web/LoginPage.java index f9bd371..65ab0cf 100644 --- a/src/main/java/net/pterodactylus/sone/web/LoginPage.java +++ b/src/main/java/net/pterodactylus/sone/web/LoginPage.java @@ -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; } } diff --git a/src/main/java/net/pterodactylus/sone/web/LogoutPage.java b/src/main/java/net/pterodactylus/sone/web/LogoutPage.java index f7a254c..3ec62a3 100644 --- a/src/main/java/net/pterodactylus/sone/web/LogoutPage.java +++ b/src/main/java/net/pterodactylus/sone/web/LogoutPage.java @@ -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); } } diff --git a/src/main/java/net/pterodactylus/sone/web/NewPage.java b/src/main/java/net/pterodactylus/sone/web/NewPage.java index fc8bb56..9eab69a 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -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 posts = new HashSet(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false))); - for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) { + Set posts = new HashSet(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))); + for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) { posts.add(reply.getPost().get()); } diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index 341bc0c..7cf7246 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -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 fieldErrors = new ArrayList(); if (currentSone != null) { diff --git a/src/main/java/net/pterodactylus/sone/web/RescuePage.java b/src/main/java/net/pterodactylus/sone/web/RescuePage.java index 78a2b9e..0f94563 100644 --- a/src/main/java/net/pterodactylus/sone/web/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/RescuePage.java @@ -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))) { diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index 27d4bf7..2b16dba 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -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; } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1ee7292..30613af 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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 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 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 { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java index 26a1207..b43d837 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java @@ -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"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java index 65d3943..59c6b2f 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java @@ -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 notifications = new ArrayList(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()); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java index 7a4c391..efb6716 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -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 sones = new HashSet(Collections.singleton(getCurrentSone(request.getToadletContext(), false))); + Set sones = new HashSet(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 notifications = new ArrayList(webInterface.getNotifications(currentSone)); Collections.sort(notifications, Notification.CREATED_TIME_SORTER); /* load new posts. */ - Collection newPosts = webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false)); + Collection 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 newReplies = webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false)); + Collection newReplies = webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext())); ArrayNode jsonReplies = new ArrayNode(instance); for (PostReply reply : newReplies) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java index 1050eee..eee28ec 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -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"))); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java index a186d46..586366e 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java @@ -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"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java index 644dfd6..bd79b98 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java @@ -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"); } diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index a1af8cc..8e77945 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -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.absent()); when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList()); } @@ -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) { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/SoneTemplatePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/SoneTemplatePageTest.kt index c16fdef..b4a6a55 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/SoneTemplatePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/SoneTemplatePageTest.kt @@ -33,18 +33,10 @@ class SoneTemplatePageTest : WebPageTest() { } @Test - fun `retrieving current sone with creation is forwarded to web interface`() { - mock().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().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)) } }