Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
- PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
+ PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), getSessionManager(), "/Sone/");
pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this, postVisibilityFilter), "Index"));
pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
import freenet.client.HighLevelSimpleClient;
import freenet.clients.http.LinkEnabledCallback;
import freenet.clients.http.LinkFilterExceptedToadlet;
+import freenet.clients.http.SessionManager;
import freenet.clients.http.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
*/
public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet {
+ private final SessionManager sessionManager;
+
/** The name of the menu item. */
private final String menuName;
* Prefix that is prepended to all {@link Page#getPath()} return
* values
*/
- protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page<FreenetRequest> page, String pathPrefix) {
+ protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, SessionManager sessionManager, String menuName, Page<FreenetRequest> page, String pathPrefix) {
super(highLevelSimpleClient);
+ this.sessionManager = sessionManager;
this.menuName = menuName;
this.page = page;
this.pathPrefix = pathPrefix;
* if the toadlet context is closed
*/
public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
- handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase()));
+ handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager));
}
/**
* if the toadlet context is closed
*/
public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
- handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase()));
+ handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase(), sessionManager));
}
/**
package net.pterodactylus.sone.web.page
import freenet.clients.http.*
+import freenet.clients.http.SessionManager.*
import freenet.l10n.*
import freenet.support.api.*
import net.pterodactylus.util.web.*
import java.net.*
+import java.util.UUID.*
open class FreenetRequest(uri: URI, method: Method,
val httpRequest: HTTPRequest,
val toadletContext: ToadletContext,
- val l10n: BaseL10n
-) : Request(uri, method)
+ val l10n: BaseL10n,
+ val sessionManager: SessionManager
+) : Request(uri, method) {
+
+ val session: Session
+ get() =
+ sessionManager.useSession(toadletContext)
+ ?: sessionManager.createSession(randomUUID().toString(), toadletContext)
+
+ val existingSession: Session? get() = sessionManager.useSession(toadletContext)
+
+}
package net.pterodactylus.sone.web.page
import freenet.client.*
+import freenet.clients.http.*
import net.pterodactylus.util.web.*
-class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClient, private val pathPrefix: String) {
+class PageToadletFactory(private val highLevelSimpleClient: HighLevelSimpleClient, private val sessionManager: SessionManager, private val pathPrefix: String) {
@JvmOverloads
fun createPageToadlet(page: Page<FreenetRequest>, menuName: String? = null) =
- PageToadlet(highLevelSimpleClient, menuName ?: page.menuName, page, pathPrefix)
+ PageToadlet(highLevelSimpleClient, sessionManager, menuName ?: page.menuName, page, pathPrefix)
}
import net.pterodactylus.util.web.*
import java.net.*
-class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n,
+class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n, sessionManager: SessionManager,
val core: Core,
val webInterface: WebInterface
-) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
+) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager)
-fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface)
+fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface)
) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") {
private val core = webInterface.core
- protected val sessionProvider: SessionProvider = webInterface
+ private val sessionProvider: SessionProvider = webInterface
protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) =
sessionProvider.getCurrentSone(toadletContext, createSession)
private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
override fun isEnabled(toadletContext: ToadletContext) =
- isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, core, webInterface))
+ isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface))
open fun isEnabled(soneRequest: SoneRequest) = when {
requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false
import net.pterodactylus.util.web.Page;
import net.pterodactylus.util.web.Response;
+import freenet.clients.http.SessionManager;
import freenet.clients.http.ToadletContext;
import freenet.l10n.BaseL10n;
import freenet.support.api.HTTPRequest;
Method method = Method.GET;
HTTPRequest httpRequest = mock(HTTPRequest.class);
ToadletContext toadletContext = mock(ToadletContext.class);
- FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n);
+ SessionManager sessionManager = mock(SessionManager.class);
+ FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager);
OutputStream outputStream = new ByteArrayOutputStream();
Response response = new Response(outputStream);
page.handleRequest(request, response);
import net.pterodactylus.util.web.Page;
import net.pterodactylus.util.web.Response;
+import freenet.clients.http.SessionManager;
import freenet.clients.http.ToadletContext;
import freenet.l10n.BaseL10n;
import freenet.support.api.HTTPRequest;
Method method = Method.GET;
HTTPRequest httpRequest = mock(HTTPRequest.class);
ToadletContext toadletContext = mock(ToadletContext.class);
- FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n);
+ SessionManager sessionManager = mock(SessionManager.class);
+ FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager);
OutputStream outputStream = new ByteArrayOutputStream();
Response response = new Response(outputStream);
staticPage.handleRequest(request, response);
package net.pterodactylus.sone.web.page
import freenet.clients.http.*
+import freenet.clients.http.SessionManager.*
import freenet.l10n.*
import freenet.support.api.*
import net.pterodactylus.sone.test.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.*
+import org.mockito.*
import org.mockito.Mockito.*
-import org.mockito.Mockito.mock
import java.net.*
class FreenetRequestTest {
private val httpRequest = mock(HTTPRequest::class.java)
private val toadletContext = mock(ToadletContext::class.java)
private val l10n = mock<BaseL10n>()
- private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
+ private val sessionManager = mock<SessionManager>()
+ private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager)
@Test
fun `uri is retained correctly`() {
assertThat(request.l10n, equalTo(l10n))
}
+ @Test
+ fun `null is returned if no session exists`() {
+ assertThat(request.existingSession, nullValue())
+ }
+
+ @Test
+ fun `existing session can be retrieved`() {
+ val session = mock<Session>()
+ whenever(sessionManager.useSession(toadletContext)).thenReturn(session)
+ assertThat(request.existingSession, sameInstance(session))
+ }
+
+ @Test
+ fun `existing session is returned if it exists`() {
+ val session = mock<Session>()
+ whenever(sessionManager.useSession(toadletContext)).thenReturn(session)
+ assertThat(request.session, sameInstance(session))
+ }
+
+ @Test
+ fun `new session is returned if none exists`() {
+ val session = mock<Session>()
+ whenever(sessionManager.createSession(anyString(), ArgumentMatchers.eq(toadletContext))).thenReturn(session)
+ assertThat(request.session, sameInstance(session))
+ }
+
}
package net.pterodactylus.sone.web.page
import freenet.client.*
+import freenet.clients.http.*
import net.pterodactylus.sone.test.*
import net.pterodactylus.util.web.*
import org.hamcrest.MatcherAssert.*
import org.junit.*
private val highLevelSimpleClient = mock<HighLevelSimpleClient>()
+private val sessionManager = mock<SessionManager>()
private const val pathPrefix = "/some/prefix/"
class PageToadletFactoryTest {
- private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, pathPrefix)
+ private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, sessionManager, pathPrefix)
@Test
fun `page toadlet without menu name is created without menu name`() {
private val httpRequest = Mockito.mock(HTTPRequest::class.java)
private val toadletContext = Mockito.mock(ToadletContext::class.java)
private val l10n = mock<BaseL10n>()
+ private val sessionManager = mock<SessionManager>()
private val core = mock<Core>()
private val webInterface = mock<WebInterface>()
- private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface)
+ private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager, core, webInterface)
@Test
fun `freenet request properties are retained correctly`() {
assertThat(soneRequest.httpRequest, equalTo(httpRequest))
assertThat(soneRequest.toadletContext, equalTo(toadletContext))
assertThat(soneRequest.l10n, equalTo(l10n))
+ assertThat(soneRequest.sessionManager, equalTo(sessionManager))
}
@Test
@Test
fun `freenet request is wrapped correctly`() {
- val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
+ val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager)
val wrappedSoneRequest = freenetRequest.toSoneRequest(core, webInterface)
assertThat(wrappedSoneRequest.uri, equalTo(uri))
assertThat(wrappedSoneRequest.method, equalTo(method))
assertThat(wrappedSoneRequest.httpRequest, equalTo(httpRequest))
assertThat(wrappedSoneRequest.toadletContext, equalTo(toadletContext))
assertThat(wrappedSoneRequest.l10n, equalTo(l10n))
+ assertThat(wrappedSoneRequest.sessionManager, equalTo(sessionManager))
assertThat(wrappedSoneRequest.core, sameInstance(core))
assertThat(wrappedSoneRequest.webInterface, sameInstance(webInterface))
}
package net.pterodactylus.sone.web.pages
import com.google.common.eventbus.EventBus
-import freenet.clients.http.ToadletContext
+import freenet.clients.http.*
import freenet.support.SimpleReadOnlyArrayBucket
import freenet.support.api.HTTPRequest
import freenet.support.api.HTTPUploadedFile
val eventBus = mock<EventBus>()
val preferences = Preferences(eventBus)
val l10n = webInterface.l10n!!
+ val sessionManager = mock<SessionManager>()
val page by lazy { pageSupplier(template, webInterface) }
val httpRequest = mock<HTTPRequest>()
val freenetRequest = mock<FreenetRequest>()
init {
whenever(freenetRequest.l10n).thenReturn(l10n)
+ whenever(freenetRequest.sessionManager).thenReturn(sessionManager)
whenever(freenetRequest.uri).thenReturn(mock())
}
val soneRequest by lazy { freenetRequest.toSoneRequest(core, webInterface) }
}
private fun setupWebInterface() {
+ whenever(webInterface.sessionManager).thenReturn(sessionManager)
whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)