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