🚚 Move Kotlin files to correct source path
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / page / PageToadlet.kt
1 /*
2  * Sone - PageToadlet.kt - 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 freenet.client.HighLevelSimpleClient
21 import freenet.clients.http.LinkEnabledCallback
22 import freenet.clients.http.LinkFilterExceptedToadlet
23 import freenet.clients.http.SessionManager
24 import freenet.clients.http.Toadlet
25 import freenet.clients.http.ToadletContext
26 import freenet.support.MultiValueTable
27 import freenet.support.api.HTTPRequest
28 import net.pterodactylus.sone.utils.use
29 import net.pterodactylus.util.web.Method
30 import net.pterodactylus.util.web.Page
31 import net.pterodactylus.util.web.Response
32 import java.net.URI
33
34 /**
35  * [Toadlet] implementation that is wrapped around a [Page].
36  */
37 class PageToadlet(
38                 highLevelSimpleClient: HighLevelSimpleClient,
39                 private val sessionManager: SessionManager,
40                 val menuName: String?,
41                 private val page: Page<FreenetRequest>,
42                 private val pathPrefix: String
43 ) : Toadlet(highLevelSimpleClient), LinkEnabledCallback, LinkFilterExceptedToadlet {
44
45         override fun path() = pathPrefix + page.path
46
47         override fun handleMethodGET(uri: URI, httpRequest: HTTPRequest, toadletContext: ToadletContext) =
48                         handleRequest(FreenetRequest(uri, Method.GET, httpRequest, toadletContext, sessionManager))
49
50         fun handleMethodPOST(uri: URI?, httpRequest: HTTPRequest?, toadletContext: ToadletContext?) =
51                         handleRequest(FreenetRequest(uri!!, Method.POST, httpRequest!!, toadletContext!!, sessionManager))
52
53         private fun handleRequest(pageRequest: FreenetRequest) {
54                 pageRequest.toadletContext.bucketFactory.makeBucket(-1).use { pageBucket ->
55                         pageBucket.outputStream.use { pageBucketOutputStream ->
56                                 val pageResponse = page.handleRequest(pageRequest, Response(pageBucketOutputStream))
57                                 // according to the javadoc, headers is allowed to return null but that’s stupid and it doesn’t do that.
58                                 val headers = pageResponse.headers.fold(MultiValueTable<String, String>()) { headers, header ->
59                                         headers.apply {
60                                                 header.forEach { put(header.name, it) }
61                                         }
62                                 }
63                                 with(pageResponse) {
64                                         writeReply(pageRequest.toadletContext, statusCode, contentType, statusText, headers, pageBucket)
65                                 }
66                         }
67                 }
68         }
69
70         override fun isEnabled(toadletContext: ToadletContext) =
71                         if (page is LinkEnabledCallback) {
72                                 page.isEnabled(toadletContext)
73                         } else
74                                 true
75
76         override fun isLinkExcepted(link: URI) =
77                         page is FreenetPage && page.isLinkExcepted(link)
78
79         override fun toString() = "${javaClass.name}[path=${path()},page=$page]"
80
81 }