import freenet.clients.http.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
+import freenet.l10n.NodeL10n;
import freenet.support.MultiValueTable;
import freenet.support.api.HTTPRequest;
* 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));
+ handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase()));
}
/**
* 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));
+ handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase()));
}
/**
package net.pterodactylus.sone.web.page
import freenet.clients.http.*
+import freenet.l10n.*
import freenet.support.api.*
import net.pterodactylus.util.web.*
import java.net.*
-open class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext) : Request(uri, method)
+open class FreenetRequest(uri: URI, method: Method,
+ val httpRequest: HTTPRequest,
+ val toadletContext: ToadletContext,
+ val l10n: BaseL10n
+) : Request(uri, method)
package net.pterodactylus.sone.web.page
import freenet.clients.http.*
+import freenet.l10n.*
import freenet.support.api.*
import net.pterodactylus.sone.core.*
+import net.pterodactylus.sone.web.*
import net.pterodactylus.util.web.*
import java.net.*
-class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, val core: Core) : FreenetRequest(uri, method, httpRequest, toadletContext)
+class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n,
+ val core: Core,
+ val webInterface: WebInterface
+) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
-fun FreenetRequest.toSoneRequest(core: Core) = SoneRequest(uri, method, httpRequest, toadletContext, core)
+fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface)
package net.pterodactylus.sone.web.pages
-import freenet.clients.http.ToadletContext
+import freenet.clients.http.*
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.main.SonePlugin
import net.pterodactylus.sone.utils.emptyToNull
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.template.Template
import net.pterodactylus.util.template.TemplateContext
+import net.pterodactylus.util.web.*
import java.net.URLEncoder
/**
fun requiresLogin() = requiresLogin
- override public fun getPageTitle(freenetRequest: FreenetRequest) = pageTitle(freenetRequest)
+ override public fun getPageTitle(freenetRequest: FreenetRequest) = getPageTitle(freenetRequest.toSoneRequest(core, webInterface))
+
+ open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest)
override public fun getStyleSheets() =
listOf("css/sone.css")
}
internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
- handleRequest(freenetRequest.toSoneRequest(core), templateContext)
+ handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext)
}
open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
- override fun isEnabled(toadletContext: ToadletContext) = when {
- requiresLogin && getCurrentSone(toadletContext) == null -> false
- core.preferences.requireFullAccess && !toadletContext.isAllowedFullAccess -> false
+ override fun isEnabled(toadletContext: ToadletContext) =
+ isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, core, webInterface))
+
+ open fun isEnabled(soneRequest: SoneRequest) = when {
+ requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false
+ core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false
else -> true
}
import net.pterodactylus.util.web.Response;
import freenet.clients.http.ToadletContext;
+import freenet.l10n.BaseL10n;
import freenet.support.api.HTTPRequest;
import com.google.common.base.Charsets;
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
+ private final BaseL10n l10n = mock(BaseL10n.class);
private final StringWriter stringWriter = new StringWriter();
private final TemplateContext templateContext = new TemplateContext();
private Loaders loaders;
Method method = Method.GET;
HTTPRequest httpRequest = mock(HTTPRequest.class);
ToadletContext toadletContext = mock(ToadletContext.class);
- FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext);
+ FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n);
OutputStream outputStream = new ByteArrayOutputStream();
Response response = new Response(outputStream);
page.handleRequest(request, response);
import net.pterodactylus.util.web.Response;
import freenet.clients.http.ToadletContext;
+import freenet.l10n.BaseL10n;
import freenet.support.api.HTTPRequest;
import org.junit.Test;
*/
public class DefaultLoadersTest {
+ private final BaseL10n l10n = mock(BaseL10n.class);
private final Loaders loaders = new DefaultLoaders();
private final StringWriter stringWriter = new StringWriter();
private final TemplateContext templateContext = new TemplateContext();
Method method = Method.GET;
HTTPRequest httpRequest = mock(HTTPRequest.class);
ToadletContext toadletContext = mock(ToadletContext.class);
- FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext);
+ FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n);
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.l10n.*
import freenet.support.api.*
+import net.pterodactylus.sone.test.*
import net.pterodactylus.util.web.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.*
import org.mockito.Mockito.*
+import org.mockito.Mockito.mock
import java.net.*
class FreenetRequestTest {
private val method = Method.GET
private val httpRequest = mock(HTTPRequest::class.java)
private val toadletContext = mock(ToadletContext::class.java)
- private val request = FreenetRequest(uri, method, httpRequest, toadletContext)
+ private val l10n = mock<BaseL10n>()
+ private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
@Test
fun `uri is retained correctly`() {
assertThat(request.toadletContext, equalTo(toadletContext))
}
+ @Test
+ fun `l10n is retained correctly`() {
+ assertThat(request.l10n, equalTo(l10n))
+ }
+
}
package net.pterodactylus.sone.web.page
import freenet.clients.http.*
+import freenet.l10n.*
import freenet.support.api.*
import net.pterodactylus.sone.core.*
import net.pterodactylus.sone.test.*
+import net.pterodactylus.sone.web.*
import net.pterodactylus.util.web.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
private val method = Method.GET
private val httpRequest = Mockito.mock(HTTPRequest::class.java)
private val toadletContext = Mockito.mock(ToadletContext::class.java)
+ private val l10n = mock<BaseL10n>()
private val core = mock<Core>()
- private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, core)
+ private val webInterface = mock<WebInterface>()
+ private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface)
@Test
fun `freenet request properties are retained correctly`() {
assertThat(soneRequest.method, equalTo(method))
assertThat(soneRequest.httpRequest, equalTo(httpRequest))
assertThat(soneRequest.toadletContext, equalTo(toadletContext))
+ assertThat(soneRequest.l10n, equalTo(l10n))
}
@Test
}
@Test
+ fun `web interface is retained correctly`() {
+ assertThat(soneRequest.webInterface, sameInstance(webInterface))
+ }
+
+ @Test
fun `freenet request is wrapped correctly`() {
- val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext)
- val wrappedSoneRequest = freenetRequest.toSoneRequest(core)
+ val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n)
+ 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.core, sameInstance(core))
+ assertThat(wrappedSoneRequest.webInterface, sameInstance(webInterface))
}
}
import net.pterodactylus.sone.utils.asList
import net.pterodactylus.sone.utils.asOptional
import net.pterodactylus.sone.web.WebInterface
-import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.sone.web.page.*
import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.template.Template
val page by lazy { pageSupplier(template, webInterface) }
val httpRequest = mock<HTTPRequest>()
val freenetRequest = mock<FreenetRequest>()
+ init {
+ whenever(freenetRequest.l10n).thenReturn(l10n)
+ whenever(freenetRequest.uri).thenReturn(mock())
+ }
val templateContext = TemplateContext()
val toadletContext = deepMock<ToadletContext>()
val responseContent = ByteArrayOutputStream()