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

com.github.jchanghong.http.utils.JchCookieJar.kt Maven / Gradle / Ivy

package com.github.jchanghong.http.utils


import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import java.security.cert.X509Certificate
import java.util.concurrent.ConcurrentHashMap
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLSession
import javax.net.ssl.X509TrustManager

class JchCookieJar(val cookieStore: ConcurrentHashMap>) : CookieJar {
    override fun saveFromResponse(url: HttpUrl, cookies: List) {
        for (cookie in cookies) {
            val hashMap = cookieStore.getOrPut(cookie.domain + cookie.path) { ConcurrentHashMap() }
            hashMap[cookie.name] = cookie
        }
//                        info("cookieJar saveFromResponse ${url.host}" + cookies.joinToString { it.name + it.value })
    }

    override fun loadForRequest(url: HttpUrl): List {
        val pathone = url.pathSegments.firstOrNull()
        val hashMap = ConcurrentHashMap()
        val roothashMap = cookieStore.getOrPut(url.host + "/") { ConcurrentHashMap() }
        hashMap.putAll(roothashMap)
        if (!pathone.isNullOrBlank()) {
            val roothashMap2 = cookieStore.getOrPut(url.host + "/${pathone}") { ConcurrentHashMap() }
            hashMap.putAll(roothashMap2)
        }
//                    info("cookieJar loadForRequest ${url.host}${pathone} :${hashMap.values}")
        return hashMap.values.toList()
    }
}

class JchTrustAllCerts : X509TrustManager {
    override fun checkClientTrusted(chain: Array, authType: String?) {
//            info(authType+chain.firstOrNull().toString())
    }

    override fun checkServerTrusted(chain: Array, authType: String?) {
//            info(authType+chain.firstOrNull().toString())
    }

    override fun getAcceptedIssuers(): Array {
        return arrayOf()
    }

}

class JchTrustAllHostnameVerifier : HostnameVerifier {
    override fun verify(hostname: String?, session: SSLSession?): Boolean {
//            info("TrustAllHostnameVerifier"+hostname + session.toString())
        return true
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy