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

Fri, 24 Nov 2023 00:07:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 24 Nov 2023 00:07:26 +0100
changeset 295
1c31921664c4
parent 292
703591e739f4
child 311
bf67e0ff7131
permissions
-rw-r--r--

fix broken link in "create issue" button

/*
 * 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.entities.HasNode
import de.uapcore.lightpit.viewmodel.NavMenu
import de.uapcore.lightpit.viewmodel.View
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import jakarta.servlet.http.HttpSession
import java.util.*
import kotlin.math.min
import java.sql.Date as SqlDate

typealias MappingMethod = (HttpRequest, DataAccessObject) -> Unit
typealias PathParameters = Map<String, String>

sealed class OptionalPathInfo<in T : HasNode>(info: T) {
    class Specific<T: HasNode>(val elem: T) : OptionalPathInfo<T>(elem)
    data object All : OptionalPathInfo<HasNode>(object : HasNode { override val node = "-"})
    data object None : OptionalPathInfo<HasNode>(object : HasNode { override val node = "~"})
    data object NotFound : OptionalPathInfo<HasNode>(object : HasNode { override val node = ""})
    val node = info.node
}

sealed interface ValidationResult<T>
class ValidationError<T>(val message: String): ValidationResult<T>
class ValidatedValue<T>(val result: T): ValidationResult<T>

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))
        }

    /**
     * The name of the content page.
     *
     * @see Constants#REQ_ATTR_PAGE_TITLE
     */
    var pageTitle = ""
        set(value) {
            field = value
            request.setAttribute(Constants.REQ_ATTR_PAGE_TITLE, 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") }
            )
        }

    /**
     * A list of additional style sheets.
     *
     * @see Constants#REQ_ATTR_JAVASCRIPT
     */
    var javascript = ""
        set(value) {
            field = value
            request.setAttribute(Constants.REQ_ATTR_JAVASCRIPT,
                value.withExt(".js")
            )
        }

    /**
     * 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: String? = null
        set(value) {
            field = value
            if (value == null) {
                request.removeAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION)
            } else {
                request.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, baseHref + value)
            }
        }

    var feedPath: String? = null
        set(value) {
            field = value
            if (value == null) {
                request.removeAttribute(Constants.REQ_ATTR_FEED_HREF)
            } else {
                request.setAttribute(Constants.REQ_ATTR_FEED_HREF, 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)
        }

    /**
     * Additional port info, if necessary.
     */
    private val portInfo =
        if ((request.scheme == "http" && request.serverPort == 80)
            || (request.scheme == "https" && request.serverPort == 443)
        ) "" else ":${request.serverPort}"

    /**
     * The base path of this application.
     */
    val baseHref get() = "${request.scheme}://${request.serverName}$portInfo${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 <T> param(name: String, validator: (String?) -> (ValidationResult<T>),
                  defaultValue: T, errorMessages: MutableList<String>): T {
        return when (val result = validator(param(name))) {
            is ValidationError -> {
                errorMessages.add(i18n(result.message))
                defaultValue
            }
            is ValidatedValue -> {
                result.result
            }
        }
    }


    fun <T : HasNode> lookupPathParam(paramName: String, list: List<T>): OptionalPathInfo<T> {
        return when (val node = this.pathParams[paramName]) {
            null -> OptionalPathInfo.All
            "-" -> OptionalPathInfo.All
            "~" -> OptionalPathInfo.None
            else -> list.find { it.node == node }
                ?.let { OptionalPathInfo.Specific(it) }
                ?: OptionalPathInfo.NotFound
        }
    }

    val body: String by lazy {
        request.reader.lineSequence().joinToString("\n")
    }

    private fun forward(jsp: String) {
        request.getRequestDispatcher(jspPath(jsp)).forward(request, response)
    }

    fun renderFeed(page: String? = null) {
        page?.let { contentPage = it }
        forward("feed")
    }

    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()
    }

    fun i18n(key: String): String = ResourceBundle.getBundle("localization/strings", response.locale).getString(key)
}

/**
 * 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("/")
    }
}

// <editor-fold desc="Validators">

fun dateOptValidator(input: String?): ValidationResult<SqlDate?> {
    return if (input.isNullOrBlank()) {
        ValidatedValue(null)
    } else {
        try {
            ValidatedValue(SqlDate.valueOf(input))
        } catch (ignored: IllegalArgumentException) {
            ValidationError("validation.date.format")
        }
    }
}

fun boolValidator(input: String?): ValidationResult<Boolean> {
    return if (input.isNullOrBlank()) {
        ValidatedValue(false)
    } else {
        ValidatedValue(!(input.equals("false", true) || input == "0"))
    }
}

// </editor-fold>

mercurial