Return an optional Sone from the current session.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
index e18e1a4..fa2798e 100644 (file)
@@ -34,6 +34,7 @@ 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;
 
@@ -137,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);
        }
 
@@ -152,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);
        }
 
@@ -223,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);
@@ -233,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());
@@ -244,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()) {
@@ -286,7 +287,7 @@ public class SoneTemplatePage extends FreenetTemplatePage {
                        return false;
                }
                if (requiresLogin()) {
-                       return getCurrentSone(toadletContext, false) != null;
+                       return getCurrentSone(toadletContext, false).isPresent();
                }
                return true;
        }