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

Sat, 15 May 2021 16:19:29 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 15 May 2021 16:19:29 +0200
changeset 199
59393c8cc557
parent 198
94f174d591ab
child 205
7725a79416f3
permissions
-rw-r--r--

#109 adds RSS feed button to project header and changes feed output slightly

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

mercurial