@@ -6,12 +6,18 @@ import com.fasterxml.jackson.databind.json.JsonMapper
66import java.net.Proxy
77import java.time.Clock
88import java.time.Duration
9+ import java.util.Optional
10+ import javax.net.ssl.HostnameVerifier
11+ import javax.net.ssl.SSLSocketFactory
12+ import javax.net.ssl.X509TrustManager
13+ import kotlin.jvm.optionals.getOrNull
914import org.onebusaway.client.OnebusawaySdkClient
1015import org.onebusaway.client.OnebusawaySdkClientImpl
1116import org.onebusaway.core.ClientOptions
1217import org.onebusaway.core.Timeout
1318import org.onebusaway.core.http.Headers
1419import org.onebusaway.core.http.QueryParams
20+ import org.onebusaway.core.jsonMapper
1521
1622class OnebusawaySdkOkHttpClient private constructor() {
1723
@@ -29,10 +35,63 @@ class OnebusawaySdkOkHttpClient private constructor() {
2935 class Builder internal constructor() {
3036
3137 private var clientOptions: ClientOptions .Builder = ClientOptions .builder()
32- private var timeout: Timeout = Timeout .default()
3338 private var proxy: Proxy ? = null
39+ private var sslSocketFactory: SSLSocketFactory ? = null
40+ private var trustManager: X509TrustManager ? = null
41+ private var hostnameVerifier: HostnameVerifier ? = null
3442
35- fun baseUrl (baseUrl : String ) = apply { clientOptions.baseUrl(baseUrl) }
43+ fun proxy (proxy : Proxy ? ) = apply { this .proxy = proxy }
44+
45+ /* * Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
46+ fun proxy (proxy : Optional <Proxy >) = proxy(proxy.getOrNull())
47+
48+ /* *
49+ * The socket factory used to secure HTTPS connections.
50+ *
51+ * If this is set, then [trustManager] must also be set.
52+ *
53+ * If unset, then the system default is used. Most applications should not call this method,
54+ * and instead use the system default. The default include special optimizations that can be
55+ * lost if the implementation is modified.
56+ */
57+ fun sslSocketFactory (sslSocketFactory : SSLSocketFactory ? ) = apply {
58+ this .sslSocketFactory = sslSocketFactory
59+ }
60+
61+ /* * Alias for calling [Builder.sslSocketFactory] with `sslSocketFactory.orElse(null)`. */
62+ fun sslSocketFactory (sslSocketFactory : Optional <SSLSocketFactory >) =
63+ sslSocketFactory(sslSocketFactory.getOrNull())
64+
65+ /* *
66+ * The trust manager used to secure HTTPS connections.
67+ *
68+ * If this is set, then [sslSocketFactory] must also be set.
69+ *
70+ * If unset, then the system default is used. Most applications should not call this method,
71+ * and instead use the system default. The default include special optimizations that can be
72+ * lost if the implementation is modified.
73+ */
74+ fun trustManager (trustManager : X509TrustManager ? ) = apply {
75+ this .trustManager = trustManager
76+ }
77+
78+ /* * Alias for calling [Builder.trustManager] with `trustManager.orElse(null)`. */
79+ fun trustManager (trustManager : Optional <X509TrustManager >) =
80+ trustManager(trustManager.getOrNull())
81+
82+ /* *
83+ * The verifier used to confirm that response certificates apply to requested hostnames for
84+ * HTTPS connections.
85+ *
86+ * If unset, then a default hostname verifier is used.
87+ */
88+ fun hostnameVerifier (hostnameVerifier : HostnameVerifier ? ) = apply {
89+ this .hostnameVerifier = hostnameVerifier
90+ }
91+
92+ /* * Alias for calling [Builder.hostnameVerifier] with `hostnameVerifier.orElse(null)`. */
93+ fun hostnameVerifier (hostnameVerifier : Optional <HostnameVerifier >) =
94+ hostnameVerifier(hostnameVerifier.getOrNull())
3695
3796 /* *
3897 * Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -49,6 +108,30 @@ class OnebusawaySdkOkHttpClient private constructor() {
49108
50109 fun clock (clock : Clock ) = apply { clientOptions.clock(clock) }
51110
111+ fun baseUrl (baseUrl : String? ) = apply { clientOptions.baseUrl(baseUrl) }
112+
113+ /* * Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
114+ fun baseUrl (baseUrl : Optional <String >) = baseUrl(baseUrl.getOrNull())
115+
116+ fun responseValidation (responseValidation : Boolean ) = apply {
117+ clientOptions.responseValidation(responseValidation)
118+ }
119+
120+ fun timeout (timeout : Timeout ) = apply { clientOptions.timeout(timeout) }
121+
122+ /* *
123+ * Sets the maximum time allowed for a complete HTTP call, not including retries.
124+ *
125+ * See [Timeout.request] for more details.
126+ *
127+ * For fine-grained control, pass a [Timeout] object.
128+ */
129+ fun timeout (timeout : Duration ) = apply { clientOptions.timeout(timeout) }
130+
131+ fun maxRetries (maxRetries : Int ) = apply { clientOptions.maxRetries(maxRetries) }
132+
133+ fun apiKey (apiKey : String ) = apply { clientOptions.apiKey(apiKey) }
134+
52135 fun headers (headers : Headers ) = apply { clientOptions.headers(headers) }
53136
54137 fun headers (headers : Map <String , Iterable <String >>) = apply {
@@ -129,30 +212,6 @@ class OnebusawaySdkOkHttpClient private constructor() {
129212 clientOptions.removeAllQueryParams(keys)
130213 }
131214
132- fun timeout (timeout : Timeout ) = apply {
133- clientOptions.timeout(timeout)
134- this .timeout = timeout
135- }
136-
137- /* *
138- * Sets the maximum time allowed for a complete HTTP call, not including retries.
139- *
140- * See [Timeout.request] for more details.
141- *
142- * For fine-grained control, pass a [Timeout] object.
143- */
144- fun timeout (timeout : Duration ) = timeout(Timeout .builder().request(timeout).build())
145-
146- fun maxRetries (maxRetries : Int ) = apply { clientOptions.maxRetries(maxRetries) }
147-
148- fun proxy (proxy : Proxy ) = apply { this .proxy = proxy }
149-
150- fun responseValidation (responseValidation : Boolean ) = apply {
151- clientOptions.responseValidation(responseValidation)
152- }
153-
154- fun apiKey (apiKey : String ) = apply { clientOptions.apiKey(apiKey) }
155-
156215 fun fromEnv () = apply { clientOptions.fromEnv() }
157216
158217 /* *
@@ -163,7 +222,15 @@ class OnebusawaySdkOkHttpClient private constructor() {
163222 fun build (): OnebusawaySdkClient =
164223 OnebusawaySdkClientImpl (
165224 clientOptions
166- .httpClient(OkHttpClient .builder().timeout(timeout).proxy(proxy).build())
225+ .httpClient(
226+ OkHttpClient .builder()
227+ .timeout(clientOptions.timeout())
228+ .proxy(proxy)
229+ .sslSocketFactory(sslSocketFactory)
230+ .trustManager(trustManager)
231+ .hostnameVerifier(hostnameVerifier)
232+ .build()
233+ )
167234 .build()
168235 )
169236 }
0 commit comments