From 27200057b8e9a1785defcaaceb227916ea38b727 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 13 May 2019 20:55:47 -0400 Subject: [PATCH] Adopt idiomatic Kotlin in Challenge --- .../main/java/okhttp3/JavaNetAuthenticator.kt | 12 +-- okhttp/src/main/java/okhttp3/Authenticator.kt | 2 +- okhttp/src/main/java/okhttp3/Challenge.kt | 81 +++++++++++++------ okhttp/src/main/java/okhttp3/CookieJar.kt | 8 +- okhttp/src/main/java/okhttp3/Dns.kt | 7 +- 5 files changed, 69 insertions(+), 41 deletions(-) diff --git a/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt b/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt index c0bc87105..90133e3d3 100644 --- a/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt +++ b/okhttp-urlconnection/src/main/java/okhttp3/JavaNetAuthenticator.kt @@ -35,7 +35,7 @@ class JavaNetAuthenticator : okhttp3.Authenticator { val proxy = route?.proxy() ?: Proxy.NO_PROXY for (challenge in challenges) { - if (!"Basic".equals(challenge.scheme(), ignoreCase = true)) { + if (!"Basic".equals(challenge.scheme, ignoreCase = true)) { continue } @@ -46,8 +46,8 @@ class JavaNetAuthenticator : okhttp3.Authenticator { proxy.connectToInetAddress(url), proxyAddress.port, url.scheme(), - challenge.realm(), - challenge.scheme(), + challenge.realm, + challenge.scheme, url.url(), Authenticator.RequestorType.PROXY ) @@ -57,8 +57,8 @@ class JavaNetAuthenticator : okhttp3.Authenticator { proxy.connectToInetAddress(url), url.port(), url.scheme(), - challenge.realm(), - challenge.scheme(), + challenge.realm, + challenge.scheme, url.url(), Authenticator.RequestorType.SERVER ) @@ -67,7 +67,7 @@ class JavaNetAuthenticator : okhttp3.Authenticator { if (auth != null) { val credentialHeader = if (proxyAuthorization) "Proxy-Authorization" else "Authorization" val credential = Credentials.basic( - auth.userName, String(auth.password), challenge.charset()) + auth.userName, String(auth.password), challenge.charset) return request.newBuilder() .header(credentialHeader, credential) .build() diff --git a/okhttp/src/main/java/okhttp3/Authenticator.kt b/okhttp/src/main/java/okhttp3/Authenticator.kt index 31031f387..86a160906 100644 --- a/okhttp/src/main/java/okhttp3/Authenticator.kt +++ b/okhttp/src/main/java/okhttp3/Authenticator.kt @@ -98,7 +98,7 @@ import java.io.IOException interface Authenticator { /** * Returns a request that includes a credential to satisfy an authentication challenge in - * `response`. Returns null if the challenge cannot be satisfied. + * [response]. Returns null if the challenge cannot be satisfied. * * The route is best effort, it currently may not always be provided even when logically * available. It may also not be provided when an authenticator is re-used manually in an diff --git a/okhttp/src/main/java/okhttp3/Challenge.kt b/okhttp/src/main/java/okhttp3/Challenge.kt index c69b7950d..b5a92463f 100644 --- a/okhttp/src/main/java/okhttp3/Challenge.kt +++ b/okhttp/src/main/java/okhttp3/Challenge.kt @@ -21,12 +21,41 @@ import java.util.Collections.singletonMap import java.util.Collections.unmodifiableMap import java.util.Locale.US -/** An RFC 7235 challenge. */ +/** + * An [RFC 7235][rfc_7235] challenge. + * + * [rfc_7235]: https://tools.ietf.org/html/rfc7235 + */ class Challenge( - private val scheme: String, + /** Returns the authentication scheme, like `Basic`. */ + @get:JvmName("scheme") val scheme: String, + authParams: Map ) { - private val authParams: Map + /** + * Returns the auth params, including [realm] and [charset] if present, but as + * strings. The map's keys are lowercase and should be treated case-insensitively. + */ + @get:JvmName("authParams") val authParams: Map + + /** Returns the protection space. */ + @get:JvmName("realm") val realm: String? + get() = authParams["realm"] + + /** The charset that should be used to encode the credentials. */ + @get:JvmName("charset") val charset: Charset + get() { + val charset = authParams["charset"] + if (charset != null) { + try { + return Charset.forName(charset) + } catch (ignore: Exception) { + } + } + return ISO_8859_1 + } + + constructor(scheme: String, realm: String) : this(scheme, singletonMap("realm", realm)) init { val newAuthParams = mutableMapOf() @@ -37,8 +66,6 @@ class Challenge( this.authParams = unmodifiableMap(newAuthParams) } - constructor(scheme: String, realm: String) : this(scheme, singletonMap("realm", realm)) - /** Returns a copy of this charset that expects a credential encoded with [charset]. */ fun withCharset(charset: Charset): Challenge { val authParams = this.authParams.toMutableMap() @@ -46,29 +73,33 @@ class Challenge( return Challenge(scheme, authParams) } - /** Returns the authentication scheme, like `Basic`. */ + @JvmName("-deprecated_scheme") + @Deprecated( + message = "moved to val", + replaceWith = ReplaceWith(expression = "scheme"), + level = DeprecationLevel.WARNING) fun scheme() = scheme - /** - * Returns the auth params, including [realm] and [charset] if present, but as - * strings. The map's keys are lowercase and should be treated case-insensitively. - */ + @JvmName("-deprecated_authParams") + @Deprecated( + message = "moved to val", + replaceWith = ReplaceWith(expression = "authParams"), + level = DeprecationLevel.WARNING) fun authParams() = authParams - /** Returns the protection space. */ - fun realm(): String? = authParams["realm"] + @JvmName("-deprecated_realm") + @Deprecated( + message = "moved to val", + replaceWith = ReplaceWith(expression = "realm"), + level = DeprecationLevel.WARNING) + fun realm(): String? = realm - /** Returns the charset that should be used to encode the credentials. */ - fun charset(): Charset { - val charset = authParams["charset"] - if (charset != null) { - try { - return Charset.forName(charset) - } catch (ignore: Exception) { - } - } - return ISO_8859_1 - } + @JvmName("-deprecated_charset") + @Deprecated( + message = "moved to val", + replaceWith = ReplaceWith(expression = "charset"), + level = DeprecationLevel.WARNING) + fun charset(): Charset = charset override fun equals(other: Any?): Boolean { return other is Challenge && @@ -83,7 +114,5 @@ class Challenge( return result } - override fun toString(): String { - return "$scheme authParams=$authParams" - } + override fun toString() = "$scheme authParams=$authParams" } \ No newline at end of file diff --git a/okhttp/src/main/java/okhttp3/CookieJar.kt b/okhttp/src/main/java/okhttp3/CookieJar.kt index 8ae48fd7c..cae9aebc8 100644 --- a/okhttp/src/main/java/okhttp3/CookieJar.kt +++ b/okhttp/src/main/java/okhttp3/CookieJar.kt @@ -31,20 +31,20 @@ package okhttp3 */ interface CookieJar { /** - * Saves `cookies` from an HTTP response to this store according to this jar's policy. + * Saves [cookies] from an HTTP response to this store according to this jar's policy. * * Note that this method may be called a second time for a single HTTP response if the response - * includes a trailer. For this obscure HTTP feature, `cookies` contains only the trailer's + * includes a trailer. For this obscure HTTP feature, [cookies] contains only the trailer's * cookies. */ fun saveFromResponse(url: HttpUrl, cookies: List) /** - * Load cookies from the jar for an HTTP request to `url`. This method returns a possibly + * Load cookies from the jar for an HTTP request to [url]. This method returns a possibly * empty list of cookies for the network request. * * Simple implementations will return the accepted cookies that have not yet expired and that - * [match][Cookie.matches] `url`. + * [match][Cookie.matches] [url]. */ fun loadForRequest(url: HttpUrl): List diff --git a/okhttp/src/main/java/okhttp3/Dns.kt b/okhttp/src/main/java/okhttp3/Dns.kt index 4aef7fd93..b2aa4acf2 100644 --- a/okhttp/src/main/java/okhttp3/Dns.kt +++ b/okhttp/src/main/java/okhttp3/Dns.kt @@ -47,10 +47,9 @@ interface Dns { try { return InetAddress.getAllByName(hostname).toList() } catch (e: NullPointerException) { - val unknownHostException = UnknownHostException( - "Broken system behaviour for dns lookup of $hostname") - unknownHostException.initCause(e) - throw unknownHostException + throw UnknownHostException("Broken system behaviour for dns lookup of $hostname").apply { + initCause(e) + } } } }