X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Fweb%2FPageToadlet.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fwotns%2Fweb%2FPageToadlet.java;h=818a27ce03004c346b9c71b98ba4757bc994eb2b;hb=622c4a4d3ebed447d5708a41cf3e1e82e18fa29b;hp=0000000000000000000000000000000000000000;hpb=77869390c46e8e5eff63bf00c7ef44a37ba8f317;p=WoTNS.git diff --git a/src/main/java/net/pterodactylus/wotns/web/PageToadlet.java b/src/main/java/net/pterodactylus/wotns/web/PageToadlet.java new file mode 100644 index 0000000..818a27c --- /dev/null +++ b/src/main/java/net/pterodactylus/wotns/web/PageToadlet.java @@ -0,0 +1,177 @@ +/* + * Sone - PageToadlet.java - Copyright © 2010 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.wotns.web; + +import java.io.IOException; +import java.net.URI; + +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.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.Closer; + +/** + * {@link Toadlet} implementation that is wrapped around a {@link Page}. + * + * @author David ‘Bombe’ Roden + */ +public class PageToadlet extends Toadlet implements LinkEnabledCallback { + + /** The name of the menu item. */ + private final String menuName; + + /** The page that handles processing. */ + 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 + * The page to handle processing + * @param pathPrefix + * Prefix that is prepended to all {@link Page#getPath()} return + * values + */ + protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page page, String pathPrefix) { + super(highLevelSimpleClient); + this.menuName = menuName; + this.page = page; + this.pathPrefix = pathPrefix; + } + + /** + * 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(); + } + + /** + * Handles a HTTP GET request. + * + * @param uri + * The URI of the request + * @param httpRequest + * The HTTP request + * @param toadletContext + * The toadlet context + * @throws IOException + * if an I/O error occurs + * @throws ToadletContextClosedException + * 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)); + } + + /** + * Handles a HTTP POST request. + * + * @param uri + * The URI of the request + * @param httpRequest + * The HTTP request + * @param toadletContext + * The toadlet context + * @throws IOException + * if an I/O error occurs + * @throws ToadletContextClosedException + * 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)); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return getClass().getName() + "[path=" + path() + ",page=" + page + "]"; + } + + /** + * Handles a HTTP request. + * + * @param pageRequest + * The request to handle + * @throws IOException + * if an I/O error occurs + * @throws ToadletContextClosedException + * 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(); + if (pageResponse.getHeaders() != null) { + for (Header header : pageResponse.getHeaders()) { + for (String value : header) { + headers.put(header.getName(), value); + } + } + } + writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket); + } catch (Throwable t1) { + writeInternalError(t1, pageRequest.getToadletContext()); + } finally { + Closer.close(pageBucket); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isEnabled(ToadletContext toadletContext) { + if (page instanceof LinkEnabledCallback) { + return ((LinkEnabledCallback) page).isEnabled(toadletContext); + } + return true; + } + +}