Skip to content

Commit 2568fbd

Browse files
feat(client): support proxy authentication
1 parent 720bb86 commit 2568fbd

5 files changed

Lines changed: 257 additions & 85 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ CasParserClient client = CasParserOkHttpClient.builder()
342342
.build();
343343
```
344344

345+
If the proxy responds with `407 Proxy Authentication Required`, supply credentials by also configuring `proxyAuthenticator`:
346+
347+
```java
348+
import com.cas_parser.api.client.CasParserClient;
349+
import com.cas_parser.api.client.okhttp.CasParserOkHttpClient;
350+
import com.cas_parser.api.core.http.ProxyAuthenticator;
351+
352+
CasParserClient client = CasParserOkHttpClient.builder()
353+
.fromEnv()
354+
.proxy(...)
355+
// Or a custom implementation of `ProxyAuthenticator`.
356+
.proxyAuthenticator(ProxyAuthenticator.basic("username", "password"))
357+
.build();
358+
```
359+
345360
### Connection pooling
346361

347362
To customize the underlying OkHttp connection pool, configure the client using the `maxIdleConnections` and `keepAliveDuration` methods:

cas-parser-java-client-okhttp/src/main/kotlin/com/cas_parser/api/client/okhttp/CasParserOkHttpClient.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.cas_parser.api.core.Sleeper
99
import com.cas_parser.api.core.Timeout
1010
import com.cas_parser.api.core.http.Headers
1111
import com.cas_parser.api.core.http.HttpClient
12+
import com.cas_parser.api.core.http.ProxyAuthenticator
1213
import com.cas_parser.api.core.http.QueryParams
1314
import com.cas_parser.api.core.jsonMapper
1415
import com.fasterxml.jackson.databind.json.JsonMapper
@@ -47,6 +48,7 @@ class CasParserOkHttpClient private constructor() {
4748
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
4849
private var dispatcherExecutorService: ExecutorService? = null
4950
private var proxy: Proxy? = null
51+
private var proxyAuthenticator: ProxyAuthenticator? = null
5052
private var maxIdleConnections: Int? = null
5153
private var keepAliveDuration: Duration? = null
5254
private var sslSocketFactory: SSLSocketFactory? = null
@@ -77,6 +79,20 @@ class CasParserOkHttpClient private constructor() {
7779
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
7880
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
7981

82+
/**
83+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
84+
* Required`.
85+
*/
86+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
87+
this.proxyAuthenticator = proxyAuthenticator
88+
}
89+
90+
/**
91+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
92+
*/
93+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
94+
proxyAuthenticator(proxyAuthenticator.getOrNull())
95+
8096
/**
8197
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
8298
*
@@ -363,6 +379,7 @@ class CasParserOkHttpClient private constructor() {
363379
OkHttpClient.builder()
364380
.timeout(clientOptions.timeout())
365381
.proxy(proxy)
382+
.proxyAuthenticator(proxyAuthenticator)
366383
.maxIdleConnections(maxIdleConnections)
367384
.keepAliveDuration(keepAliveDuration)
368385
.dispatcherExecutorService(dispatcherExecutorService)

cas-parser-java-client-okhttp/src/main/kotlin/com/cas_parser/api/client/okhttp/CasParserOkHttpClientAsync.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.cas_parser.api.core.Sleeper
99
import com.cas_parser.api.core.Timeout
1010
import com.cas_parser.api.core.http.Headers
1111
import com.cas_parser.api.core.http.HttpClient
12+
import com.cas_parser.api.core.http.ProxyAuthenticator
1213
import com.cas_parser.api.core.http.QueryParams
1314
import com.cas_parser.api.core.jsonMapper
1415
import com.fasterxml.jackson.databind.json.JsonMapper
@@ -47,6 +48,7 @@ class CasParserOkHttpClientAsync private constructor() {
4748
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
4849
private var dispatcherExecutorService: ExecutorService? = null
4950
private var proxy: Proxy? = null
51+
private var proxyAuthenticator: ProxyAuthenticator? = null
5052
private var maxIdleConnections: Int? = null
5153
private var keepAliveDuration: Duration? = null
5254
private var sslSocketFactory: SSLSocketFactory? = null
@@ -77,6 +79,20 @@ class CasParserOkHttpClientAsync private constructor() {
7779
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
7880
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
7981

82+
/**
83+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
84+
* Required`.
85+
*/
86+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
87+
this.proxyAuthenticator = proxyAuthenticator
88+
}
89+
90+
/**
91+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
92+
*/
93+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
94+
proxyAuthenticator(proxyAuthenticator.getOrNull())
95+
8096
/**
8197
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
8298
*
@@ -363,6 +379,7 @@ class CasParserOkHttpClientAsync private constructor() {
363379
OkHttpClient.builder()
364380
.timeout(clientOptions.timeout())
365381
.proxy(proxy)
382+
.proxyAuthenticator(proxyAuthenticator)
366383
.maxIdleConnections(maxIdleConnections)
367384
.keepAliveDuration(keepAliveDuration)
368385
.dispatcherExecutorService(dispatcherExecutorService)

0 commit comments

Comments
 (0)