From 20f1a23cf97bf2d0d77c3942fda23af08479dfcc Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Fri, 14 Aug 2020 13:54:55 +0200 Subject: Rename `doOpenConnectionToReadContent` to `readContent` and make it return `InputStream` --- .../main/kotlin/resolvers/shared/PackageList.kt | 4 +-- .../base/src/main/kotlin/resolvers/shared/utils.kt | 33 +++++++++++----------- 2 files changed, 19 insertions(+), 18 deletions(-) (limited to 'plugins/base/src/main/kotlin') diff --git a/plugins/base/src/main/kotlin/resolvers/shared/PackageList.kt b/plugins/base/src/main/kotlin/resolvers/shared/PackageList.kt index a9f0e618..bf59e2ff 100644 --- a/plugins/base/src/main/kotlin/resolvers/shared/PackageList.kt +++ b/plugins/base/src/main/kotlin/resolvers/shared/PackageList.kt @@ -15,7 +15,7 @@ data class PackageList( if (dokkaContext.configuration.offlineMode && url.protocol.toLowerCase() != "file") return null - val packageListStream = url.doOpenConnectionToReadContent().getInputStream() + val packageListStream = url.readContent() val (params, packages) = packageListStream .bufferedReader() @@ -39,4 +39,4 @@ data class PackageList( return PackageList(format, packages.toSet(), locations, url) } } -} \ No newline at end of file +} diff --git a/plugins/base/src/main/kotlin/resolvers/shared/utils.kt b/plugins/base/src/main/kotlin/resolvers/shared/utils.kt index cb737041..0c3f2495 100644 --- a/plugins/base/src/main/kotlin/resolvers/shared/utils.kt +++ b/plugins/base/src/main/kotlin/resolvers/shared/utils.kt @@ -1,36 +1,37 @@ package org.jetbrains.dokka.base.resolvers.shared +import java.io.InputStream import java.net.HttpURLConnection import java.net.URL import java.net.URLConnection -internal fun URL.doOpenConnectionToReadContent(timeout: Int = 10000, redirectsAllowed: Int = 16): URLConnection { - val connection = this.openConnection().apply { - connectTimeout = timeout - readTimeout = timeout - } +internal fun URL.readContent(timeout: Int = 10000, redirectsAllowed: Int = 16): InputStream { + fun URL.doOpenConnection(timeout: Int, redirectsAllowed: Int): URLConnection { + val connection = this.openConnection().apply { + connectTimeout = timeout + readTimeout = timeout + } + + when (connection) { + is HttpURLConnection -> return when (connection.responseCode) { + in 200..299 -> connection - when (connection) { - is HttpURLConnection -> { - return when (connection.responseCode) { - in 200..299 -> { - connection - } HttpURLConnection.HTTP_MOVED_PERM, HttpURLConnection.HTTP_MOVED_TEMP, HttpURLConnection.HTTP_SEE_OTHER -> { if (redirectsAllowed > 0) { val newUrl = connection.getHeaderField("Location") - URL(newUrl).doOpenConnectionToReadContent(timeout, redirectsAllowed - 1) + URL(newUrl).doOpenConnection(timeout, redirectsAllowed - 1) } else { throw RuntimeException("Too many redirects") } } - else -> { - throw RuntimeException("Unhandled http code: ${connection.responseCode}") - } + + else -> throw RuntimeException("Unhandled HTTP code: ${connection.responseCode}") } + + else -> return connection } - else -> return connection } + return doOpenConnection(timeout, redirectsAllowed).getInputStream() } -- cgit