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