X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSoneTemplatePage.java;h=e0b42b14464b9976734ef815a34207cd32eb854d;hb=98aedbc2097b3007e1529a8dded81cbfa0914513;hp=2b697ce7783f3b7f68fd81b3e4a08a664b329cac;hpb=18c431e8997e88425d596ee72a01da4244a9a3e9;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index 2b697ce..e0b42b1 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -19,6 +19,7 @@ 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; @@ -60,26 +61,71 @@ public class SoneTemplatePage extends TemplatePage { // /** - * Returns the currently logged in Sone. + * Returns the current session, creating a new session if there is no + * current session. * * @param request * The request to extract the session information from - * @return The currently logged in Sone, or {@code null} if no Sone is - * currently logged in + * @return The current session, or {@code null} if there is no current + * session */ - protected Sone getCurrentSone(Request request) { + protected Session getCurrentSession(Request request) { + return getCurrentSession(request, true); + } + + /** + * Returns the current session, creating a new session if there is no + * current session and {@code create} is {@code true}. + * + * @param request + * The request to extract the session information from + * @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 + */ + protected Session getCurrentSession(Request request, boolean create) { try { Session session = webInterface.sessionManager().useSession(request.getToadletContext()); - if (session == null) { - return null; + if (create && (session == null)) { + session = webInterface.sessionManager().createSession(UUID.randomUUID().toString(), request.getToadletContext()); } - return (Sone) session.getAttribute("Sone.CurrentSone"); + return session; } catch (RedirectException re1) { - /* okay, no current session, return null. */ return null; } } + /** + * Returns the currently logged in Sone. + * + * @param request + * The request to extract the session information from + * @return The currently logged in Sone, or {@code null} if no Sone is + * currently logged in + */ + protected Sone getCurrentSone(Request request) { + Session session = getCurrentSession(request); + if (session == null) { + return null; + } + return (Sone) session.getAttribute("Sone.CurrentSone"); + } + + /** + * Sets the currently logged in Sone. + * + * @param request + * The request + * @param sone + * The Sone to set as currently logged in + */ + protected void setCurrentSone(Request request, Sone sone) { + Session session = getCurrentSession(request); + session.setAttribute("Sone.CurrentSone", sone); + } + // // TEMPLATEPAGE METHODS //