Last active
September 16, 2017 14:35
-
-
Save iSanechek/fd086da6f25c59d61bdb525d3f678dba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap | |
| class CookieJar(vararg val cookies: Cookie = arrayOf()) : MutableMap<String, String> by (cookies.associate { it.key to it.valueWithAttributes } as HashMap<String, String>) { | |
| companion object { | |
| private fun Map<String, Any>.toCookieArray(): Array<Cookie> { | |
| return this.map { | |
| val valueList = it.value.toString().split(";").map(String::trim) | |
| val value = valueList[0] | |
| val attributes = if (valueList.size < 2) mapOf<String, String>() else { | |
| valueList.subList(1, valueList.size).associate { | |
| val k = it.split("=")[0].trim() | |
| val split = it.split("=") | |
| val v = (if (split.size > 1) split[1] else null)?.trim() | |
| k to v | |
| } | |
| } | |
| Cookie(it.key, value, attributes) | |
| }.toTypedArray() | |
| } | |
| } | |
| constructor(cookies: Map<String, Any>) : this(*cookies.toCookieArray()) | |
| fun getCookie(key: String): Cookie? { | |
| val value = this[key] ?: return null | |
| return Cookie("$key=$value") | |
| } | |
| fun setCookie(cookie: Cookie) { | |
| this[cookie.key] = cookie.valueWithAttributes | |
| } | |
| override fun toString() = this.cookies.joinToString("; ") { "${it.key}=${it.value}" } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment