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

run.qontract.core.utilities.URIUtils.kt Maven / Gradle / Ivy

Go to download

A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner

There is a newer version: 0.23.1
Show newest version
package run.qontract.core.utilities

import run.qontract.core.pattern.isPatternToken
import run.qontract.core.pattern.withoutPatternDelimiters
import java.net.URLDecoder
import java.nio.charset.StandardCharsets
import java.util.stream.Collectors

object URIUtils {
    fun parseQuery(query: String?): Map {
        if (query == null) {
            return emptyMap()
        }
        val pairs = query.split("&".toRegex()).toTypedArray()
        val queryParams = HashMap()
        for (pair in pairs) {
            val idx = pair.indexOf("=")
            if(idx < 0) {
                throw Exception("a part of the query string does not seem to be a key-value pair: $pair")
            }
            queryParams[URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.toString())] = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.toString())
        }
        return queryParams
    }

    fun parsePathParams(rawPath: String): Map {
        val pathParts = rawPath.split("/".toRegex()).toTypedArray()
        val pathParams = HashMap()
        for (pathPart in java.util.Arrays.stream(pathParts).filter { isPatternToken(it) }.collect(Collectors.toList())) {
            val nameAndType = getNameAndType(pathPart)
            pathParams[nameAndType[0]] = "(" + nameAndType[1] + ")"
        }
        return pathParams
    }

    private fun getNameAndType(placeHolder: String): Array {
        return withoutPatternDelimiters(placeHolder).split(":".toRegex()).toTypedArray()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy