X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FPageToadlet.java;h=d919bd75f5cef68805cefb1562961db9805c4d51;hb=7708df5b6f4d1cf1416a8202b825a4f6f78f289e;hp=57a34395ee78f4855bfd05f795de2ffd294d206f;hpb=13bc2f6ae2379dd9f81edb4848971c8b4e91aabd;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 57a3439..d919bd7 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 @@
/*
- * shortener - 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,41 +18,46 @@
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;
/**
* {@link Toadlet} implementation that is wrapped around a {@link Page}.
- *
+ *
* @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;
/**
* Creates a new toadlet that hands off processing to a {@link Page}.
- *
+ *
* @param highLevelSimpleClient
+ * The high-level simple client
* @param menuName
* The name of the menu item
* @param page
@@ -61,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;
@@ -70,16 +75,13 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
/**
* Returns the name to display in the menu.
- *
+ *
* @return The name in the menu
*/
public String getMenuName() {
return menuName;
}
- /**
- * {@inheritDoc}
- */
@Override
public String path() {
return pathPrefix + page.getPath();
@@ -87,7 +89,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
/**
* Handles a HTTP GET request.
- *
+ *
* @param uri
* The URI of the request
* @param httpRequest
@@ -100,12 +102,12 @@ 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));
}
/**
* Handles a HTTP POST request.
- *
+ *
* @param uri
* The URI of the request
* @param httpRequest
@@ -118,12 +120,9 @@ 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));
}
- /**
- * {@inheritDoc}
- */
@Override
public String toString() {
return getClass().getName() + "[path=" + path() + ",page=" + page + "]";
@@ -131,7 +130,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback {
/**
* Handles a HTTP request.
- *
+ *
* @param pageRequest
* The request to handle
* @throws IOException
@@ -139,41 +138,50 @@ 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);
}
}
- /**
- * {@inheritDoc}
- */
@Override
public boolean isEnabled(ToadletContext toadletContext) {
+ if (page instanceof LinkEnabledCallback) {
+ return ((LinkEnabledCallback) page).isEnabled(toadletContext);
+ }
return true;
}
+ //
+ // LINKFILTEREXCEPTEDTOADLET METHODS
+ //
+
+ @Override
+ public boolean isLinkExcepted(URI link) {
+ return (page instanceof FreenetPage) ? ((FreenetPage) page).isLinkExcepted(link) : false;
+ }
+
}