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