Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Address to Kotlin #4787

Merged
merged 6 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Convert Address to Kotlin
  • Loading branch information
ShaishavGandhi committed Mar 26, 2019
commit e2af8b17b510b4e9628ac2ac8750e4db42186fad
17 changes: 15 additions & 2 deletions okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ apply plugin: 'me.champeau.gradle.japicmp'
task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask, dependsOn: 'jar') {
oldClasspath = files(baselineJar(project, baselineVersion))
newClasspath = files(jar.archivePath)
onlyBinaryIncompatibleModified = true
failOnModification = true
onlyBinaryIncompatibleModified = false
failOnModification = false
txtOutputFile = file("$buildDir/reports/japi.txt")
ignoreMissingClasses = true
includeSynthetic = true
Expand Down Expand Up @@ -98,6 +98,19 @@ task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask, dependsOn: 'jar') {
'okhttp3.Cookie$Builder#path(java.lang.String)',
'okhttp3.Cookie$Builder#secure()',
'okhttp3.Cookie$Builder#value(java.lang.String)',
'okhttp3.Address#certificatePinner()',
'okhttp3.Address#connectionSpecs()',
'okhttp3.Address#dns()',
'okhttp3.Address#equalsNonHost()', // Remove this
'okhttp3.Address#hostnameVerifier()',
'okhttp3.Address#protocols()',
'okhttp3.Address#proxy()',
'okhttp3.Address#proxyAuthenticator()',
'okhttp3.Address#proxySelector()',
'okhttp3.Address#sslSocketFactory()',
'okhttp3.Address#url()',
'okhttp3.Address#socketFactory()',
'okhttp3.TlsVersion#javaName()',
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
'okhttp3.FormBody#encodedName(int)',
'okhttp3.FormBody#encodedValue(int)',
'okhttp3.FormBody#name(int)',
Expand Down
201 changes: 0 additions & 201 deletions okhttp/src/main/java/okhttp3/Address.java

This file was deleted.

156 changes: 156 additions & 0 deletions okhttp/src/main/java/okhttp3/Address.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3

import java.net.Proxy
import java.net.ProxySelector
import java.util.Objects
import javax.net.SocketFactory
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLSocketFactory
import okhttp3.internal.Util

/**
* A specification for a connection to an origin server. For simple connections, this is the
* server's hostname and port. If an explicit proxy is requested (or [no][Proxy.NO_PROXY] is explicitly requested), this also includes that proxy information. For secure
swankjesse marked this conversation as resolved.
Show resolved Hide resolved
* connections the address also includes the SSL socket factory, hostname verifier, and certificate
* pinner.
*
* HTTP requests that share the same `Address` may also share the same [Connection].
*/
class Address(
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
uriHost: String,
uriPort: Int,
private val dns: Dns,
private val socketFactory: SocketFactory,
private val sslSocketFactory: SSLSocketFactory?,
private val hostnameVerifier: HostnameVerifier?,
private val certificatePinner: CertificatePinner?,
private val proxyAuthenticator: Authenticator,
private val proxy: Proxy?,
protocols: List<Protocol>,
connectionSpecs: List<ConnectionSpec>,
private val proxySelector: ProxySelector
) {
private val url: HttpUrl = HttpUrl.Builder()
.scheme(if (sslSocketFactory != null) "https" else "http")
.host(uriHost)
.port(uriPort)
.build()

/**
* The protocols the client supports. This method always returns a non-null list that
* contains minimally [Protocol.HTTP_1_1].
*/
private val protocols: List<Protocol> = Util.immutableList(protocols)
swankjesse marked this conversation as resolved.
Show resolved Hide resolved

private val connectionSpecs: List<ConnectionSpec> = Util.immutableList(connectionSpecs)

/**
* Returns a URL with the hostname and port of the origin server. The path, query, and fragment of
* this URL are always empty, since they are not significant for planning a route.
*/
fun url() = url

/** Returns the service that will be used to resolve IP addresses for hostnames. */
fun dns() = dns

/** Returns the socket factory for new connections. */
fun socketFactory(): SocketFactory {
return socketFactory
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
}

/** Returns the client's proxy authenticator. */
fun proxyAuthenticator() = proxyAuthenticator

/**
* Returns the protocols the client supports. This method always returns a non-null list that
* contains minimally {@link Protocol#HTTP_1_1}.
*/
fun protocols() = protocols

fun connectionSpecs() = connectionSpecs

/**
* Returns this address's proxy selector. Only used if the proxy is null. If none of this
* selector's proxies are reachable, a direct connection will be attempted.
*/
fun proxySelector() = proxySelector

/**
* Returns this address's explicitly-specified HTTP proxy, or null to delegate to the {@linkplain
* #proxySelector proxy selector}.
*/
fun proxy() = proxy

/** Returns the SSL socket factory, or null if this is not an HTTPS address. */
fun sslSocketFactory() = sslSocketFactory

/** Returns the hostname verifier, or null if this is not an HTTPS address. */
fun hostnameVerifier() = hostnameVerifier

/** Returns this address's certificate pinner, or null if this is not an HTTPS address. */
fun certificatePinner() = certificatePinner

override fun equals(other: Any?): Boolean {
return (other is Address
&& url == other.url
&& equalsNonHost((other as Address?)!!))
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
}

override fun hashCode(): Int {
var result = 17
result = 31 * result + url.hashCode()
result = 31 * result + dns.hashCode()
result = 31 * result + proxyAuthenticator.hashCode()
result = 31 * result + protocols.hashCode()
result = 31 * result + connectionSpecs.hashCode()
result = 31 * result + proxySelector.hashCode()
result = 31 * result + Objects.hashCode(proxy)
result = 31 * result + Objects.hashCode(sslSocketFactory)
result = 31 * result + Objects.hashCode(hostnameVerifier)
result = 31 * result + Objects.hashCode(certificatePinner)
return result
}

fun equalsNonHost(that: Address): Boolean {
return (this.dns == that.dns
&& this.proxyAuthenticator == that.proxyAuthenticator
&& this.protocols == that.protocols
&& this.connectionSpecs == that.connectionSpecs
&& this.proxySelector == that.proxySelector
&& this.proxy == that.proxy
&& this.sslSocketFactory == that.sslSocketFactory
&& this.hostnameVerifier == that.hostnameVerifier
&& this.certificatePinner == that.certificatePinner
&& this.url().port() == that.url().port())
}

override fun toString(): String {
val result = StringBuilder()
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
.append("Address{")
.append(url.host()).append(":").append(url.port())

if (proxy != null) {
result.append(", proxy=").append(proxy)
} else {
result.append(", proxySelector=").append(proxySelector)
}

result.append("}")
return result.toString()
}
}
Loading