projects
/
Sone.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
de7568a
)
Add session provider interface
author
David ‘Bombe’ Roden
<bombe@pterodactylus.net>
Sat, 8 Apr 2017 14:23:59 +0000
(16:23 +0200)
committer
David ‘Bombe’ Roden
<bombe@pterodactylus.net>
Sat, 8 Apr 2017 14:23:59 +0000
(16:23 +0200)
12 files changed:
src/main/java/net/pterodactylus/sone/web/WebInterface.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
patch
|
blob
|
history
src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt
[new file with mode: 0644]
patch
|
blob
src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
patch
|
blob
|
history
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
patch
|
blob
|
history
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
patch
|
blob
|
history
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
patch
|
blob
|
history
diff --git
a/src/main/java/net/pterodactylus/sone/web/WebInterface.java
b/src/main/java/net/pterodactylus/sone/web/WebInterface.java
index
ee309df
..
946e6d6
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/WebInterface.java
+++ b/
src/main/java/net/pterodactylus/sone/web/WebInterface.java
@@
-223,7
+223,7
@@
import com.google.inject.Inject;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
*
* @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());
/** The logger. */
private static final Logger logger = getLogger(WebInterface.class.getName());
@@
-469,6
+469,12
@@
public class WebInterface {
return getCore().getLocalSone(soneId);
}
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.
*
/**
* Sets the currently logged in Sone.
*
@@
-477,7
+483,8
@@
public class WebInterface {
* @param sone
* The Sone to set as currently logged in
*/
* @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");
Session session = getOrCreateCurrentSession(toadletContext);
if (sone == null) {
session.removeAttribute("Sone.CurrentSone");
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
b/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
index
2e53761
..
c8ee0d0
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
@@
-102,7
+102,7
@@
public class ImageBrowserPage extends SoneTemplatePage {
templateContext.set("albums", albumPagination.getItems());
return;
}
templateContext.set("albums", albumPagination.getItems());
return;
}
- Sone sone = getCurrentSone
WithoutCreatingSession(request.getToadletContext()
);
+ Sone sone = getCurrentSone
(request.getToadletContext(), false
);
templateContext.set("soneRequested", true);
templateContext.set("sone", sone);
}
templateContext.set("soneRequested", true);
templateContext.set("sone", sone);
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
b/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
index
5aef1f9
..
8864257
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java
@@
-73,7
+73,7
@@
public class KnownSonesPage extends SoneTemplatePage {
templateContext.set("sort", sortField);
templateContext.set("order", sortOrder);
templateContext.set("filter", filter);
templateContext.set("sort", sortField);
templateContext.set("order", sortOrder);
templateContext.set("filter", filter);
- final Sone currentSone = getCurrentSone
WithoutCreatingSession(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>() {
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>() {
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
b/src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
index
1249086
..
1f2261c
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/LogoutPage.java
@@
-61,7
+61,7
@@
public class LogoutPage extends SoneTemplatePage {
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return (getCurrentSone
WithoutCreatingSession(toadletContext
) != null) && (webInterface.getCore().getLocalSones().size() != 1);
+ return (getCurrentSone
(toadletContext, false
) != null) && (webInterface.getCore().getLocalSones().size() != 1);
}
}
}
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
b/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
index
411f40f
..
b5c6e11
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
@@
-65,8
+65,8
@@
public class NewPage extends SoneTemplatePage {
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
/* collect new elements from notifications. */
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
/* collect new elements from notifications. */
- Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSone
WithoutCreatingSession(request.getToadletContext()
)));
- for (PostReply reply : webInterface.getNewReplies(getCurrentSone
WithoutCreatingSession(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());
}
posts.add(reply.getPost().get());
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
b/src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
index
93ef9e1
..
b2ce5d1
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/RescuePage.java
@@
-56,7
+56,7
@@
public class RescuePage extends SoneTemplatePage {
*/
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
*/
@Override
protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
- Sone currentSone = getCurrentSone
WithoutCreatingSession(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))) {
SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
if (request.getMethod() == Method.POST) {
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
diff --git
a/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
b/src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
index
46c8318
..
adc4a3a
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/pages/SoneTemplatePage.java
@@
-27,9
+27,11
@@
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
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.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.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.sone.web.page.FreenetTemplatePage;
@@
-37,7
+39,6
@@
import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
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;
import freenet.clients.http.ToadletContext;
import freenet.support.api.HTTPRequest;
@@
-53,6
+54,7
@@
public class SoneTemplatePage extends FreenetTemplatePage {
/** The Sone core. */
protected final WebInterface webInterface;
/** The Sone core. */
protected final WebInterface webInterface;
+ protected final SessionProvider sessionProvider;
/** The page title l10n key. */
private final String pageTitleKey;
/** The page title l10n key. */
private final String pageTitleKey;
@@
-111,6
+113,7
@@
public class SoneTemplatePage extends FreenetTemplatePage {
super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
this.pageTitleKey = pageTitleKey;
this.webInterface = webInterface;
super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
this.pageTitleKey = pageTitleKey;
this.webInterface = webInterface;
+ this.sessionProvider = webInterface;
this.requireLogin = requireLogin;
}
this.requireLogin = requireLogin;
}
@@
-118,32
+121,18
@@
public class SoneTemplatePage extends FreenetTemplatePage {
// PROTECTED METHODS
//
// 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);
}
//
}
//
@@
-201,7
+190,7
@@
public class SoneTemplatePage extends FreenetTemplatePage {
@Override
protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
@Override
protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
- Sone currentSone = getCurrentSone
WithoutCreatingSession(request.getToadletContext()
);
+ Sone currentSone = getCurrentSone
(request.getToadletContext(), false
);
templateContext.set("preferences", webInterface.getCore().getPreferences());
templateContext.set("currentSone", currentSone);
templateContext.set("localSones", webInterface.getCore().getLocalSones());
templateContext.set("preferences", webInterface.getCore().getPreferences());
templateContext.set("currentSone", currentSone);
templateContext.set("localSones", webInterface.getCore().getLocalSones());
@@
-226,7
+215,7
@@
public class SoneTemplatePage extends FreenetTemplatePage {
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
- if (requiresLogin() && (getCurrentSone
WithoutCreatingSession(request.getToadletContext()
) == null)) {
+ if (requiresLogin() && (getCurrentSone
(request.getToadletContext(), false
) == null)) {
HTTPRequest httpRequest = request.getHttpRequest();
String originalUrl = httpRequest.getPath();
if (httpRequest.hasParameters()) {
HTTPRequest httpRequest = request.getHttpRequest();
String originalUrl = httpRequest.getPath();
if (httpRequest.hasParameters()) {
@@
-273,7
+262,7
@@
public class SoneTemplatePage extends FreenetTemplatePage {
return false;
}
if (requiresLogin()) {
return false;
}
if (requiresLogin()) {
- return getCurrentSone
WithoutCreatingSession(toadletContext
) != null;
+ return getCurrentSone
(toadletContext, false
) != null;
}
return true;
}
}
return true;
}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt
b/src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt
new file mode 100644
(file)
index 0000000..
463ddaa
--- /dev/null
+++ b/
src/main/kotlin/net/pterodactylus/sone/web/SessionProvider.kt
@@ -0,0
+1,14
@@
+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?)
+
+}
diff --git
a/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
b/src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
index
e0dc4be
..
890ac74
100644
(file)
--- a/
src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
+++ b/
src/main/kotlin/net/pterodactylus/sone/web/pages/DeleteProfileFieldPage.kt
@@
-14,7
+14,7
@@
class DeleteProfileFieldPage(template: Template, webInterface: WebInterface):
SoneTemplatePage("deleteProfileField.html", template, "Page.DeleteProfileField.Title", webInterface, true) {
override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
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) {
val field = currentSone.profile.getFieldById(request.httpRequest.getPartAsStringFailsafe("field", 36)) ?: throw RedirectException("invalid.html")
templateContext["field"] = field
if (request.isPOST) {
diff --git
a/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
b/src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
index
695816f
..
0d839db
100644
(file)
--- a/
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
+++ b/
src/main/kotlin/net/pterodactylus/sone/web/pages/LoginPage.kt
@@
-33,7
+33,7
@@
class LoginPage(template: Template, webInterface: WebInterface):
override fun isEnabled(toadletContext: ToadletContext) = when {
webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
override fun isEnabled(toadletContext: ToadletContext) = when {
webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
- else -> getCurrentSone
WithoutCreatingSession(toadletContext
) == null
+ else -> getCurrentSone
(toadletContext, false
) == null
}
}
}
}
diff --git
a/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
b/src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
index
3c6b1d4
..
532c09a
100644
(file)
--- a/
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
+++ b/
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
@@
-274,6
+274,7
@@
public abstract class WebPageTest {
@Before
public final void setupWebInterface() {
@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());
when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone);
when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone);
when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
@@
-290,6
+291,7
@@
public abstract class WebPageTest {
}
protected void unsetCurrentSone() {
}
protected void unsetCurrentSone() {
+ when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
}
when(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null);
when(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null);
}
diff --git
a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
index
42d6676
..
2eb77e2
100644
(file)
--- a/
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
+++ b/
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt
@@
-4,7
+4,6
@@
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.main.SonePlugin
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
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
import net.pterodactylus.sone.web.page.FreenetRequest
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.template.TemplateContext
@@
-36,8
+35,8
@@
class SoneTemplatePageTest : WebPageTest() {
@Test
fun `retrieving current sone without creation is forwarded to web interface`() {
mock<Sone>().let {
@Test
fun `retrieving current sone without creation is forwarded to web interface`() {
mock<Sone>().let {
- whenever(webInterface.getCurrentSone
WithoutCreatingSession(toadletContext
)).thenReturn(it)
- assertThat(page.getCurrentSone
WithoutCreatingSession(toadletContext
), equalTo(it))
+ whenever(webInterface.getCurrentSone
(toadletContext, false
)).thenReturn(it)
+ assertThat(page.getCurrentSone
(toadletContext, false
), equalTo(it))
}
}
}
}