X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FPageToadlet.java;h=a40a543d4579a5e4eab4021d8dd121b828f3ae20;hb=0fd78802d3ee78b558108d714314d0117078d105;hp=f594e5897b9e4d88319e7703f5dc16987a6facbe;hpb=aa94dcb712392b69cb431d1637e4948688d15791;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 f594e58..a40a543 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 David Roden
+ * Sone - PageToadlet.java - Copyright © 2010â2013 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,19 +18,23 @@
package net.pterodactylus.sone.web.page;
import java.io.IOException;
+import java.io.OutputStream;
import java.net.URI;
-import java.util.Map.Entry;
-import net.pterodactylus.sone.web.page.Page.Request.Method;
+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.Toadlet;
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContextClosedException;
import freenet.support.MultiValueTable;
import freenet.support.api.Bucket;
import freenet.support.api.HTTPRequest;
-import freenet.support.io.BucketTools;
import freenet.support.io.Closer;
/**
@@ -38,13 +42,13 @@ import freenet.support.io.Closer;
*
* @author David âBombeâ Roden
*/
-public class PageToadlet extends Toadlet implements LinkEnabledCallback {
+public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet {
/** The name of the menu item. */
private final String menuName;
/** The page that handles processing. */
- private final Page page;
+ private final Page page;
/** The path prefix for the page. */
private final String pathPrefix;
@@ -62,7 +66,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
* 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, String menuName, Page page, String pathPrefix) {
super(highLevelSimpleClient);
this.menuName = menuName;
this.page = page;
@@ -101,7 +105,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
* if the toadlet context is closed
*/
public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
- handleRequest(new Page.Request(uri, Method.GET, httpRequest, toadletContext));
+ handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext));
}
/**
@@ -119,7 +123,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
* if the toadlet context is closed
*/
public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
- handleRequest(new Page.Request(uri, Method.POST, httpRequest, toadletContext));
+ handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext));
}
/**
@@ -140,32 +144,32 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
* @throws ToadletContextClosedException
* if the toadlet context is closed
*/
- private void handleRequest(Page.Request pageRequest) throws IOException, ToadletContextClosedException {
- Bucket data = null;
+ private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException {
+ Bucket pageBucket = null;
+ OutputStream pageBucketOutputStream = null;
+ Response pageResponse;
try {
- Page.Response pageResponse = page.handleRequest(pageRequest);
- MultiValueTable headers = new MultiValueTable();
- if (pageResponse.getHeaders() != null) {
- for (Entry headerEntry : pageResponse.getHeaders().entrySet()) {
- headers.put(headerEntry.getKey(), headerEntry.getValue());
- }
- }
- data = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1);
- if (pageResponse.getContent() != null) {
- try {
- BucketTools.copyFrom(data, pageResponse.getContent(), -1);
- } finally {
- Closer.close(pageResponse.getContent());
+ 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);
}
- } else {
- /* get an OutputStream and close it immediately. */
- Closer.close(data.getOutputStream());
}
- writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, data);
- } catch (Throwable t1) {
- writeInternalError(t1, pageRequest.getToadletContext());
+ }
+ try {
+ writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket);
} finally {
- Closer.close(data);
+ Closer.close(pageBucket);
}
}
@@ -180,4 +184,16 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
return true;
}
+ //
+ // LINKFILTEREXCEPTEDTOADLET METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isLinkExcepted(URI link) {
+ return (page instanceof FreenetPage) ? ((FreenetPage) page).isLinkExcepted(link) : false;
+ }
+
}