First extremely basic, kind-of-working version.
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / web / PageToadlet.java
1 /*
2  * Sone - PageToadlet.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.wotns.web;
19
20 import java.io.IOException;
21 import java.net.URI;
22
23 import net.pterodactylus.util.web.Header;
24 import net.pterodactylus.util.web.Method;
25 import net.pterodactylus.util.web.Page;
26 import net.pterodactylus.util.web.Response;
27 import freenet.client.HighLevelSimpleClient;
28 import freenet.clients.http.LinkEnabledCallback;
29 import freenet.clients.http.Toadlet;
30 import freenet.clients.http.ToadletContext;
31 import freenet.clients.http.ToadletContextClosedException;
32 import freenet.support.MultiValueTable;
33 import freenet.support.api.Bucket;
34 import freenet.support.api.HTTPRequest;
35 import freenet.support.io.Closer;
36
37 /**
38  * {@link Toadlet} implementation that is wrapped around a {@link Page}.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class PageToadlet extends Toadlet implements LinkEnabledCallback {
43
44         /** The name of the menu item. */
45         private final String menuName;
46
47         /** The page that handles processing. */
48         private final Page<FreenetRequest> page;
49
50         /** The path prefix for the page. */
51         private final String pathPrefix;
52
53         /**
54          * Creates a new toadlet that hands off processing to a {@link Page}.
55          *
56          * @param highLevelSimpleClient
57          *            The high-level simple client
58          * @param menuName
59          *            The name of the menu item
60          * @param page
61          *            The page to handle processing
62          * @param pathPrefix
63          *            Prefix that is prepended to all {@link Page#getPath()} return
64          *            values
65          */
66         protected PageToadlet(HighLevelSimpleClient highLevelSimpleClient, String menuName, Page<FreenetRequest> page, String pathPrefix) {
67                 super(highLevelSimpleClient);
68                 this.menuName = menuName;
69                 this.page = page;
70                 this.pathPrefix = pathPrefix;
71         }
72
73         /**
74          * Returns the name to display in the menu.
75          *
76          * @return The name in the menu
77          */
78         public String getMenuName() {
79                 return menuName;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public String path() {
87                 return pathPrefix + page.getPath();
88         }
89
90         /**
91          * Handles a HTTP GET request.
92          *
93          * @param uri
94          *            The URI of the request
95          * @param httpRequest
96          *            The HTTP request
97          * @param toadletContext
98          *            The toadlet context
99          * @throws IOException
100          *             if an I/O error occurs
101          * @throws ToadletContextClosedException
102          *             if the toadlet context is closed
103          */
104         public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
105                 handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext));
106         }
107
108         /**
109          * Handles a HTTP POST request.
110          *
111          * @param uri
112          *            The URI of the request
113          * @param httpRequest
114          *            The HTTP 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
121          */
122         public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException {
123                 handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext));
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         public String toString() {
131                 return getClass().getName() + "[path=" + path() + ",page=" + page + "]";
132         }
133
134         /**
135          * Handles a HTTP request.
136          *
137          * @param pageRequest
138          *            The request to handle
139          * @throws IOException
140          *             if an I/O error occurs
141          * @throws ToadletContextClosedException
142          *             if the toadlet context is closed
143          */
144         private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException {
145                 Bucket pageBucket = null;
146                 try {
147                         pageBucket = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1);
148                         Response pageResponse = new Response(pageBucket.getOutputStream());
149                         pageResponse = page.handleRequest(pageRequest, pageResponse);
150                         MultiValueTable<String, String> headers = new MultiValueTable<String, String>();
151                         if (pageResponse.getHeaders() != null) {
152                                 for (Header header : pageResponse.getHeaders()) {
153                                         for (String value : header) {
154                                                 headers.put(header.getName(), value);
155                                         }
156                                 }
157                         }
158                         writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket);
159                 } catch (Throwable t1) {
160                         writeInternalError(t1, pageRequest.getToadletContext());
161                 } finally {
162                         Closer.close(pageBucket);
163                 }
164         }
165
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public boolean isEnabled(ToadletContext toadletContext) {
171                 if (page instanceof LinkEnabledCallback) {
172                         return ((LinkEnabledCallback) page).isEnabled(toadletContext);
173                 }
174                 return true;
175         }
176
177 }