All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.server.impl.matcher.ParameterPathMatcher.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.server.impl.matcher

import com.fireflysource.net.http.server.Matcher
import com.fireflysource.net.http.server.Router
import java.util.*


class ParameterPathMatcher : Matcher {

    companion object {
        fun isParameterPath(path: String): Boolean {
            val paths = split(path)
            return paths.any { it[0] == ':' }
        }

        fun split(path: String): List {
            val paths: MutableList = LinkedList()
            var start = 1
            val last = path.lastIndex

            for (i in 1..last) {
                if (path[i] == '/') {
                    paths.add(path.substring(start, i).trim())
                    start = i + 1
                }
            }

            if (path[last] != '/') {
                paths.add(path.substring(start).trim())
            }
            return paths
        }
    }

    private val parameterRuleMap: MutableMap> =
        HashMap>()

    private inner class ParameterRule(val rule: String) {

        val paths = split(rule)

        fun match(list: List): Map {
            if (paths.size != list.size) return emptyMap()

            val param: MutableMap = HashMap()
            for (i in list.indices) {
                val path = paths[i]
                val value = list[i]
                if (path[0] != ':') {
                    if (path != value) {
                        return emptyMap()
                    }
                } else {
                    param[path.substring(1)] = value
                }
            }
            return param
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false

            other as ParameterRule
            return rule == other.rule
        }

        override fun hashCode(): Int {
            return rule.hashCode()
        }
    }

    override fun getMatchType(): Matcher.MatchType {
        return Matcher.MatchType.PATH
    }

    override fun add(rule: String, router: Router) {
        val parameterRule = ParameterRule(rule)
        parameterRuleMap.computeIfAbsent(parameterRule) { TreeSet() }.add(router)
    }

    override fun match(value: String): Matcher.MatchResult? {
        if (parameterRuleMap.isEmpty()) return null

        val routers = TreeSet()
        val parameters = HashMap>()
        val paths = split(value)

        parameterRuleMap.forEach { (rule, routerSet) ->
            val param = rule.match(paths)
            if (param.isNotEmpty()) {
                routers.addAll(routerSet)
                routerSet.forEach { router -> parameters[router] = param }
            }
        }
        return if (routers.isEmpty()) null else Matcher.MatchResult(routers, parameters, matchType)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy