X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FPageToadlet.java;h=2b223777b66992fbdd908769d965c5a55079645d;hb=9b102d5ff09775e3867ad8709f22baf0ad608d30;hp=2c6706f8646b9e35f0668b3ed15d9d16cfc11730;hpb=99888ce13cc17d49f5e217ab6f2c9ad5ef168792;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java
index 2c6706f..2b22377 100644
--- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java
+++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java
@@ -1,5 +1,5 @@
/*
- * Sone - PageToadlet.java - Copyright © 2010â2013 David Roden
+ * Sone - PageToadlet.java - Copyright © 2010â2019 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,30 +18,33 @@
package net.pterodactylus.sone.web.page;
import java.io.IOException;
+import java.io.OutputStream;
import java.net.URI;
+import net.pterodactylus.sone.utils.AutoCloseableBucket;
import net.pterodactylus.util.web.Header;
import net.pterodactylus.util.web.Method;
import net.pterodactylus.util.web.Page;
import net.pterodactylus.util.web.Response;
+
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;
+import freenet.l10n.NodeL10n;
import freenet.support.MultiValueTable;
-import freenet.support.api.Bucket;
import freenet.support.api.HTTPRequest;
-import freenet.support.io.Closer;
/**
* {@link Toadlet} implementation that is wrapped around a {@link Page}.
- *
- * @author David âBombeâ Roden
*/
public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet {
+ private final SessionManager sessionManager;
+
/** The name of the menu item. */
private final String menuName;
@@ -64,8 +67,9 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
* Prefix that is prepended to all {@link Page#getPath()} return
* values
*/
- protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page page, String pathPrefix) {
+ protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, SessionManager sessionManager, String menuName, Page page, String pathPrefix) {
super(highLevelSimpleClient);
+ this.sessionManager = sessionManager;
this.menuName = menuName;
this.page = page;
this.pathPrefix = pathPrefix;
@@ -103,7 +107,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
* 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(), sessionManager));
}
/**
@@ -121,7 +125,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
* 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(), sessionManager));
}
/**
@@ -143,12 +147,10 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
* if the toadlet context is closed
*/
private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException {
- Bucket pageBucket = null;
- try {
- pageBucket = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1);
- Response pageResponse = new Response(pageBucket.getOutputStream());
- pageResponse = page.handleRequest(pageRequest, pageResponse);
- MultiValueTable headers = new MultiValueTable();
+ try (AutoCloseableBucket pageBucket = new AutoCloseableBucket(pageRequest.getToadletContext().getBucketFactory().makeBucket(-1));
+ OutputStream pageBucketOutputStream = pageBucket.getBucket().getOutputStream()) {
+ Response pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream));
+ MultiValueTable headers = new MultiValueTable<>();
if (pageResponse.getHeaders() != null) {
for (Header header : pageResponse.getHeaders()) {
for (String value : header) {
@@ -156,11 +158,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
}
}
}
- writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket);
- } catch (Throwable t1) {
- writeInternalError(t1, pageRequest.getToadletContext());
- } finally {
- Closer.close(pageBucket);
+ writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket.getBucket());
}
}
@@ -184,7 +182,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil
*/
@Override
public boolean isLinkExcepted(URI link) {
- return (page instanceof FreenetPage) ? ((FreenetPage) page).isLinkExcepted(link) : false;
+ return (page instanceof FreenetPage) && ((FreenetPage) page).isLinkExcepted(link);
}
}