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

Thu, 13 May 2021 19:31:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 13 May 2021 19:31:09 +0200
changeset 198
94f174d591ab
parent 195
9c7aff3cbb14
child 199
59393c8cc557
permissions
-rw-r--r--

fixes wrong handling of feeds - only one channel per feed is allowed

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@198 89 var feedPath = ""
universe@198 90 set(value) {
universe@198 91 field = value
universe@198 92 request.setAttribute(Constants.REQ_ATTR_FEED_HREF, baseHref + value)
universe@198 93 }
universe@198 94
universe@184 95 /**
universe@184 96 * The view object.
universe@184 97 *
universe@184 98 * @see Constants#REQ_ATTR_VIEWMODEL
universe@184 99 */
universe@184 100 var view: View? = null
universe@184 101 set(value) {
universe@184 102 field = value
universe@184 103 request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value)
universe@184 104 }
universe@184 105
universe@184 106 /**
universe@198 107 * Additional port info, if necessary.
universe@198 108 */
universe@198 109 private val portInfo =
universe@198 110 if ((request.scheme == "http" && request.serverPort == 80)
universe@198 111 || (request.scheme == "https" && request.serverPort == 443)
universe@198 112 ) "" else ":${request.serverPort}"
universe@198 113
universe@198 114 /**
universe@184 115 * The base path of this application.
universe@184 116 */
universe@198 117 val baseHref get() = "${request.scheme}://${request.serverName}$portInfo${request.contextPath}/"
universe@198 118
universe@198 119 init {
universe@198 120 feedPath = "feed/projects.rss"
universe@198 121 }
universe@184 122
universe@184 123 private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext)
universe@184 124 private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp")
universe@184 125
universe@184 126 fun param(name: String): String? = request.getParameter(name)
universe@184 127 fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray()
universe@184 128
universe@198 129 private fun forward(jsp: String) {
universe@195 130 request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
universe@195 131 }
universe@195 132
universe@198 133 fun renderFeed(page: String? = null) {
universe@198 134 page?.let { contentPage = it }
universe@198 135 forward("feed")
universe@198 136 }
universe@198 137
universe@184 138 fun render(page: String? = null) {
universe@184 139 page?.let { contentPage = it }
universe@195 140 forward("site")
universe@184 141 }
universe@184 142
universe@184 143 fun renderCommit(location: String? = null) {
universe@184 144 location?.let { redirectLocation = it }
universe@184 145 contentPage = Constants.JSP_COMMIT_SUCCESSFUL
universe@184 146 render()
universe@184 147 }
universe@184 148 }
universe@179 149
universe@179 150 /**
universe@179 151 * A path pattern optionally containing placeholders.
universe@179 152 *
universe@179 153 * The special directories . and .. are disallowed in the pattern.
universe@184 154 * Placeholders start with a % sign.
universe@179 155 *
universe@179 156 * @param pattern the pattern
universe@179 157 */
universe@179 158 class PathPattern(pattern: String) {
universe@179 159 private val nodePatterns: List<String>
universe@179 160 private val collection: Boolean
universe@179 161
universe@179 162 private fun parse(pattern: String): List<String> {
universe@179 163 val nodes = pattern.split("/").filter { it.isNotBlank() }.toList()
universe@179 164 require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." }
universe@179 165 return nodes
universe@179 166 }
universe@179 167
universe@179 168 /**
universe@179 169 * Matches a path against this pattern.
universe@179 170 * The path must be canonical in the sense that no . or .. parts occur.
universe@179 171 *
universe@179 172 * @param path the path to match
universe@179 173 * @return true if the path matches the pattern, false otherwise
universe@179 174 */
universe@179 175 fun matches(path: String): Boolean {
universe@179 176 if (collection xor path.endsWith("/")) return false
universe@179 177 val nodes = parse(path)
universe@179 178 if (nodePatterns.size != nodes.size) return false
universe@179 179 for (i in nodePatterns.indices) {
universe@179 180 val pattern = nodePatterns[i]
universe@179 181 val node = nodes[i]
universe@184 182 if (pattern.startsWith("%")) continue
universe@179 183 if (pattern != node) return false
universe@179 184 }
universe@179 185 return true
universe@179 186 }
universe@179 187
universe@179 188 /**
universe@179 189 * Returns the path parameters found in the specified path using this pattern.
universe@179 190 * The return value of this method is undefined, if the patter does not match.
universe@179 191 *
universe@179 192 * @param path the path
universe@179 193 * @return the path parameters, if any, or an empty map
universe@179 194 * @see .matches
universe@179 195 */
universe@179 196 fun obtainPathParameters(path: String): PathParameters {
universe@184 197 val params = mutableMapOf<String, String>()
universe@179 198 val nodes = parse(path)
universe@179 199 for (i in 0 until min(nodes.size, nodePatterns.size)) {
universe@179 200 val pattern = nodePatterns[i]
universe@179 201 val node = nodes[i]
universe@184 202 if (pattern.startsWith("%")) {
universe@179 203 params[pattern.substring(1)] = node
universe@179 204 }
universe@179 205 }
universe@179 206 return params
universe@179 207 }
universe@179 208
universe@179 209 override fun hashCode(): Int {
universe@179 210 val str = StringBuilder()
universe@179 211 for (node in nodePatterns) {
universe@184 212 if (node.startsWith("%")) {
universe@184 213 str.append("/%")
universe@179 214 } else {
universe@179 215 str.append('/')
universe@179 216 str.append(node)
universe@179 217 }
universe@179 218 }
universe@179 219 if (collection) str.append('/')
universe@179 220 return str.toString().hashCode()
universe@179 221 }
universe@179 222
universe@179 223 override fun equals(other: Any?): Boolean {
universe@179 224 if (other is PathPattern) {
universe@179 225 if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false
universe@179 226 for (i in nodePatterns.indices) {
universe@179 227 val left = nodePatterns[i]
universe@179 228 val right = other.nodePatterns[i]
universe@184 229 if (left.startsWith("%") && right.startsWith("%")) continue
universe@179 230 if (left != right) return false
universe@179 231 }
universe@179 232 return true
universe@179 233 } else {
universe@179 234 return false
universe@179 235 }
universe@179 236 }
universe@179 237
universe@179 238 init {
universe@179 239 nodePatterns = parse(pattern)
universe@179 240 collection = pattern.endsWith("/")
universe@179 241 }
universe@179 242 }
universe@179 243

mercurial