2 * Sone - PageToadlet.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web.page;
20 import java.io.IOException;
21 import java.io.OutputStream;
24 import net.pterodactylus.util.web.Header;
25 import net.pterodactylus.util.web.Method;
26 import net.pterodactylus.util.web.Page;
27 import net.pterodactylus.util.web.Response;
29 import freenet.client.HighLevelSimpleClient;
30 import freenet.clients.http.LinkEnabledCallback;
31 import freenet.clients.http.LinkFilterExceptedToadlet;
32 import freenet.clients.http.Toadlet;
33 import freenet.clients.http.ToadletContext;
34 import freenet.clients.http.ToadletContextClosedException;
35 import freenet.support.MultiValueTable;
36 import freenet.support.api.Bucket;
37 import freenet.support.api.HTTPRequest;
38 import freenet.support.io.Closer;
41 * {@link Toadlet} implementation that is wrapped around a {@link Page}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFilterExceptedToadlet {
47 /** The name of the menu item. */
48 private final String menuName;
50 /** The page that handles processing. */
51 private final Page<FreenetRequest> page;
53 /** The path prefix for the page. */
54 private final String pathPrefix;
57 * Creates a new toadlet that hands off processing to a {@link Page}.
59 * @param highLevelSimpleClient
60 * The high-level simple client
62 * The name of the menu item
64 * The page to handle processing
66 * Prefix that is prepended to all {@link Page#getPath()} return
69 protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page<FreenetRequest> page, String pathPrefix) {
70 super(highLevelSimpleClient);
71 this.menuName = menuName;
73 this.pathPrefix = pathPrefix;
77 * Returns the name to display in the menu.
79 * @return The name in the menu
81 public String getMenuName() {
86 public String path() {
87 return pathPrefix + page.getPath();
91 * Handles a HTTP GET request.
94 * The URI of the request
97 * @param toadletContext
100 * if an I/O error occurs
101 * @throws ToadletContextClosedException
102 * if the toadlet context is closed
104 public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
105 handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext));
109 * Handles a HTTP POST request.
112 * The URI of the request
115 * @param toadletContext
116 * The toadlet context
117 * @throws IOException
118 * if an I/O error occurs
119 * @throws ToadletContextClosedException
120 * if the toadlet context is closed
122 public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
123 handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext));
127 public String toString() {
128 return getClass().getName() + "[path=" + path() + ",page=" + page + "]";
132 * Handles a HTTP request.
135 * The request to handle
136 * @throws IOException
137 * if an I/O error occurs
138 * @throws ToadletContextClosedException
139 * if the toadlet context is closed
141 private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException {
142 Bucket pageBucket = null;
143 OutputStream pageBucketOutputStream = null;
144 Response pageResponse;
146 pageBucket = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1);
147 pageBucketOutputStream = pageBucket.getOutputStream();
148 pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream));
149 } catch (IOException ioe1) {
150 Closer.close(pageBucket);
153 Closer.close(pageBucketOutputStream);
155 MultiValueTable<String, String> headers = new MultiValueTable<String, String>();
156 if (pageResponse.getHeaders() != null) {
157 for (Header header : pageResponse.getHeaders()) {
158 for (String value : header) {
159 headers.put(header.getName(), value);
164 writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket);
166 Closer.close(pageBucket);
171 public boolean isEnabled(ToadletContext toadletContext) {
172 if (page instanceof LinkEnabledCallback) {
173 return ((LinkEnabledCallback) page).isEnabled(toadletContext);
179 // LINKFILTEREXCEPTEDTOADLET METHODS
183 public boolean isLinkExcepted(URI link) {
184 return (page instanceof FreenetPage) ? ((FreenetPage) page).isLinkExcepted(link) : false;