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

Tue, 10 Aug 2021 18:18:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Aug 2021 18:18:06 +0200
changeset 220
1f7e76a213dd
parent 210
37fbdcb422b7
child 225
87328572e36f
permissions
-rw-r--r--

#157 adds assignee column to issue list

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
209
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
40 sealed interface ValidationResult<T>
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
41 class ValidationError<T>(val message: String): ValidationResult<T>
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
42 class ValidatedValue<T>(val result: T): ValidationResult<T>
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
43
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
44 class HttpRequest(
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
45 val request: HttpServletRequest,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
46 val response: HttpServletResponse,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
47 val pathParams: PathParameters = emptyMap()
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 val session: HttpSession = request.session
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
50
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
51 val remoteUser: String? = request.remoteUser
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
52
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 * The name of the content page.
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
55 *
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
56 * @see Constants#REQ_ATTR_CONTENT_PAGE
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
57 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
58 var contentPage = ""
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
59 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
60 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
61 request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value))
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
62 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
63
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
64 /**
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
65 * The name of the content page.
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
66 *
207
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
67 * @see Constants#REQ_ATTR_PAGE_TITLE
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
68 */
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
69 var pageTitle = ""
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
70 set(value) {
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
71 field = value
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
72 request.setAttribute(Constants.REQ_ATTR_PAGE_TITLE, value)
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
73 }
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
74
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
75 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
76 * A list of additional style sheets.
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
77 *
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
78 * @see Constants#REQ_ATTR_STYLESHEET
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
79 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
80 var styleSheets = emptyList<String>()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
81 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
82 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
83 request.setAttribute(Constants.REQ_ATTR_STYLESHEET,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
84 value.map { it.withExt(".css") }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
85 )
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
86 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 /**
207
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
89 * A list of additional style sheets.
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
90 *
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
91 * @see Constants#REQ_ATTR_JAVASCRIPT
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
92 */
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
93 var javascript = ""
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
94 set(value) {
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
95 field = value
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
96 request.setAttribute(Constants.REQ_ATTR_JAVASCRIPT,
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
97 value.withExt(".js")
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
98 )
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
99 }
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
100
479dd7993ef9 #22 adds possibility to edit own comments
Mike Becker <universe@uap-core.de>
parents: 205
diff changeset
101 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
102 * 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
103 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
104 * @see Constants#REQ_ATTR_NAVIGATION
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 */
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
106 var navigationMenu: NavMenu? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
107 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
108 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
109 request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
110 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
111
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
112 var redirectLocation: String? = null
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
113 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
114 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
115 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
116 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
117 } 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
118 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
119 }
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
120 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121
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
122 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
123 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
124 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
125 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
126 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
127 } 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
128 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
129 }
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
130 }
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
131
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
133 * The view object.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
135 * @see Constants#REQ_ATTR_VIEWMODEL
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
136 */
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
137 var view: View? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
138 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
139 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
140 request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value)
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
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
143 /**
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
144 * 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
145 */
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
146 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
147 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
148 || (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
149 ) "" 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
150
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
151 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
152 * The base path of this application.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 */
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
154 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
155
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
156 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
157 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
158
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
159 fun param(name: String): String? = request.getParameter(name)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
160 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
161
210
37fbdcb422b7 minor code changes
Mike Becker <universe@uap-core.de>
parents: 209
diff changeset
162 fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>),
37fbdcb422b7 minor code changes
Mike Becker <universe@uap-core.de>
parents: 209
diff changeset
163 defaultValue: T, errorMessages: MutableList<String>): T {
209
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
164 return when (val result = validator(param(name))) {
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
165 is ValidationError -> {
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
166 errorMessages.add(i18n(result.message))
210
37fbdcb422b7 minor code changes
Mike Becker <universe@uap-core.de>
parents: 209
diff changeset
167 defaultValue
209
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
168 }
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
169 is ValidatedValue -> {
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
170 result.result
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
171 }
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
172 }
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
173 }
c9c6abf167c7 #21 adds input validation mechanism
Mike Becker <universe@uap-core.de>
parents: 207
diff changeset
174
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
175 private fun forward(jsp: String) {
195
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
176 request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
177 }
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
178
198
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
179 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
180 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
181 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
182 }
94f174d591ab fixes wrong handling of feeds - only one channel per feed is allowed
Mike Becker <universe@uap-core.de>
parents: 195
diff changeset
183
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
184 fun render(page: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
185 page?.let { contentPage = it }
195
9c7aff3cbb14 #109 - add RSS feed
Mike Becker <universe@uap-core.de>
parents: 184
diff changeset
186 forward("site")
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
187 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
188
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
189 fun renderCommit(location: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
190 location?.let { redirectLocation = it }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
191 contentPage = Constants.JSP_COMMIT_SUCCESSFUL
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
192 render()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
193 }
205
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
194
7725a79416f3 #115 adds custom page titles
Mike Becker <universe@uap-core.de>
parents: 199
diff changeset
195 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
196 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 * A path pattern optionally containing placeholders.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 * The special directories . and .. are disallowed in the pattern.
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
202 * Placeholders start with a % sign.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 * @param pattern the pattern
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 class PathPattern(pattern: String) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 private val nodePatterns: List<String>
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 private val collection: Boolean
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 private fun parse(pattern: String): List<String> {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 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
212 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
213 return nodes
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 * Matches a path against this pattern.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 * 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
219 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 * @param path the path to match
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 * @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
222 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 fun matches(path: String): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 if (collection xor path.endsWith("/")) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 if (nodePatterns.size != nodes.size) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
230 if (pattern.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 if (pattern != node) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 }
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 * 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
238 * 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
239 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 * @param path the path
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 * @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
242 * @see .matches
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 fun obtainPathParameters(path: String): PathParameters {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
245 val params = mutableMapOf<String, String>()
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 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
248 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
250 if (pattern.startsWith("%")) {
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 params[pattern.substring(1)] = node
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 }
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 return params
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
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 override fun hashCode(): Int {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 val str = StringBuilder()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 for (node in nodePatterns) {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
260 if (node.startsWith("%")) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
261 str.append("/%")
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 str.append(node)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 if (collection) str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 return str.toString().hashCode()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 override fun equals(other: Any?): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 if (other is PathPattern) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 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
274 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 val left = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 val right = other.nodePatterns[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
277 if (left.startsWith("%") && right.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 if (left != right) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286 init {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 nodePatterns = parse(pattern)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 collection = pattern.endsWith("/")
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291

mercurial