From 85e6bdee8e9376766c131e85629b37a1dc0942e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 1 Dec 2010 20:25:45 +0100 Subject: [PATCH] Move session-related methods to web interface. --- .../pterodactylus/sone/web/SoneTemplatePage.java | 26 ++----- .../net/pterodactylus/sone/web/WebInterface.java | 84 ++++++++++++++++++++++ 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index 496fb60..9a8bd9b 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -19,7 +19,6 @@ package net.pterodactylus.sone.web; import java.util.Arrays; import java.util.Collection; -import java.util.UUID; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.Page; @@ -93,7 +92,7 @@ public class SoneTemplatePage extends TemplatePage { * session */ protected Session getCurrentSession(ToadletContext toadletContenxt) { - return getCurrentSession(toadletContenxt, true); + return webInterface.getCurrentSession(toadletContenxt); } /** @@ -109,11 +108,7 @@ public class SoneTemplatePage extends TemplatePage { * session */ protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) { - Session session = webInterface.getSessionManager().useSession(toadletContenxt); - if (create && (session == null)) { - session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt); - } - return session; + return webInterface.getCurrentSession(toadletContenxt, create); } /** @@ -125,15 +120,7 @@ public class SoneTemplatePage extends TemplatePage { * currently logged in */ protected Sone getCurrentSone(ToadletContext toadletContext) { - Session session = getCurrentSession(toadletContext); - if (session == null) { - return null; - } - String soneId = (String) session.getAttribute("Sone.CurrentSone"); - if (soneId == null) { - return null; - } - return webInterface.getCore().getLocalSone(soneId, false); + return webInterface.getCurrentSone(toadletContext); } /** @@ -145,12 +132,7 @@ public class SoneTemplatePage extends TemplatePage { * The Sone to set as currently logged in */ protected void setCurrentSone(ToadletContext toadletContext, Sone sone) { - Session session = getCurrentSession(toadletContext); - if (sone == null) { - session.removeAttribute("Sone.CurrentSone"); - } else { - session.setAttribute("Sone.CurrentSone", sone.getId()); - } + webInterface.setCurrentSone(toadletContext, sone); } // diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 75b218c..1612e28 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -84,7 +85,9 @@ import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.template.XmlFilter; import net.pterodactylus.util.thread.Ticker; import freenet.clients.http.SessionManager; +import freenet.clients.http.SessionManager.Session; import freenet.clients.http.ToadletContainer; +import freenet.clients.http.ToadletContext; import freenet.l10n.BaseL10n; /** @@ -189,6 +192,87 @@ public class WebInterface implements CoreListener { } /** + * 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 + */ + public Session getCurrentSession(ToadletContext toadletContenxt) { + return getCurrentSession(toadletContenxt, true); + } + + /** + * 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 + */ + public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) { + Session session = getSessionManager().useSession(toadletContenxt); + if (create && (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(getCurrentSession(toadletContext)); + } + + /** + * Returns the currently logged in Sone. + * + * @param session + * The session + * @return The currently logged in Sone, or {@code null} if no Sone is + * currently logged in + */ + public Sone getCurrentSone(Session session) { + if (session == null) { + return null; + } + String soneId = (String) session.getAttribute("Sone.CurrentSone"); + if (soneId == null) { + return null; + } + return getCore().getLocalSone(soneId, false); + } + + /** + * Sets the currently logged in Sone. + * + * @param toadletContext + * The toadlet context + * @param sone + * The Sone to set as currently logged in + */ + public void setCurrentSone(ToadletContext toadletContext, Sone sone) { + Session session = getCurrentSession(toadletContext); + if (sone == null) { + session.removeAttribute("Sone.CurrentSone"); + } else { + session.setAttribute("Sone.CurrentSone", sone.getId()); + } + } + + /** * Returns the notification manager. * * @return The notification manager -- 2.7.4