*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class WebInterface {
+public class WebInterface implements SessionProvider {
/** The logger. */
private static final Logger logger = getLogger(WebInterface.class.getName());
return getCore().getLocalSone(soneId);
}
+ @Override
+ @Nullable
+ public Sone getCurrentSone(@Nonnull ToadletContext toadletContext, boolean createSession) {
+ return createSession ? getCurrentSoneCreatingSession(toadletContext) : getCurrentSoneWithoutCreatingSession(toadletContext);
+ }
+
/**
* Sets the currently logged in Sone.
*
* @param sone
* The Sone to set as currently logged in
*/
- public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
+ @Override
+ public void setCurrentSone(@Nonnull ToadletContext toadletContext, @Nullable Sone sone) {
Session session = getOrCreateCurrentSession(toadletContext);
if (sone == null) {
session.removeAttribute("Sone.CurrentSone");
templateContext.set("albums", albumPagination.getItems());
return;
}
- Sone sone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
+ Sone sone = getCurrentSone(request.getToadletContext(), false);
templateContext.set("soneRequested", true);
templateContext.set("sone", sone);
}
templateContext.set("sort", sortField);
templateContext.set("order", sortOrder);
templateContext.set("filter", filter);
- final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
+ final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
if ((currentSone != null) && "followed".equals(filter)) {
knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return (getCurrentSoneWithoutCreatingSession(toadletContext) != null) && (webInterface.getCore().getLocalSones().size() != 1);
+ return (getCurrentSone(toadletContext, false) != null) && (webInterface.getCore().getLocalSones().size() != 1);
}
}
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
/* collect new elements from notifications. */
- Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
- for (PostReply reply : webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()))) {
+ Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false)));
+ for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) {
posts.add(reply.getPost().get());
}
*/
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
- Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
+ Sone currentSone = getCurrentSone(request.getToadletContext(), false);
SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
if (request.getMethod() == Method.POST) {
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
import java.util.Map;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.sone.web.SessionProvider;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.sone.web.page.FreenetTemplatePage;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
-import freenet.clients.http.SessionManager.Session;
import freenet.clients.http.ToadletContext;
import freenet.support.api.HTTPRequest;
/** The Sone core. */
protected final WebInterface webInterface;
+ protected final SessionProvider sessionProvider;
/** The page title l10n key. */
private final String pageTitleKey;
super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
this.pageTitleKey = pageTitleKey;
this.webInterface = webInterface;
+ this.sessionProvider = webInterface;
this.requireLogin = requireLogin;
}
// PROTECTED METHODS
//
- /**
- * 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);
+ @Nullable
+ protected Sone getCurrentSone(@Nonnull ToadletContext toadletContext) {
+ return getCurrentSone(toadletContext, true);
}
- protected Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
- return webInterface.getCurrentSoneWithoutCreatingSession(toadletContext);
+ @Nullable
+ protected Sone getCurrentSone(@Nonnull ToadletContext toadletContext, boolean createSession) {
+ return sessionProvider.getCurrentSone(toadletContext, createSession);
}
- /**
- * Sets the currently logged in Sone.
- *
- * @param toadletContext
- * The toadlet context
- * @param sone
- * The Sone to set as currently logged in
- */
- protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
- webInterface.setCurrentSone(toadletContext, sone);
+ protected void setCurrentSone(@Nonnull ToadletContext toadletContext, @Nullable Sone sone) {
+ sessionProvider.setCurrentSone(toadletContext, sone);
}
//
@Override
protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
+ Sone currentSone = getCurrentSone(request.getToadletContext(), false);
templateContext.set("preferences", webInterface.getCore().getPreferences());
templateContext.set("currentSone", currentSone);
templateContext.set("localSones", webInterface.getCore().getLocalSones());
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
- if (requiresLogin() && (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) == null)) {
+ if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
HTTPRequest httpRequest = request.getHttpRequest();
String originalUrl = httpRequest.getPath();
if (httpRequest.hasParameters()) {
return false;
}
if (requiresLogin()) {
- return getCurrentSoneWithoutCreatingSession(toadletContext) != null;
+ return getCurrentSone(toadletContext, false) != null;
}
return true;
}
--- /dev/null
+package net.pterodactylus.sone.web
+
+import freenet.clients.http.ToadletContext
+import net.pterodactylus.sone.data.Sone
+
+/**
+ * Provides access to the currently logged-in [Sone].
+ */
+interface SessionProvider {
+
+ fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true): Sone?
+ fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?)
+
+}
SoneTemplatePage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface, true) {
override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
- val currentSone = getCurrentSone(request.toadletContext)
+ val currentSone = getCurrentSone(request.toadletContext)!!
val field = currentSone.profile.getFieldById(request.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html")
templateContext["field"] = field
if (request.isPOST) {
override fun isEnabled(toadletContext: ToadletContext) = when {
webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
- else -> getCurrentSoneWithoutCreatingSession(toadletContext) == null
+ else -> getCurrentSone(toadletContext, false) == null
}
}
@Before
public final void setupWebInterface() {
+ when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone);
when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone);
when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
}
protected void unsetCurrentSone() {
+ when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
}
import net.pterodactylus.sone.main.SonePlugin
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.sone.web.pages.SoneTemplatePage
import net.pterodactylus.sone.web.page.FreenetRequest
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.template.TemplateContext
@Test
fun `retrieving current sone without creation is forwarded to web interface`() {
mock<Sone>().let {
- whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(it)
- assertThat(page.getCurrentSoneWithoutCreatingSession(toadletContext), equalTo(it))
+ whenever(webInterface.getCurrentSone(toadletContext, false)).thenReturn(it)
+ assertThat(page.getCurrentSone(toadletContext, false), equalTo(it))
}
}