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