From 6610d8a9946508f17fc0ed2f0a7ff806a60c357f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 8 Apr 2017 16:40:27 +0200 Subject: [PATCH] Use session provider in json base page --- .../sone/web/ajax/DistrustAjaxPage.java | 2 +- .../sone/web/ajax/GetNotificationsAjaxPage.java | 2 +- .../net/pterodactylus/sone/web/ajax/JsonPage.java | 20 +++++++------------- .../pterodactylus/sone/web/ajax/TrustAjaxPage.java | 2 +- .../pterodactylus/sone/web/ajax/UntrustAjaxPage.java | 2 +- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java index b43d837..26a1207 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java @@ -47,7 +47,7 @@ public class DistrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext(), false); if (currentSone == null) { return createErrorJsonObject("auth-required"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java index 59c6b2f..a61f4ec 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java @@ -79,7 +79,7 @@ public class GetNotificationsAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext(), false); List notifications = new ArrayList(webInterface.getNotifications(currentSone)); Collections.sort(notifications, Notification.CREATED_TIME_SORTER); ArrayNode jsonNotifications = new ArrayNode(instance); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java index 70ecb2b..2f84d27 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -30,6 +30,7 @@ import java.util.logging.Logger; import javax.annotation.Nonnull; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.web.SessionProvider; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetPage; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -38,7 +39,6 @@ import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; import com.fasterxml.jackson.databind.ObjectMapper; -import freenet.clients.http.SessionManager.Session; import freenet.clients.http.ToadletContext; /** @@ -60,6 +60,7 @@ public abstract class JsonPage implements FreenetPage { /** The Sone web interface. */ protected final WebInterface webInterface; + private final SessionProvider sessionProvider; /** * Creates a new JSON page at the given path. @@ -72,26 +73,19 @@ public abstract class JsonPage implements FreenetPage { public JsonPage(String path, WebInterface webInterface) { this.path = path; this.webInterface = webInterface; + this.sessionProvider = webInterface; } // // ACCESSORS // - /** - * Returns the currently logged in Sone. - * - * @param toadletContext - * The toadlet context - * @return The currently logged in Sone, or {@code null} if no Sone is - * currently logged in - */ protected Sone getCurrentSone(ToadletContext toadletContext) { - return webInterface.getCurrentSoneCreatingSession(toadletContext); + return sessionProvider.getCurrentSone(toadletContext, true); } - protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) { - return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext); + protected Sone getCurrentSone(ToadletContext toadletContext, boolean createSession) { + return sessionProvider.getCurrentSone(toadletContext, createSession); } // @@ -192,7 +186,7 @@ public abstract class JsonPage implements FreenetPage { } } if (requiresLogin()) { - if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null) { + if (getCurrentSone(request.getToadletContext(), false) == null) { return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required"))); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java index 586366e..a186d46 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java @@ -47,7 +47,7 @@ public class TrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext(), false); if (currentSone == null) { return createErrorJsonObject("auth-required"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java index bd79b98..644dfd6 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java @@ -47,7 +47,7 @@ public class UntrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext(), false); if (currentSone == null) { return createErrorJsonObject("auth-required"); } -- 2.7.4