Return an optional Sone from the current session.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
index 7dfdfe7..fa2798e 100644 (file)
@@ -34,10 +34,10 @@ import net.pterodactylus.util.notify.Notification;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
-import freenet.clients.http.SessionManager.Session;
 import freenet.clients.http.ToadletContext;
 import freenet.support.api.HTTPRequest;
 
@@ -131,35 +131,6 @@ public class SoneTemplatePage extends FreenetTemplatePage {
        //
 
        /**
-        * 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
-        */
-       protected Session getCurrentSession(ToadletContext toadletContenxt) {
-               return webInterface.getCurrentSession(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
-        */
-       protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
-               return webInterface.getCurrentSession(toadletContenxt, create);
-       }
-
-       /**
         * Returns the currently logged in Sone.
         *
         * @param toadletContext
@@ -167,7 +138,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
         * @return The currently logged in Sone, or {@code null} if no Sone is
         *         currently logged in
         */
-       protected Sone getCurrentSone(ToadletContext toadletContext) {
+       protected Optional<Sone> getCurrentSone(ToadletContext toadletContext) {
                return webInterface.getCurrentSone(toadletContext);
        }
 
@@ -182,7 +153,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
         * @return The currently logged in Sone, or {@code null} if no Sone is
         *         currently logged in
         */
-       protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
+       protected Optional<Sone> getCurrentSone(ToadletContext toadletContext, boolean create) {
                return webInterface.getCurrentSone(toadletContext, create);
        }
 
@@ -253,9 +224,9 @@ public class SoneTemplatePage extends FreenetTemplatePage {
        @Override
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
-               Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+               Optional<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
                templateContext.set("core", webInterface.getCore());
-               templateContext.set("currentSone", currentSone);
+               templateContext.set("currentSone", currentSone.orNull());
                templateContext.set("localSones", webInterface.getCore().getLocalSones());
                templateContext.set("request", request);
                templateContext.set("currentVersion", SonePlugin.VERSION);
@@ -263,7 +234,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
                templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
                templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
                templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
-               List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
+               List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
                Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
                templateContext.set("notifications", notifications);
                templateContext.set("notificationHash", notifications.hashCode());
@@ -274,7 +245,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
         */
        @Override
        protected String getRedirectTarget(FreenetRequest request) {
-               if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
+               if (requiresLogin() && !getCurrentSone(request.getToadletContext(), false).isPresent()) {
                        HTTPRequest httpRequest = request.getHttpRequest();
                        String originalUrl = httpRequest.getPath();
                        if (httpRequest.hasParameters()) {
@@ -316,7 +287,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
                        return false;
                }
                if (requiresLogin()) {
-                       return getCurrentSone(toadletContext, false) != null;
+                       return getCurrentSone(toadletContext, false).isPresent();
                }
                return true;
        }