From 3a0fc31c90cb8236a923ff519c72be5b5bb1f4bb Mon Sep 17 00:00:00 2001 From: Junya Awano Date: Wed, 6 Jun 2018 14:40:58 +0900 Subject: [PATCH] #213 obtain proxy settings from JVM arguments --- .../ExternalDocumentationLinkResolver.kt | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/Kotlin/ExternalDocumentationLinkResolver.kt b/core/src/main/kotlin/Kotlin/ExternalDocumentationLinkResolver.kt index e19ecf76ca..60b1f0ba2e 100644 --- a/core/src/main/kotlin/Kotlin/ExternalDocumentationLinkResolver.kt +++ b/core/src/main/kotlin/Kotlin/ExternalDocumentationLinkResolver.kt @@ -16,9 +16,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.parents import java.io.ByteArrayOutputStream import java.io.PrintWriter -import java.net.HttpURLConnection -import java.net.URL -import java.net.URLConnection +import java.net.* import java.nio.file.Path import java.security.MessageDigest @@ -42,7 +40,26 @@ class ExternalDocumentationLinkResolver @Inject constructor( val cachedProtocols = setOf("http", "https", "ftp") fun URL.doOpenConnectionToReadContent(timeout: Int = 10000, redirectsAllowed: Int = 16): URLConnection { - val connection = this.openConnection() + + val proxyHost: String? = System.getProperty("http.proxyHost") + val proxyPort: String? = System.getProperty("http.proxyPort") + + val connection = if(proxyHost != null && proxyPort != null){ + val proxyUser: String? = System.getProperty("http.proxyUser") + val proxyPassword: String? = System.getProperty("http.proxyPassword") + if(proxyUser != null && proxyPassword != null){ + Authenticator.setDefault( object : Authenticator() { + override fun getPasswordAuthentication(): PasswordAuthentication { + return PasswordAuthentication(proxyUser,proxyPassword.toCharArray()) + } + }) + } + this.openConnection(Proxy(Proxy.Type.HTTP, + InetSocketAddress(proxyHost,Integer.parseInt(proxyPort)))) + }else{ + this.openConnection() + } + connection.connectTimeout = timeout connection.readTimeout = timeout