src/main/kotlin/de/uapcore/lightpit/RequestMapping.kt

Sun, 01 Aug 2021 18:56:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Aug 2021 18:56:28 +0200
changeset 205
7725a79416f3
parent 199
59393c8cc557
child 207
479dd7993ef9
permissions
-rw-r--r--

#115 adds custom page titles

179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * Copyright 2021 Mike Becker. All rights reserved.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Redistribution and use in source and binary forms, with or without
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * modification, are permitted provided that the following conditions are met:
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * 1. Redistributions of source code must retain the above copyright
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * notice, this list of conditions and the following disclaimer.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 package de.uapcore.lightpit
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
28 import de.uapcore.lightpit.dao.DataAccessObject
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
29 import de.uapcore.lightpit.viewmodel.NavMenu
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
30 import de.uapcore.lightpit.viewmodel.View
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
31 import java.util.*
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
32 import javax.servlet.http.HttpServletRequest
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
33 import javax.servlet.http.HttpServletResponse
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
34 import javax.servlet.http.HttpSession
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 import kotlin.math.min
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
37 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
38 typealias PathParameters = Map<String, String>
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
39
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
40 class HttpRequest(
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
41 val request: HttpServletRequest,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
42 val response: HttpServletResponse,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
43 val pathParams: PathParameters = emptyMap()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
44 ) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
45 val session: HttpSession = request.session
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
46
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
47 val remoteUser: String? = request.remoteUser
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
48
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
49 /**
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
50 * The name of the content page.
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
51 *
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
52 * @see Constants#REQ_ATTR_CONTENT_PAGE
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
53 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
54 var contentPage = ""
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
55 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
56 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
57 request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value))
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
58 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
59
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
60 /**
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
61 * The name of the content page.
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
62 *
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
63 * @see Constants#REQ_ATTR_CONTENT_PAGE
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
64 */
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
65 var pageTitle = ""
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
66 set(value) {
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
67 field = value
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
68 request.setAttribute(Constants.REQ_ATTR_PAGE_TITLE, value)
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
69 }
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
70
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
71 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
72 * A list of additional style sheets.
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
73 *
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
74 * @see Constants#REQ_ATTR_STYLESHEET
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
75 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
76 var styleSheets = emptyList<String>()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
77 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
78 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
79 request.setAttribute(Constants.REQ_ATTR_STYLESHEET,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
80 value.map { it.withExt(".css") }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
81 )
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
82 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
85 * The name of the navigation menu JSP.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
87 * @see Constants#REQ_ATTR_NAVIGATION
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 */
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
89 var navigationMenu: NavMenu? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
90 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
91 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
92 request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
93 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
94
199
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
95 var redirectLocation: String? = null
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
96 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
97 field = value
199
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
98 if (value == null) {
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
99 request.removeAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION)
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
100 } else {
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
101 request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value)
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
102 }
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
103 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
199
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
105 var feedPath: String? = null
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
106 set(value) {
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
107 field = value
199
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
108 if (value == null) {
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
109 request.removeAttribute(Constants.REQ_ATTR_FEED_HREF)
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
110 } else {
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
111 request.setAttribute(Constants.REQ_ATTR_FEED_HREF, baseHref + value)
59393c8cc557 #109 adds RSS feed button to project header and changes feed output slightly
Mike Becker <universe@uap-core.de>
parents: 198
diff changeset
112 }
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
113 }
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
114
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
116 * The view object.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
118 * @see Constants#REQ_ATTR_VIEWMODEL
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
119 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
120 var view: View? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
121 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
122 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
123 request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
124 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
125
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
126 /**
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
127 * Additional port info, if necessary.
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
128 */
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
129 private val portInfo =
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
130 if ((request.scheme == "http" && request.serverPort == 80)
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
131 || (request.scheme == "https" && request.serverPort == 443)
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
132 ) "" else ":${request.serverPort}"
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
133
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
134 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
135 * The base path of this application.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 */
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
137 val baseHref get() = "${request.scheme}://${request.serverName}$portInfo${request.contextPath}/"
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
138
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
139 private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
140 private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp")
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
141
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
142 fun param(name: String): String? = request.getParameter(name)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
143 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray()
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
145 private fun forward(jsp: String) {
195
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
146 request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
147 }
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
148
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
149 fun renderFeed(page: String? = null) {
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
150 page?.let { contentPage = it }
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
151 forward("feed")
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
152 }
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
153
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
154 fun render(page: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
155 page?.let { contentPage = it }
195
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
156 forward("site")
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
157 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
158
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
159 fun renderCommit(location: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
160 location?.let { redirectLocation = it }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
161 contentPage = Constants.JSP_COMMIT_SUCCESSFUL
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
162 render()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
163 }
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
164
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
165 fun i18n(key: String) = ResourceBundle.getBundle("localization/strings", response.locale).getString(key)
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
166 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 * A path pattern optionally containing placeholders.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 * The special directories . and .. are disallowed in the pattern.
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
172 * Placeholders start with a % sign.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 * @param pattern the pattern
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 class PathPattern(pattern: String) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 private val nodePatterns: List<String>
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 private val collection: Boolean
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 private fun parse(pattern: String): List<String> {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 val nodes = pattern.split("/").filter { it.isNotBlank() }.toList()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 return nodes
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 * Matches a path against this pattern.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 * The path must be canonical in the sense that no . or .. parts occur.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 * @param path the path to match
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 * @return true if the path matches the pattern, false otherwise
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 fun matches(path: String): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 if (collection xor path.endsWith("/")) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 if (nodePatterns.size != nodes.size) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
200 if (pattern.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 if (pattern != node) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 * Returns the path parameters found in the specified path using this pattern.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 * The return value of this method is undefined, if the patter does not match.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 * @param path the path
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 * @return the path parameters, if any, or an empty map
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 * @see .matches
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 fun obtainPathParameters(path: String): PathParameters {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
215 val params = mutableMapOf<String, String>()
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 for (i in 0 until min(nodes.size, nodePatterns.size)) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
220 if (pattern.startsWith("%")) {
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 params[pattern.substring(1)] = node
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 return params
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 override fun hashCode(): Int {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 val str = StringBuilder()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 for (node in nodePatterns) {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
230 if (node.startsWith("%")) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
231 str.append("/%")
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 str.append(node)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 if (collection) str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 return str.toString().hashCode()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 override fun equals(other: Any?): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 if (other is PathPattern) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 val left = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 val right = other.nodePatterns[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
247 if (left.startsWith("%") && right.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 if (left != right) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 init {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 nodePatterns = parse(pattern)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 collection = pattern.endsWith("/")
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261

mercurial