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

Wed, 06 Jan 2021 16:20:32 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Jan 2021 16:20:32 +0100
changeset 181
502a10e24b85
parent 179
623c340058f3
child 184
e8eecee6aadf
permissions
-rw-r--r--

renames several resource keys

/*
 * 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 kotlin.math.min

/**
 * Maps requests to methods.
 *
 * This annotation is used to annotate methods within classes which
 * override [AbstractServlet].
 */
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class RequestMapping(

    /**
     * Specifies the HTTP method.
     *
     * @return the HTTP method handled by the annotated Java method
     */
    val method: HttpMethod,

    /**
     * Specifies the request path relative to the module path.
     * The trailing slash is important.
     * A node may start with a dollar ($) sign.
     * This part of the path is then treated as an path parameter.
     * Path parameters can be obtained by including the [PathParameters] type in the signature.
     *
     * @return the request path the annotated method should handle
     */
    val requestPath: String = "/"
)

class PathParameters : HashMap<String, String>()

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

mercurial