Wed, 18 Aug 2021 12:47:32 +0200
#158 adds total number of comments
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@209 | 40 | sealed interface ValidationResult<T> |
universe@209 | 41 | class ValidationError<T>(val message: String): ValidationResult<T> |
universe@209 | 42 | class ValidatedValue<T>(val result: T): ValidationResult<T> |
universe@209 | 43 | |
universe@184 | 44 | class HttpRequest( |
universe@184 | 45 | val request: HttpServletRequest, |
universe@184 | 46 | val response: HttpServletResponse, |
universe@184 | 47 | val pathParams: PathParameters = emptyMap() |
universe@184 | 48 | ) { |
universe@184 | 49 | val session: HttpSession = request.session |
universe@184 | 50 | |
universe@184 | 51 | val remoteUser: String? = request.remoteUser |
universe@179 | 52 | |
universe@179 | 53 | /** |
universe@184 | 54 | * The name of the content page. |
universe@179 | 55 | * |
universe@184 | 56 | * @see Constants#REQ_ATTR_CONTENT_PAGE |
universe@179 | 57 | */ |
universe@184 | 58 | var contentPage = "" |
universe@184 | 59 | set(value) { |
universe@184 | 60 | field = value |
universe@184 | 61 | request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value)) |
universe@184 | 62 | } |
universe@179 | 63 | |
universe@179 | 64 | /** |
universe@205 | 65 | * The name of the content page. |
universe@205 | 66 | * |
universe@207 | 67 | * @see Constants#REQ_ATTR_PAGE_TITLE |
universe@205 | 68 | */ |
universe@205 | 69 | var pageTitle = "" |
universe@205 | 70 | set(value) { |
universe@205 | 71 | field = value |
universe@205 | 72 | request.setAttribute(Constants.REQ_ATTR_PAGE_TITLE, value) |
universe@205 | 73 | } |
universe@205 | 74 | |
universe@205 | 75 | /** |
universe@184 | 76 | * A list of additional style sheets. |
universe@179 | 77 | * |
universe@184 | 78 | * @see Constants#REQ_ATTR_STYLESHEET |
universe@179 | 79 | */ |
universe@184 | 80 | var styleSheets = emptyList<String>() |
universe@184 | 81 | set(value) { |
universe@184 | 82 | field = value |
universe@184 | 83 | request.setAttribute(Constants.REQ_ATTR_STYLESHEET, |
universe@184 | 84 | value.map { it.withExt(".css") } |
universe@184 | 85 | ) |
universe@184 | 86 | } |
universe@179 | 87 | |
universe@184 | 88 | /** |
universe@207 | 89 | * A list of additional style sheets. |
universe@207 | 90 | * |
universe@207 | 91 | * @see Constants#REQ_ATTR_JAVASCRIPT |
universe@207 | 92 | */ |
universe@207 | 93 | var javascript = "" |
universe@207 | 94 | set(value) { |
universe@207 | 95 | field = value |
universe@207 | 96 | request.setAttribute(Constants.REQ_ATTR_JAVASCRIPT, |
universe@207 | 97 | value.withExt(".js") |
universe@207 | 98 | ) |
universe@207 | 99 | } |
universe@207 | 100 | |
universe@207 | 101 | /** |
universe@184 | 102 | * The name of the navigation menu JSP. |
universe@184 | 103 | * |
universe@184 | 104 | * @see Constants#REQ_ATTR_NAVIGATION |
universe@184 | 105 | */ |
universe@184 | 106 | var navigationMenu: NavMenu? = null |
universe@184 | 107 | set(value) { |
universe@184 | 108 | field = value |
universe@184 | 109 | request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu) |
universe@184 | 110 | } |
universe@184 | 111 | |
universe@199 | 112 | var redirectLocation: String? = null |
universe@184 | 113 | set(value) { |
universe@184 | 114 | field = value |
universe@199 | 115 | if (value == null) { |
universe@199 | 116 | request.removeAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION) |
universe@199 | 117 | } else { |
universe@199 | 118 | request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value) |
universe@199 | 119 | } |
universe@184 | 120 | } |
universe@184 | 121 | |
universe@199 | 122 | var feedPath: String? = null |
universe@198 | 123 | set(value) { |
universe@198 | 124 | field = value |
universe@199 | 125 | if (value == null) { |
universe@199 | 126 | request.removeAttribute(Constants.REQ_ATTR_FEED_HREF) |
universe@199 | 127 | } else { |
universe@199 | 128 | request.setAttribute(Constants.REQ_ATTR_FEED_HREF, baseHref + value) |
universe@199 | 129 | } |
universe@198 | 130 | } |
universe@198 | 131 | |
universe@184 | 132 | /** |
universe@184 | 133 | * The view object. |
universe@184 | 134 | * |
universe@184 | 135 | * @see Constants#REQ_ATTR_VIEWMODEL |
universe@184 | 136 | */ |
universe@184 | 137 | var view: View? = null |
universe@184 | 138 | set(value) { |
universe@184 | 139 | field = value |
universe@184 | 140 | request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value) |
universe@184 | 141 | } |
universe@184 | 142 | |
universe@184 | 143 | /** |
universe@198 | 144 | * Additional port info, if necessary. |
universe@198 | 145 | */ |
universe@198 | 146 | private val portInfo = |
universe@198 | 147 | if ((request.scheme == "http" && request.serverPort == 80) |
universe@198 | 148 | || (request.scheme == "https" && request.serverPort == 443) |
universe@198 | 149 | ) "" else ":${request.serverPort}" |
universe@198 | 150 | |
universe@198 | 151 | /** |
universe@184 | 152 | * The base path of this application. |
universe@184 | 153 | */ |
universe@198 | 154 | val baseHref get() = "${request.scheme}://${request.serverName}$portInfo${request.contextPath}/" |
universe@198 | 155 | |
universe@184 | 156 | private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext) |
universe@184 | 157 | private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp") |
universe@184 | 158 | |
universe@184 | 159 | fun param(name: String): String? = request.getParameter(name) |
universe@184 | 160 | fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() |
universe@184 | 161 | |
universe@210 | 162 | fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), |
universe@210 | 163 | defaultValue: T, errorMessages: MutableList<String>): T { |
universe@209 | 164 | return when (val result = validator(param(name))) { |
universe@209 | 165 | is ValidationError -> { |
universe@209 | 166 | errorMessages.add(i18n(result.message)) |
universe@210 | 167 | defaultValue |
universe@209 | 168 | } |
universe@209 | 169 | is ValidatedValue -> { |
universe@209 | 170 | result.result |
universe@209 | 171 | } |
universe@209 | 172 | } |
universe@209 | 173 | } |
universe@209 | 174 | |
universe@198 | 175 | private fun forward(jsp: String) { |
universe@195 | 176 | request.getRequestDispatcher(jspPath(jsp)).forward(request, response) |
universe@195 | 177 | } |
universe@195 | 178 | |
universe@198 | 179 | fun renderFeed(page: String? = null) { |
universe@198 | 180 | page?.let { contentPage = it } |
universe@198 | 181 | forward("feed") |
universe@198 | 182 | } |
universe@198 | 183 | |
universe@184 | 184 | fun render(page: String? = null) { |
universe@184 | 185 | page?.let { contentPage = it } |
universe@195 | 186 | forward("site") |
universe@184 | 187 | } |
universe@184 | 188 | |
universe@184 | 189 | fun renderCommit(location: String? = null) { |
universe@184 | 190 | location?.let { redirectLocation = it } |
universe@184 | 191 | contentPage = Constants.JSP_COMMIT_SUCCESSFUL |
universe@184 | 192 | render() |
universe@184 | 193 | } |
universe@205 | 194 | |
universe@205 | 195 | fun i18n(key: String) = ResourceBundle.getBundle("localization/strings", response.locale).getString(key) |
universe@184 | 196 | } |
universe@179 | 197 | |
universe@179 | 198 | /** |
universe@179 | 199 | * A path pattern optionally containing placeholders. |
universe@179 | 200 | * |
universe@179 | 201 | * The special directories . and .. are disallowed in the pattern. |
universe@184 | 202 | * Placeholders start with a % sign. |
universe@179 | 203 | * |
universe@179 | 204 | * @param pattern the pattern |
universe@179 | 205 | */ |
universe@179 | 206 | class PathPattern(pattern: String) { |
universe@179 | 207 | private val nodePatterns: List<String> |
universe@179 | 208 | private val collection: Boolean |
universe@179 | 209 | |
universe@179 | 210 | private fun parse(pattern: String): List<String> { |
universe@179 | 211 | val nodes = pattern.split("/").filter { it.isNotBlank() }.toList() |
universe@179 | 212 | require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." } |
universe@179 | 213 | return nodes |
universe@179 | 214 | } |
universe@179 | 215 | |
universe@179 | 216 | /** |
universe@179 | 217 | * Matches a path against this pattern. |
universe@179 | 218 | * The path must be canonical in the sense that no . or .. parts occur. |
universe@179 | 219 | * |
universe@179 | 220 | * @param path the path to match |
universe@179 | 221 | * @return true if the path matches the pattern, false otherwise |
universe@179 | 222 | */ |
universe@179 | 223 | fun matches(path: String): Boolean { |
universe@179 | 224 | if (collection xor path.endsWith("/")) return false |
universe@179 | 225 | val nodes = parse(path) |
universe@179 | 226 | if (nodePatterns.size != nodes.size) return false |
universe@179 | 227 | for (i in nodePatterns.indices) { |
universe@179 | 228 | val pattern = nodePatterns[i] |
universe@179 | 229 | val node = nodes[i] |
universe@184 | 230 | if (pattern.startsWith("%")) continue |
universe@179 | 231 | if (pattern != node) return false |
universe@179 | 232 | } |
universe@179 | 233 | return true |
universe@179 | 234 | } |
universe@179 | 235 | |
universe@179 | 236 | /** |
universe@179 | 237 | * Returns the path parameters found in the specified path using this pattern. |
universe@179 | 238 | * The return value of this method is undefined, if the patter does not match. |
universe@179 | 239 | * |
universe@179 | 240 | * @param path the path |
universe@179 | 241 | * @return the path parameters, if any, or an empty map |
universe@179 | 242 | * @see .matches |
universe@179 | 243 | */ |
universe@179 | 244 | fun obtainPathParameters(path: String): PathParameters { |
universe@184 | 245 | val params = mutableMapOf<String, String>() |
universe@179 | 246 | val nodes = parse(path) |
universe@179 | 247 | for (i in 0 until min(nodes.size, nodePatterns.size)) { |
universe@179 | 248 | val pattern = nodePatterns[i] |
universe@179 | 249 | val node = nodes[i] |
universe@184 | 250 | if (pattern.startsWith("%")) { |
universe@179 | 251 | params[pattern.substring(1)] = node |
universe@179 | 252 | } |
universe@179 | 253 | } |
universe@179 | 254 | return params |
universe@179 | 255 | } |
universe@179 | 256 | |
universe@179 | 257 | override fun hashCode(): Int { |
universe@179 | 258 | val str = StringBuilder() |
universe@179 | 259 | for (node in nodePatterns) { |
universe@184 | 260 | if (node.startsWith("%")) { |
universe@184 | 261 | str.append("/%") |
universe@179 | 262 | } else { |
universe@179 | 263 | str.append('/') |
universe@179 | 264 | str.append(node) |
universe@179 | 265 | } |
universe@179 | 266 | } |
universe@179 | 267 | if (collection) str.append('/') |
universe@179 | 268 | return str.toString().hashCode() |
universe@179 | 269 | } |
universe@179 | 270 | |
universe@179 | 271 | override fun equals(other: Any?): Boolean { |
universe@179 | 272 | if (other is PathPattern) { |
universe@179 | 273 | if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false |
universe@179 | 274 | for (i in nodePatterns.indices) { |
universe@179 | 275 | val left = nodePatterns[i] |
universe@179 | 276 | val right = other.nodePatterns[i] |
universe@184 | 277 | if (left.startsWith("%") && right.startsWith("%")) continue |
universe@179 | 278 | if (left != right) return false |
universe@179 | 279 | } |
universe@179 | 280 | return true |
universe@179 | 281 | } else { |
universe@179 | 282 | return false |
universe@179 | 283 | } |
universe@179 | 284 | } |
universe@179 | 285 | |
universe@179 | 286 | init { |
universe@179 | 287 | nodePatterns = parse(pattern) |
universe@179 | 288 | collection = pattern.endsWith("/") |
universe@179 | 289 | } |
universe@179 | 290 | } |
universe@179 | 291 |