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

Thu, 13 May 2021 11:28:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 13 May 2021 11:28:50 +0200
changeset 195
9c7aff3cbb14
parent 184
e8eecee6aadf
child 198
94f174d591ab
permissions
-rw-r--r--

#109 - add RSS feed

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@184 31 import javax.servlet.http.HttpServletRequest
universe@184 32 import javax.servlet.http.HttpServletResponse
universe@184 33 import javax.servlet.http.HttpSession
universe@179 34 import kotlin.math.min
universe@179 35
universe@184 36 typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
universe@184 37 typealias PathParameters = Map<String, String>
universe@184 38
universe@184 39 class HttpRequest(
universe@184 40 val request: HttpServletRequest,
universe@184 41 val response: HttpServletResponse,
universe@184 42 val pathParams: PathParameters = emptyMap()
universe@184 43 ) {
universe@184 44 val session: HttpSession = request.session
universe@184 45
universe@184 46 val remoteUser: String? = request.remoteUser
universe@179 47
universe@179 48 /**
universe@184 49 * The name of the content page.
universe@179 50 *
universe@184 51 * @see Constants#REQ_ATTR_CONTENT_PAGE
universe@179 52 */
universe@184 53 var contentPage = ""
universe@184 54 set(value) {
universe@184 55 field = value
universe@184 56 request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value))
universe@184 57 }
universe@179 58
universe@179 59 /**
universe@184 60 * A list of additional style sheets.
universe@179 61 *
universe@184 62 * @see Constants#REQ_ATTR_STYLESHEET
universe@179 63 */
universe@184 64 var styleSheets = emptyList<String>()
universe@184 65 set(value) {
universe@184 66 field = value
universe@184 67 request.setAttribute(Constants.REQ_ATTR_STYLESHEET,
universe@184 68 value.map { it.withExt(".css") }
universe@184 69 )
universe@184 70 }
universe@179 71
universe@184 72 /**
universe@184 73 * The name of the navigation menu JSP.
universe@184 74 *
universe@184 75 * @see Constants#REQ_ATTR_NAVIGATION
universe@184 76 */
universe@184 77 var navigationMenu: NavMenu? = null
universe@184 78 set(value) {
universe@184 79 field = value
universe@184 80 request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu)
universe@184 81 }
universe@184 82
universe@184 83 var redirectLocation = ""
universe@184 84 set(value) {
universe@184 85 field = value
universe@184 86 request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value)
universe@184 87 }
universe@184 88
universe@184 89 /**
universe@184 90 * The view object.
universe@184 91 *
universe@184 92 * @see Constants#REQ_ATTR_VIEWMODEL
universe@184 93 */
universe@184 94 var view: View? = null
universe@184 95 set(value) {
universe@184 96 field = value
universe@184 97 request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value)
universe@184 98 }
universe@184 99
universe@184 100 /**
universe@184 101 * The base path of this application.
universe@184 102 */
universe@184 103 val baseHref get() = "${request.scheme}://${request.serverName}:${request.serverPort}${request.contextPath}/"
universe@184 104
universe@184 105 private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext)
universe@184 106 private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp")
universe@184 107
universe@184 108 fun param(name: String): String? = request.getParameter(name)
universe@184 109 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray()
universe@184 110
universe@195 111 fun forward(jsp: String) {
universe@195 112 request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
universe@195 113 }
universe@195 114
universe@184 115 fun render(page: String? = null) {
universe@184 116 page?.let { contentPage = it }
universe@195 117 forward("site")
universe@184 118 }
universe@184 119
universe@184 120 fun renderCommit(location: String? = null) {
universe@184 121 location?.let { redirectLocation = it }
universe@184 122 contentPage = Constants.JSP_COMMIT_SUCCESSFUL
universe@184 123 render()
universe@184 124 }
universe@184 125 }
universe@179 126
universe@179 127 /**
universe@179 128 * A path pattern optionally containing placeholders.
universe@179 129 *
universe@179 130 * The special directories . and .. are disallowed in the pattern.
universe@184 131 * Placeholders start with a % sign.
universe@179 132 *
universe@179 133 * @param pattern the pattern
universe@179 134 */
universe@179 135 class PathPattern(pattern: String) {
universe@179 136 private val nodePatterns: List<String>
universe@179 137 private val collection: Boolean
universe@179 138
universe@179 139 private fun parse(pattern: String): List<String> {
universe@179 140 val nodes = pattern.split("/").filter { it.isNotBlank() }.toList()
universe@179 141 require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." }
universe@179 142 return nodes
universe@179 143 }
universe@179 144
universe@179 145 /**
universe@179 146 * Matches a path against this pattern.
universe@179 147 * The path must be canonical in the sense that no . or .. parts occur.
universe@179 148 *
universe@179 149 * @param path the path to match
universe@179 150 * @return true if the path matches the pattern, false otherwise
universe@179 151 */
universe@179 152 fun matches(path: String): Boolean {
universe@179 153 if (collection xor path.endsWith("/")) return false
universe@179 154 val nodes = parse(path)
universe@179 155 if (nodePatterns.size != nodes.size) return false
universe@179 156 for (i in nodePatterns.indices) {
universe@179 157 val pattern = nodePatterns[i]
universe@179 158 val node = nodes[i]
universe@184 159 if (pattern.startsWith("%")) continue
universe@179 160 if (pattern != node) return false
universe@179 161 }
universe@179 162 return true
universe@179 163 }
universe@179 164
universe@179 165 /**
universe@179 166 * Returns the path parameters found in the specified path using this pattern.
universe@179 167 * The return value of this method is undefined, if the patter does not match.
universe@179 168 *
universe@179 169 * @param path the path
universe@179 170 * @return the path parameters, if any, or an empty map
universe@179 171 * @see .matches
universe@179 172 */
universe@179 173 fun obtainPathParameters(path: String): PathParameters {
universe@184 174 val params = mutableMapOf<String, String>()
universe@179 175 val nodes = parse(path)
universe@179 176 for (i in 0 until min(nodes.size, nodePatterns.size)) {
universe@179 177 val pattern = nodePatterns[i]
universe@179 178 val node = nodes[i]
universe@184 179 if (pattern.startsWith("%")) {
universe@179 180 params[pattern.substring(1)] = node
universe@179 181 }
universe@179 182 }
universe@179 183 return params
universe@179 184 }
universe@179 185
universe@179 186 override fun hashCode(): Int {
universe@179 187 val str = StringBuilder()
universe@179 188 for (node in nodePatterns) {
universe@184 189 if (node.startsWith("%")) {
universe@184 190 str.append("/%")
universe@179 191 } else {
universe@179 192 str.append('/')
universe@179 193 str.append(node)
universe@179 194 }
universe@179 195 }
universe@179 196 if (collection) str.append('/')
universe@179 197 return str.toString().hashCode()
universe@179 198 }
universe@179 199
universe@179 200 override fun equals(other: Any?): Boolean {
universe@179 201 if (other is PathPattern) {
universe@179 202 if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false
universe@179 203 for (i in nodePatterns.indices) {
universe@179 204 val left = nodePatterns[i]
universe@179 205 val right = other.nodePatterns[i]
universe@184 206 if (left.startsWith("%") && right.startsWith("%")) continue
universe@179 207 if (left != right) return false
universe@179 208 }
universe@179 209 return true
universe@179 210 } else {
universe@179 211 return false
universe@179 212 }
universe@179 213 }
universe@179 214
universe@179 215 init {
universe@179 216 nodePatterns = parse(pattern)
universe@179 217 collection = pattern.endsWith("/")
universe@179 218 }
universe@179 219 }
universe@179 220

mercurial