Thu, 13 May 2021 11:28:50 +0200
#109 - add RSS feed
/* * Copyright 2021 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package de.uapcore.lightpit import de.uapcore.lightpit.dao.DataAccessObject import de.uapcore.lightpit.viewmodel.NavMenu import de.uapcore.lightpit.viewmodel.View import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse import javax.servlet.http.HttpSession import kotlin.math.min typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit typealias PathParameters = Map<String, String> class HttpRequest( val request: HttpServletRequest, val response: HttpServletResponse, val pathParams: PathParameters = emptyMap() ) { val session: HttpSession = request.session val remoteUser: String? = request.remoteUser /** * The name of the content page. * * @see Constants#REQ_ATTR_CONTENT_PAGE */ var contentPage = "" set(value) { field = value request.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, jspPath(value)) } /** * A list of additional style sheets. * * @see Constants#REQ_ATTR_STYLESHEET */ var styleSheets = emptyList<String>() set(value) { field = value request.setAttribute(Constants.REQ_ATTR_STYLESHEET, value.map { it.withExt(".css") } ) } /** * The name of the navigation menu JSP. * * @see Constants#REQ_ATTR_NAVIGATION */ var navigationMenu: NavMenu? = null set(value) { field = value request.setAttribute(Constants.REQ_ATTR_NAVIGATION, navigationMenu) } var redirectLocation = "" set(value) { field = value request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value) } /** * The view object. * * @see Constants#REQ_ATTR_VIEWMODEL */ var view: View? = null set(value) { field = value request.setAttribute(Constants.REQ_ATTR_VIEWMODEL, value) } /** * The base path of this application. */ val baseHref get() = "${request.scheme}://${request.serverName}:${request.serverPort}${request.contextPath}/" private fun String.withExt(ext: String) = if (endsWith(ext)) this else plus(ext) private fun jspPath(name: String) = Constants.JSP_PATH_PREFIX.plus(name).withExt(".jsp") fun param(name: String): String? = request.getParameter(name) fun paramArray(name: String): Array<String> = request.getParameterValues(name) ?: emptyArray() fun forward(jsp: String) { request.getRequestDispatcher(jspPath(jsp)).forward(request, response) } fun render(page: String? = null) { page?.let { contentPage = it } forward("site") } fun renderCommit(location: String? = null) { location?.let { redirectLocation = it } contentPage = Constants.JSP_COMMIT_SUCCESSFUL render() } } /** * A path pattern optionally containing placeholders. * * The special directories . and .. are disallowed in the pattern. * Placeholders start with a % sign. * * @param pattern the pattern */ class PathPattern(pattern: String) { private val nodePatterns: List<String> private val collection: Boolean private fun parse(pattern: String): List<String> { val nodes = pattern.split("/").filter { it.isNotBlank() }.toList() require(nodes.none { it == "." || it == ".." }) { "Path must not contain '.' or '..' nodes." } return nodes } /** * Matches a path against this pattern. * The path must be canonical in the sense that no . or .. parts occur. * * @param path the path to match * @return true if the path matches the pattern, false otherwise */ fun matches(path: String): Boolean { if (collection xor path.endsWith("/")) return false val nodes = parse(path) if (nodePatterns.size != nodes.size) return false for (i in nodePatterns.indices) { val pattern = nodePatterns[i] val node = nodes[i] if (pattern.startsWith("%")) continue if (pattern != node) return false } return true } /** * Returns the path parameters found in the specified path using this pattern. * The return value of this method is undefined, if the patter does not match. * * @param path the path * @return the path parameters, if any, or an empty map * @see .matches */ fun obtainPathParameters(path: String): PathParameters { val params = mutableMapOf<String, String>() val nodes = parse(path) for (i in 0 until min(nodes.size, nodePatterns.size)) { val pattern = nodePatterns[i] val node = nodes[i] if (pattern.startsWith("%")) { params[pattern.substring(1)] = node } } return params } override fun hashCode(): Int { val str = StringBuilder() for (node in nodePatterns) { if (node.startsWith("%")) { str.append("/%") } else { str.append('/') str.append(node) } } if (collection) str.append('/') return str.toString().hashCode() } override fun equals(other: Any?): Boolean { if (other is PathPattern) { if (collection xor other.collection || nodePatterns.size != other.nodePatterns.size) return false for (i in nodePatterns.indices) { val left = nodePatterns[i] val right = other.nodePatterns[i] if (left.startsWith("%") && right.startsWith("%")) continue if (left != right) return false } return true } else { return false } } init { nodePatterns = parse(pattern) collection = pattern.endsWith("/") } }