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=c64f2d7a3a1ad96597dedbfa9fcfe2132945a0c5;hpb=1e9a08c2b73b16dc178437eb8c8025aaa083fcca;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 c64f2d7..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â2015 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
@@ -21,6 +21,7 @@ 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;
@@ -29,21 +30,21 @@ 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;
@@ -66,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;
@@ -105,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));
}
/**
@@ -123,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));
}
/**
@@ -145,31 +147,18 @@ 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;
- OutputStream pageBucketOutputStream = null;
- Response pageResponse;
- try {
- pageBucket = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1);
- pageBucketOutputStream = pageBucket.getOutputStream();
- pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream));
- } catch (IOException ioe1) {
- Closer.close(pageBucket);
- throw ioe1;
- } finally {
- Closer.close(pageBucketOutputStream);
- }
- MultiValueTable headers = new MultiValueTable();
- if (pageResponse.getHeaders() != null) {
- for (Header header : pageResponse.getHeaders()) {
- for (String value : header) {
- headers.put(header.getName(), value);
+ 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) {
+ headers.put(header.getName(), value);
+ }
}
}
- }
- try {
- writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket);
- } finally {
- Closer.close(pageBucket);
+ writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket.getBucket());
}
}