-
Notifications
You must be signed in to change notification settings - Fork 69
/
ExternalInfinispanClient.java
153 lines (132 loc) · 5.9 KB
/
ExternalInfinispanClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.keycloak.benchmark.crossdc.client;
import org.apache.http.client.utils.URIBuilder;
import org.keycloak.benchmark.crossdc.util.InfinispanUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.keycloak.benchmark.crossdc.AbstractCrossDCTest.ISPN_USERNAME;
import static org.keycloak.benchmark.crossdc.AbstractCrossDCTest.MAIN_PASSWORD;
import static org.keycloak.benchmark.crossdc.util.InfinispanUtils.getBasicAuthenticationHeader;
public class ExternalInfinispanClient implements InfinispanClient {
private final HttpClient httpClient;
private final String infinispanUrl;
private final String username;
private final String password;
Pattern UUID_REGEX = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
public ExternalInfinispanClient(HttpClient httpClient, String infinispanUrl, String username, String password) {
assertNotNull(infinispanUrl, "Infinispan URL cannot be null");
this.httpClient = httpClient;
this.infinispanUrl = infinispanUrl;
this.username = username;
this.password = password;
}
public class ExternalCache implements InfinispanClient.Cache {
private final String cacheName;
private ExternalCache(String cacheName) {
this.cacheName = cacheName;
}
@Override
public long size() {
URI uri = null;
try {
uri = new URIBuilder(infinispanUrl + "/rest/v2/caches/" + cacheName + "/")
.addParameter("action", "size")
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header("Authorization", getBasicAuthenticationHeader(ISPN_USERNAME, MAIN_PASSWORD))
.build();
try {
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
if (cacheName.equals(InfinispanUtils.SESSIONS) || cacheName.equals(InfinispanUtils.CLIENT_SESSIONS)) {
return Long.parseLong(response.body()) - KeycloakClient.getCurrentlyInitializedAdminClients();
}
return Long.parseLong(response.body());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void clear() {
URI uri = null;
try {
uri = new URIBuilder(infinispanUrl + "/rest/v2/caches/" + cacheName + "/")
.addParameter("action","clear")
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9")
.header("Authorization", getBasicAuthenticationHeader(username, password))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Exception thrown for cache " + cacheName, e);
}
assertEquals(204, response.statusCode());
}
@Override
public boolean contains(String key) {
return keys().contains(key);
}
@Override
public Set<String> keys() {
URI uri = null;
try {
uri = new URIBuilder(infinispanUrl + "/rest/v2/caches/" + cacheName + "/")
.addParameter("action", "keys")
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header("Authorization", getBasicAuthenticationHeader(ISPN_USERNAME, MAIN_PASSWORD))
.build();
try {
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
Set<String> keys = Arrays.stream(response.body().split(","))
.map(UUID_REGEX::matcher)
.map(m -> {
if (m.find()) {
return m.group();
} else {
return null;
}
}).filter(Objects::nonNull)
.collect(Collectors.toSet());
if (cacheName.equals(InfinispanUtils.SESSIONS)) {
return KeycloakClient.removeAdminClientSessions(keys);
}
return keys;
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@Override
public Cache cache(String name) {
return new ExternalCache(name);
}
}