Skip to content

Commit 43a8ddb

Browse files
committed
Merge branch 'master' of github.com:UnterrainerInformatik/java-rest-client
2 parents 1b05a0b + e96a1e7 commit 43a8ddb

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
24

35
<parent>
46
<groupId>info.unterrainer.commons</groupId>
@@ -36,5 +38,10 @@
3638
<artifactId>okio</artifactId>
3739
<version>3.6.0</version>
3840
</dependency>
41+
<dependency>
42+
<groupId>io.github.rburgst</groupId>
43+
<artifactId>okhttp-digest</artifactId>
44+
<version>3.0</version>
45+
</dependency>
3946
</dependencies>
4047
</project>

src/main/java/info/unterrainer/commons/restclient/RestClient.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package info.unterrainer.commons.restclient;
22

33
import java.io.IOException;
4+
import java.util.Map;
45
import java.util.Random;
6+
import java.util.concurrent.ConcurrentHashMap;
57
import java.util.concurrent.TimeUnit;
68

9+
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
10+
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
11+
import com.burgstaller.okhttp.DispatchingAuthenticator;
12+
import com.burgstaller.okhttp.basic.BasicAuthenticator;
13+
import com.burgstaller.okhttp.digest.CachingAuthenticator;
14+
import com.burgstaller.okhttp.digest.Credentials;
15+
import com.burgstaller.okhttp.digest.DigestAuthenticator;
16+
717
import info.unterrainer.commons.restclient.exceptions.RestClientException;
818
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
919
import lombok.experimental.Accessors;
1020
import lombok.extern.slf4j.Slf4j;
1121
import okhttp3.Call;
12-
import okhttp3.Credentials;
1322
import okhttp3.Headers;
1423
import okhttp3.MediaType;
1524
import okhttp3.OkHttpClient;
@@ -25,6 +34,7 @@ public class RestClient {
2534
private final Random random = new Random();
2635

2736
protected OkHttpClient client;
37+
protected final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
2838
protected final JsonMapper jsonMapper;
2939

3040
public RestClient(final JsonMapper jsonMapper) {
@@ -50,11 +60,20 @@ public RestClient(final JsonMapper jsonMapper, final String userName, final Stri
5060
.writeTimeout(writeTimeoutInMillis, TimeUnit.MILLISECONDS)
5161
.addInterceptor(new GzipInterceptor())
5262
.followRedirects(true);
53-
if (userName != null || password != null)
54-
c.authenticator((route, response) -> {
55-
String credential = Credentials.basic(userName, password);
56-
return response.request().newBuilder().header("Authorization", credential).build();
57-
});
63+
if (userName != null || password != null) {
64+
Credentials credentials = new Credentials(userName, password);
65+
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
66+
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
67+
68+
// Note that all authentication schemes should be registered as lower-case!
69+
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder()
70+
.with("digest", digestAuthenticator)
71+
.with("basic", basicAuthenticator)
72+
.build();
73+
74+
c.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache))
75+
.addInterceptor(new AuthenticationCacheInterceptor(authCache));
76+
}
5877
client = c.build();
5978
}
6079

0 commit comments

Comments
 (0)