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

in.specmatic.core.utilities.URIUtils.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends. Deprecation Notice for group ID "in.specmatic" ****************************************************************************************************** Updates for "specmatic-core" will no longer be available under the deprecated group ID "in.specmatic". Please update your dependencies to use the new group ID "io.specmatic". ******************************************************************************************************

There is a newer version: 1.3.39
Show newest version
package `in`.specmatic.core.utilities

import `in`.specmatic.core.pattern.isPatternToken
import `in`.specmatic.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