Use optional session in web interface.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Dec 2014 19:43:51 +0000 (20:43 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Dec 2014 19:43:51 +0000 (20:43 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java

index 9fe5bba..02a5dd5 100644 (file)
@@ -152,6 +152,7 @@ import net.pterodactylus.util.web.RedirectPage;
 import net.pterodactylus.util.web.StaticPage;
 import net.pterodactylus.util.web.TemplatePage;
 
 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;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.eventbus.Subscribe;
@@ -360,37 +361,16 @@ public class WebInterface {
                return templateContextFactory;
        }
 
                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) {
        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<Session> getCurrentSession(ToadletContext toadletContenxt, boolean createSession) {
                Session session = getSessionManager().useSession(toadletContenxt);
                Session session = getSessionManager().useSession(toadletContenxt);
-               if (create && (session == null)) {
+               if (createSession && (session == null)) {
                        session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
                }
                        session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
                }
-               return session;
+               return Optional.fromNullable(session);
        }
 
        /**
        }
 
        /**
@@ -410,18 +390,18 @@ public class WebInterface {
         *
         * @param toadletContext
         *            The toadlet context
         *
         * @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
         */
         *            {@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 getCurrentSone(ToadletContext toadletContext, boolean createSession) {
                Collection<Sone> localSones = getCore().getLocalSones();
                if (localSones.size() == 1) {
                        return localSones.iterator().next();
                }
                Collection<Sone> localSones = getCore().getLocalSones();
                if (localSones.size() == 1) {
                        return localSones.iterator().next();
                }
-               return getCurrentSone(getCurrentSession(toadletContext, create));
+               return getCurrentSone(getCurrentSession(toadletContext, createSession));
        }
 
        /**
        }
 
        /**
@@ -432,11 +412,11 @@ public class WebInterface {
         * @return The currently logged in Sone, or {@code null} if no Sone is
         *         currently logged in
         */
         * @return The currently logged in Sone, or {@code null} if no Sone is
         *         currently logged in
         */
-       public Sone getCurrentSone(Session session) {
-               if (session == null) {
+       public Sone getCurrentSone(Optional<Session> session) {
+               if (!session.isPresent()) {
                        return null;
                }
                        return null;
                }
-               String soneId = (String) session.getAttribute("Sone.CurrentSone");
+               String soneId = (String) session.get().getAttribute("Sone.CurrentSone");
                if (soneId == null) {
                        return null;
                }
                if (soneId == null) {
                        return null;
                }