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

Mon, 05 Apr 2021 17:56:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 05 Apr 2021 17:56:46 +0200
changeset 187
0bf35031a918
parent 184
e8eecee6aadf
child 195
9c7aff3cbb14
permissions
-rw-r--r--

project name and node may have 50 chars - fixes #118

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
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
31 import javax.servlet.http.HttpServletRequest
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
32 import javax.servlet.http.HttpServletResponse
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
33 import javax.servlet.http.HttpSession
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import kotlin.math.min
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
36 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
37 typealias PathParameters = Map<String, String>
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
38
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
39 class HttpRequest(
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
40 val request: HttpServletRequest,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
41 val response: HttpServletResponse,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
42 val pathParams: PathParameters = emptyMap()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
43 ) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
44 val session: HttpSession = request.session
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
45
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
46 val remoteUser: String? = request.remoteUser
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
47
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 * The name of the content page.
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 * @see Constants#REQ_ATTR_CONTENT_PAGE
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 var contentPage = ""
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
54 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
55 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
56 request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value))
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
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 * A list of additional style sheets.
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
61 *
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
62 * @see Constants#REQ_ATTR_STYLESHEET
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 var styleSheets = emptyList<String>()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
65 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
66 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
67 request.setAttribute(Constants.REQ_ATTR_STYLESHEET,
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
68 value.map { it.withExt(".css") }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
69 )
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
70 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
73 * 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
74 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
75 * @see Constants#REQ_ATTR_NAVIGATION
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 */
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
77 var navigationMenu: NavMenu? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
78 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
79 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
80 request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu)
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
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
83 var redirectLocation = ""
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
84 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
85 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
86 request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
87 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 /**
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
90 * The view object.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 *
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
92 * @see Constants#REQ_ATTR_VIEWMODEL
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 var view: View? = null
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
95 set(value) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
96 field = value
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
97 request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
98 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
99
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
100 /**
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
101 * The base path of this application.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 */
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
103 val baseHref get() = "${request.scheme}://${request.serverName}:${request.serverPort}${request.contextPath}/"
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
104
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
105 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
106 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
107
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
108 fun param(name: String): String? = request.getParameter(name)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
109 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
110
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
111 fun render(page: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
112 page?.let { contentPage = it }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
113 request.getRequestDispatcher(jspPath("site")).forward(request, response)
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
114 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
115
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
116 fun renderCommit(location: String? = null) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
117 location?.let { redirectLocation = it }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
118 contentPage = Constants.JSP_COMMIT_SUCCESSFUL
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
119 render()
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
120 }
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
121 }
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 * A path pattern optionally containing placeholders.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 * The special directories . and .. are disallowed in the pattern.
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
127 * Placeholders start with a % sign.
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 * @param pattern the pattern
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 class PathPattern(pattern: String) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 private val nodePatterns: List<String>
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 private val collection: Boolean
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 private fun parse(pattern: String): List<String> {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 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
137 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
138 return nodes
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 * Matches a path against this pattern.
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 * 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
144 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 * @param path the path to match
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 * @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
147 */
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 fun matches(path: String): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 if (collection xor path.endsWith("/")) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 if (nodePatterns.size != nodes.size) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
155 if (pattern.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 if (pattern != node) return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 /**
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 * 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
163 * 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
164 *
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 * @param path the path
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 * @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
167 * @see .matches
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 fun obtainPathParameters(path: String): PathParameters {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
170 val params = mutableMapOf<String, String>()
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 val nodes = parse(path)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 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
173 val pattern = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 val node = nodes[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
175 if (pattern.startsWith("%")) {
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 params[pattern.substring(1)] = node
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 return params
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 override fun hashCode(): Int {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 val str = StringBuilder()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 for (node in nodePatterns) {
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
185 if (node.startsWith("%")) {
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
186 str.append("/%")
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 str.append(node)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 if (collection) str.append('/')
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 return str.toString().hashCode()
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 }
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 override fun equals(other: Any?): Boolean {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 if (other is PathPattern) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 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
199 for (i in nodePatterns.indices) {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 val left = nodePatterns[i]
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 val right = other.nodePatterns[i]
184
e8eecee6aadf completes kotlin migration
Mike Becker <universe@uap-core.de>
parents: 179
diff changeset
202 if (left.startsWith("%") && right.startsWith("%")) continue
179
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 if (left != right) return false
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 return true
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 } else {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 return false
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 }
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
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 init {
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 nodePatterns = parse(pattern)
623c340058f3 migrates the utility classes for the AbstractServlet
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 collection = pattern.endsWith("/")
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

mercurial