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

Tue, 03 Aug 2021 12:22:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 03 Aug 2021 12:22:10 +0200
changeset 208
785820da6485
parent 207
479dd7993ef9
child 209
c9c6abf167c7
permissions
-rw-r--r--

fixes response locale not set for new sessions

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

mercurial