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