Mon, 30 Oct 2023 14:44:36 +0100
add possibility to show issues w/o version or component - fixes #335
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@292 | 29 | import de.uapcore.lightpit.entities.HasNode |
universe@184 | 30 | import de.uapcore.lightpit.viewmodel.NavMenu |
universe@184 | 31 | import de.uapcore.lightpit.viewmodel.View |
universe@254 | 32 | import jakarta.servlet.http.HttpServletRequest |
universe@254 | 33 | import jakarta.servlet.http.HttpServletResponse |
universe@254 | 34 | import jakarta.servlet.http.HttpSession |
universe@205 | 35 | import java.util.* |
universe@179 | 36 | import kotlin.math.min |
universe@225 | 37 | import java.sql.Date as SqlDate |
universe@179 | 38 | |
universe@184 | 39 | typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit |
universe@184 | 40 | typealias PathParameters = Map<String, String> |
universe@184 | 41 | |
universe@292 | 42 | sealed class OptionalPathInfo<in T : HasNode>(info: T) { |
universe@292 | 43 | class Specific<T: HasNode>(val elem: T) : OptionalPathInfo<T>(elem) |
universe@292 | 44 | data object All : OptionalPathInfo<HasNode>(object : HasNode { override val node = "-"}) |
universe@292 | 45 | data object None : OptionalPathInfo<HasNode>(object : HasNode { override val node = "~"}) |
universe@292 | 46 | data object NotFound : OptionalPathInfo<HasNode>(object : HasNode { override val node = ""}) |
universe@292 | 47 | val node = info.node |
universe@292 | 48 | } |
universe@292 | 49 | |
universe@209 | 50 | sealed interface ValidationResult<T> |
universe@209 | 51 | class ValidationError<T>(val message: String): ValidationResult<T> |
universe@209 | 52 | class ValidatedValue<T>(val result: T): ValidationResult<T> |
universe@209 | 53 | |
universe@184 | 54 | class HttpRequest( |
universe@184 | 55 | val request: HttpServletRequest, |
universe@184 | 56 | val response: HttpServletResponse, |
universe@184 | 57 | val pathParams: PathParameters = emptyMap() |
universe@184 | 58 | ) { |
universe@184 | 59 | val session: HttpSession = request.session |
universe@184 | 60 | |
universe@184 | 61 | val remoteUser: String? = request.remoteUser |
universe@179 | 62 | |
universe@179 | 63 | /** |
universe@184 | 64 | * The name of the content page. |
universe@179 | 65 | * |
universe@184 | 66 | * @see Constants#REQ_ATTR_CONTENT_PAGE |
universe@179 | 67 | */ |
universe@184 | 68 | var contentPage = "" |
universe@184 | 69 | set(value) { |
universe@184 | 70 | field = value |
universe@184 | 71 | request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value)) |
universe@184 | 72 | } |
universe@179 | 73 | |
universe@179 | 74 | /** |
universe@205 | 75 | * The name of the content page. |
universe@205 | 76 | * |
universe@207 | 77 | * @see Constants#REQ_ATTR_PAGE_TITLE |
universe@205 | 78 | */ |
universe@205 | 79 | var pageTitle = "" |
universe@205 | 80 | set(value) { |
universe@205 | 81 | field = value |
universe@205 | 82 | request.setAttribute(Constants.REQ_ATTR_PAGE_TITLE, value) |
universe@205 | 83 | } |
universe@205 | 84 | |
universe@205 | 85 | /** |
universe@184 | 86 | * A list of additional style sheets. |
universe@179 | 87 | * |
universe@184 | 88 | * @see Constants#REQ_ATTR_STYLESHEET |
universe@179 | 89 | */ |
universe@184 | 90 | var styleSheets = emptyList<String>() |
universe@184 | 91 | set(value) { |
universe@184 | 92 | field = value |
universe@184 | 93 | request.setAttribute(Constants.REQ_ATTR_STYLESHEET, |
universe@184 | 94 | value.map { it.withExt(".css") } |
universe@184 | 95 | ) |
universe@184 | 96 | } |
universe@179 | 97 | |
universe@184 | 98 | /** |
universe@207 | 99 | * A list of additional style sheets. |
universe@207 | 100 | * |
universe@207 | 101 | * @see Constants#REQ_ATTR_JAVASCRIPT |
universe@207 | 102 | */ |
universe@207 | 103 | var javascript = "" |
universe@207 | 104 | set(value) { |
universe@207 | 105 | field = value |
universe@207 | 106 | request.setAttribute(Constants.REQ_ATTR_JAVASCRIPT, |
universe@207 | 107 | value.withExt(".js") |
universe@207 | 108 | ) |
universe@207 | 109 | } |
universe@207 | 110 | |
universe@207 | 111 | /** |
universe@184 | 112 | * The name of the navigation menu JSP. |
universe@184 | 113 | * |
universe@184 | 114 | * @see Constants#REQ_ATTR_NAVIGATION |
universe@184 | 115 | */ |
universe@184 | 116 | var navigationMenu: NavMenu? = null |
universe@184 | 117 | set(value) { |
universe@184 | 118 | field = value |
universe@184 | 119 | request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu) |
universe@184 | 120 | } |
universe@184 | 121 | |
universe@199 | 122 | var redirectLocation: String? = null |
universe@184 | 123 | set(value) { |
universe@184 | 124 | field = value |
universe@199 | 125 | if (value == null) { |
universe@199 | 126 | request.removeAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION) |
universe@199 | 127 | } else { |
universe@199 | 128 | request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value) |
universe@199 | 129 | } |
universe@184 | 130 | } |
universe@184 | 131 | |
universe@199 | 132 | var feedPath: String? = null |
universe@198 | 133 | set(value) { |
universe@198 | 134 | field = value |
universe@199 | 135 | if (value == null) { |
universe@199 | 136 | request.removeAttribute(Constants.REQ_ATTR_FEED_HREF) |
universe@199 | 137 | } else { |
universe@199 | 138 | request.setAttribute(Constants.REQ_ATTR_FEED_HREF, baseHref + value) |
universe@199 | 139 | } |
universe@198 | 140 | } |
universe@198 | 141 | |
universe@184 | 142 | /** |
universe@184 | 143 | * The view object. |
universe@184 | 144 | * |
universe@184 | 145 | * @see Constants#REQ_ATTR_VIEWMODEL |
universe@184 | 146 | */ |
universe@184 | 147 | var view: View? = null |
universe@184 | 148 | set(value) { |
universe@184 | 149 | field = value |
universe@184 | 150 | request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value) |
universe@184 | 151 | } |
universe@184 | 152 | |
universe@184 | 153 | /** |
universe@198 | 154 | * Additional port info, if necessary. |
universe@198 | 155 | */ |
universe@198 | 156 | private val portInfo = |
universe@198 | 157 | if ((request.scheme == "http" && request.serverPort == 80) |
universe@198 | 158 | || (request.scheme == "https" && request.serverPort == 443) |
universe@198 | 159 | ) "" else ":${request.serverPort}" |
universe@198 | 160 | |
universe@198 | 161 | /** |
universe@184 | 162 | * The base path of this application. |
universe@184 | 163 | */ |
universe@198 | 164 | val baseHref get() = "${request.scheme}://${request.serverName}$portInfo${request.contextPath}/" |
universe@198 | 165 | |
universe@184 | 166 | private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext) |
universe@184 | 167 | private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp") |
universe@184 | 168 | |
universe@184 | 169 | fun param(name: String): String? = request.getParameter(name) |
universe@184 | 170 | fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() |
universe@184 | 171 | |
universe@210 | 172 | fun <T> param(name: String, validator: (String?) -> (ValidationResult<T>), |
universe@210 | 173 | defaultValue: T, errorMessages: MutableList<String>): T { |
universe@209 | 174 | return when (val result = validator(param(name))) { |
universe@209 | 175 | is ValidationError -> { |
universe@209 | 176 | errorMessages.add(i18n(result.message)) |
universe@210 | 177 | defaultValue |
universe@209 | 178 | } |
universe@209 | 179 | is ValidatedValue -> { |
universe@209 | 180 | result.result |
universe@209 | 181 | } |
universe@209 | 182 | } |
universe@209 | 183 | } |
universe@209 | 184 | |
universe@292 | 185 | |
universe@292 | 186 | fun <T : HasNode> lookupPathParam(paramName: String, list: List<T>): OptionalPathInfo<T> { |
universe@292 | 187 | return when (val node = this.pathParams[paramName]) { |
universe@292 | 188 | null -> OptionalPathInfo.All |
universe@292 | 189 | "-" -> OptionalPathInfo.All |
universe@292 | 190 | "~" -> OptionalPathInfo.None |
universe@292 | 191 | else -> list.find { it.node == node } |
universe@292 | 192 | ?.let { OptionalPathInfo.Specific(it) } |
universe@292 | 193 | ?: OptionalPathInfo.NotFound |
universe@292 | 194 | } |
universe@292 | 195 | } |
universe@292 | 196 | |
universe@284 | 197 | val body: String by lazy { |
universe@284 | 198 | request.reader.lineSequence().joinToString("\n") |
universe@284 | 199 | } |
universe@284 | 200 | |
universe@198 | 201 | private fun forward(jsp: String) { |
universe@195 | 202 | request.getRequestDispatcher(jspPath(jsp)).forward(request, response) |
universe@195 | 203 | } |
universe@195 | 204 | |
universe@198 | 205 | fun renderFeed(page: String? = null) { |
universe@198 | 206 | page?.let { contentPage = it } |
universe@198 | 207 | forward("feed") |
universe@198 | 208 | } |
universe@198 | 209 | |
universe@184 | 210 | fun render(page: String? = null) { |
universe@184 | 211 | page?.let { contentPage = it } |
universe@195 | 212 | forward("site") |
universe@184 | 213 | } |
universe@184 | 214 | |
universe@184 | 215 | fun renderCommit(location: String? = null) { |
universe@184 | 216 | location?.let { redirectLocation = it } |
universe@184 | 217 | contentPage = Constants.JSP_COMMIT_SUCCESSFUL |
universe@184 | 218 | render() |
universe@184 | 219 | } |
universe@205 | 220 | |
universe@260 | 221 | fun i18n(key: String): String = ResourceBundle.getBundle("localization/strings", response.locale).getString(key) |
universe@184 | 222 | } |
universe@179 | 223 | |
universe@179 | 224 | /** |
universe@179 | 225 | * A path pattern optionally containing placeholders. |
universe@179 | 226 | * |
universe@179 | 227 | * The special directories . and .. are disallowed in the pattern. |
universe@184 | 228 | * Placeholders start with a % sign. |
universe@179 | 229 | * |
universe@179 | 230 | * @param pattern the pattern |
universe@179 | 231 | */ |
universe@179 | 232 | class PathPattern(pattern: String) { |
universe@179 | 233 | private val nodePatterns: List<String> |
universe@179 | 234 | private val collection: Boolean |
universe@179 | 235 | |
universe@179 | 236 | private fun parse(pattern: String): List<String> { |
universe@179 | 237 | val nodes = pattern.split("/").filter { it.isNotBlank() }.toList() |
universe@179 | 238 | require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." } |
universe@179 | 239 | return nodes |
universe@179 | 240 | } |
universe@179 | 241 | |
universe@179 | 242 | /** |
universe@179 | 243 | * Matches a path against this pattern. |
universe@179 | 244 | * The path must be canonical in the sense that no . or .. parts occur. |
universe@179 | 245 | * |
universe@179 | 246 | * @param path the path to match |
universe@179 | 247 | * @return true if the path matches the pattern, false otherwise |
universe@179 | 248 | */ |
universe@179 | 249 | fun matches(path: String): Boolean { |
universe@179 | 250 | if (collection xor path.endsWith("/")) return false |
universe@179 | 251 | val nodes = parse(path) |
universe@179 | 252 | if (nodePatterns.size != nodes.size) return false |
universe@179 | 253 | for (i in nodePatterns.indices) { |
universe@179 | 254 | val pattern = nodePatterns[i] |
universe@179 | 255 | val node = nodes[i] |
universe@184 | 256 | if (pattern.startsWith("%")) continue |
universe@179 | 257 | if (pattern != node) return false |
universe@179 | 258 | } |
universe@179 | 259 | return true |
universe@179 | 260 | } |
universe@179 | 261 | |
universe@179 | 262 | /** |
universe@179 | 263 | * Returns the path parameters found in the specified path using this pattern. |
universe@179 | 264 | * The return value of this method is undefined, if the patter does not match. |
universe@179 | 265 | * |
universe@179 | 266 | * @param path the path |
universe@179 | 267 | * @return the path parameters, if any, or an empty map |
universe@179 | 268 | * @see .matches |
universe@179 | 269 | */ |
universe@179 | 270 | fun obtainPathParameters(path: String): PathParameters { |
universe@184 | 271 | val params = mutableMapOf<String, String>() |
universe@179 | 272 | val nodes = parse(path) |
universe@179 | 273 | for (i in 0 until min(nodes.size, nodePatterns.size)) { |
universe@179 | 274 | val pattern = nodePatterns[i] |
universe@179 | 275 | val node = nodes[i] |
universe@184 | 276 | if (pattern.startsWith("%")) { |
universe@179 | 277 | params[pattern.substring(1)] = node |
universe@179 | 278 | } |
universe@179 | 279 | } |
universe@179 | 280 | return params |
universe@179 | 281 | } |
universe@179 | 282 | |
universe@179 | 283 | override fun hashCode(): Int { |
universe@179 | 284 | val str = StringBuilder() |
universe@179 | 285 | for (node in nodePatterns) { |
universe@184 | 286 | if (node.startsWith("%")) { |
universe@184 | 287 | str.append("/%") |
universe@179 | 288 | } else { |
universe@179 | 289 | str.append('/') |
universe@179 | 290 | str.append(node) |
universe@179 | 291 | } |
universe@179 | 292 | } |
universe@179 | 293 | if (collection) str.append('/') |
universe@179 | 294 | return str.toString().hashCode() |
universe@179 | 295 | } |
universe@179 | 296 | |
universe@179 | 297 | override fun equals(other: Any?): Boolean { |
universe@179 | 298 | if (other is PathPattern) { |
universe@179 | 299 | if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false |
universe@179 | 300 | for (i in nodePatterns.indices) { |
universe@179 | 301 | val left = nodePatterns[i] |
universe@179 | 302 | val right = other.nodePatterns[i] |
universe@184 | 303 | if (left.startsWith("%") && right.startsWith("%")) continue |
universe@179 | 304 | if (left != right) return false |
universe@179 | 305 | } |
universe@179 | 306 | return true |
universe@179 | 307 | } else { |
universe@179 | 308 | return false |
universe@179 | 309 | } |
universe@179 | 310 | } |
universe@179 | 311 | |
universe@179 | 312 | init { |
universe@179 | 313 | nodePatterns = parse(pattern) |
universe@179 | 314 | collection = pattern.endsWith("/") |
universe@179 | 315 | } |
universe@179 | 316 | } |
universe@179 | 317 | |
universe@225 | 318 | // <editor-fold desc="Validators"> |
universe@225 | 319 | |
universe@225 | 320 | fun dateOptValidator(input: String?): ValidationResult<SqlDate?> { |
universe@225 | 321 | return if (input.isNullOrBlank()) { |
universe@225 | 322 | ValidatedValue(null) |
universe@225 | 323 | } else { |
universe@225 | 324 | try { |
universe@225 | 325 | ValidatedValue(SqlDate.valueOf(input)) |
universe@225 | 326 | } catch (ignored: IllegalArgumentException) { |
universe@225 | 327 | ValidationError("validation.date.format") |
universe@225 | 328 | } |
universe@225 | 329 | } |
universe@225 | 330 | } |
universe@225 | 331 | |
universe@227 | 332 | fun boolValidator(input: String?): ValidationResult<Boolean> { |
universe@227 | 333 | return if (input.isNullOrBlank()) { |
universe@227 | 334 | ValidatedValue(false) |
universe@227 | 335 | } else { |
universe@227 | 336 | ValidatedValue(!(input.equals("false", true) || input == "0")) |
universe@227 | 337 | } |
universe@227 | 338 | } |
universe@227 | 339 | |
universe@225 | 340 | // </editor-fold> |