Fri, 02 Apr 2021 11:59:14 +0200
completes kotlin migration
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@184 | 111 | fun render(page: String? = null) { |
universe@184 | 112 | page?.let { contentPage = it } |
universe@184 | 113 | request.getRequestDispatcher(jspPath("site")).forward(request, response) |
universe@184 | 114 | } |
universe@184 | 115 | |
universe@184 | 116 | fun renderCommit(location: String? = null) { |
universe@184 | 117 | location?.let { redirectLocation = it } |
universe@184 | 118 | contentPage = Constants.JSP_COMMIT_SUCCESSFUL |
universe@184 | 119 | render() |
universe@184 | 120 | } |
universe@184 | 121 | } |
universe@179 | 122 | |
universe@179 | 123 | /** |
universe@179 | 124 | * A path pattern optionally containing placeholders. |
universe@179 | 125 | * |
universe@179 | 126 | * The special directories . and .. are disallowed in the pattern. |
universe@184 | 127 | * Placeholders start with a % sign. |
universe@179 | 128 | * |
universe@179 | 129 | * @param pattern the pattern |
universe@179 | 130 | */ |
universe@179 | 131 | class PathPattern(pattern: String) { |
universe@179 | 132 | private val nodePatterns: List<String> |
universe@179 | 133 | private val collection: Boolean |
universe@179 | 134 | |
universe@179 | 135 | private fun parse(pattern: String): List<String> { |
universe@179 | 136 | val nodes = pattern.split("/").filter { it.isNotBlank() }.toList() |
universe@179 | 137 | require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." } |
universe@179 | 138 | return nodes |
universe@179 | 139 | } |
universe@179 | 140 | |
universe@179 | 141 | /** |
universe@179 | 142 | * Matches a path against this pattern. |
universe@179 | 143 | * The path must be canonical in the sense that no . or .. parts occur. |
universe@179 | 144 | * |
universe@179 | 145 | * @param path the path to match |
universe@179 | 146 | * @return true if the path matches the pattern, false otherwise |
universe@179 | 147 | */ |
universe@179 | 148 | fun matches(path: String): Boolean { |
universe@179 | 149 | if (collection xor path.endsWith("/")) return false |
universe@179 | 150 | val nodes = parse(path) |
universe@179 | 151 | if (nodePatterns.size != nodes.size) return false |
universe@179 | 152 | for (i in nodePatterns.indices) { |
universe@179 | 153 | val pattern = nodePatterns[i] |
universe@179 | 154 | val node = nodes[i] |
universe@184 | 155 | if (pattern.startsWith("%")) continue |
universe@179 | 156 | if (pattern != node) return false |
universe@179 | 157 | } |
universe@179 | 158 | return true |
universe@179 | 159 | } |
universe@179 | 160 | |
universe@179 | 161 | /** |
universe@179 | 162 | * Returns the path parameters found in the specified path using this pattern. |
universe@179 | 163 | * The return value of this method is undefined, if the patter does not match. |
universe@179 | 164 | * |
universe@179 | 165 | * @param path the path |
universe@179 | 166 | * @return the path parameters, if any, or an empty map |
universe@179 | 167 | * @see .matches |
universe@179 | 168 | */ |
universe@179 | 169 | fun obtainPathParameters(path: String): PathParameters { |
universe@184 | 170 | val params = mutableMapOf<String, String>() |
universe@179 | 171 | val nodes = parse(path) |
universe@179 | 172 | for (i in 0 until min(nodes.size, nodePatterns.size)) { |
universe@179 | 173 | val pattern = nodePatterns[i] |
universe@179 | 174 | val node = nodes[i] |
universe@184 | 175 | if (pattern.startsWith("%")) { |
universe@179 | 176 | params[pattern.substring(1)] = node |
universe@179 | 177 | } |
universe@179 | 178 | } |
universe@179 | 179 | return params |
universe@179 | 180 | } |
universe@179 | 181 | |
universe@179 | 182 | override fun hashCode(): Int { |
universe@179 | 183 | val str = StringBuilder() |
universe@179 | 184 | for (node in nodePatterns) { |
universe@184 | 185 | if (node.startsWith("%")) { |
universe@184 | 186 | str.append("/%") |
universe@179 | 187 | } else { |
universe@179 | 188 | str.append('/') |
universe@179 | 189 | str.append(node) |
universe@179 | 190 | } |
universe@179 | 191 | } |
universe@179 | 192 | if (collection) str.append('/') |
universe@179 | 193 | return str.toString().hashCode() |
universe@179 | 194 | } |
universe@179 | 195 | |
universe@179 | 196 | override fun equals(other: Any?): Boolean { |
universe@179 | 197 | if (other is PathPattern) { |
universe@179 | 198 | if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false |
universe@179 | 199 | for (i in nodePatterns.indices) { |
universe@179 | 200 | val left = nodePatterns[i] |
universe@179 | 201 | val right = other.nodePatterns[i] |
universe@184 | 202 | if (left.startsWith("%") && right.startsWith("%")) continue |
universe@179 | 203 | if (left != right) return false |
universe@179 | 204 | } |
universe@179 | 205 | return true |
universe@179 | 206 | } else { |
universe@179 | 207 | return false |
universe@179 | 208 | } |
universe@179 | 209 | } |
universe@179 | 210 | |
universe@179 | 211 | init { |
universe@179 | 212 | nodePatterns = parse(pattern) |
universe@179 | 213 | collection = pattern.endsWith("/") |
universe@179 | 214 | } |
universe@179 | 215 | } |
universe@179 | 216 |