X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FWebInterface.java;h=258669af74dc6d292691036e8df12d6b18c041f3;hb=53a71d1d6b91e4d56af49a06f2e06bc4d11bf3eb;hp=dc48e4555d5c279d9c4cc8a6467f6020cfddeb98;hpb=a2bc0e269c0ad1a587764eaf9121b02890996319;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index dc48e45..258669a 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web; +import static java.util.logging.Logger.getLogger; import static net.pterodactylus.util.template.TemplateParser.parse; import java.io.IOException; @@ -128,7 +129,6 @@ import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.sone.web.page.PageToadlet; import net.pterodactylus.sone.web.page.PageToadletFactory; import net.pterodactylus.util.io.Closer; -import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.NotificationManager; import net.pterodactylus.util.notify.TemplateNotification; @@ -152,6 +152,7 @@ import net.pterodactylus.util.web.RedirectPage; import net.pterodactylus.util.web.StaticPage; import net.pterodactylus.util.web.TemplatePage; +import com.google.common.base.Optional; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableSet; import com.google.common.eventbus.Subscribe; @@ -173,7 +174,7 @@ import freenet.support.api.HTTPRequest; public class WebInterface { /** The logger. */ - private static final Logger logger = Logging.getLogger(WebInterface.class); + private static final Logger logger = getLogger("Sone.Web.Main"); /** The notification manager. */ private final NotificationManager notificationManager = new NotificationManager(); @@ -360,37 +361,16 @@ 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 - */ public Session getCurrentSession(ToadletContext toadletContenxt) { - return getCurrentSession(toadletContenxt, true); + return getCurrentSession(toadletContenxt, true).get(); } - /** - * 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) { + public Optional getCurrentSession(ToadletContext toadletContenxt, boolean createSession) { Session session = getSessionManager().useSession(toadletContenxt); - if (create && (session == null)) { + if (createSession && (session == null)) { session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt); } - return session; + return Optional.fromNullable(session); } /** @@ -401,7 +381,7 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Sone getCurrentSone(ToadletContext toadletContext) { + public Optional getCurrentSone(ToadletContext toadletContext) { return getCurrentSone(toadletContext, true); } @@ -410,18 +390,18 @@ public class WebInterface { * * @param toadletContext * The toadlet context - * @param create + * @param createSession * {@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 Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { Collection localSones = getCore().getLocalSones(); if (localSones.size() == 1) { - return localSones.iterator().next(); + return Optional.of(localSones.iterator().next()); } - return getCurrentSone(getCurrentSession(toadletContext, create)); + return getCurrentSone(getCurrentSession(toadletContext, createSession)); } /** @@ -432,15 +412,15 @@ public class WebInterface { * @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; + public Optional getCurrentSone(Optional session) { + if (!session.isPresent()) { + return Optional.absent(); } - String soneId = (String) session.getAttribute("Sone.CurrentSone"); + String soneId = (String) session.get().getAttribute("Sone.CurrentSone"); if (soneId == null) { - return null; + return Optional.absent(); } - return getCore().getLocalSone(soneId); + return Optional.fromNullable(getCore().getLocalSone(soneId)); } /**