-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConfigFetcher.java
191 lines (164 loc) · 8.22 KB
/
ConfigFetcher.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.configcat;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import java.io.Closeable;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
class ConfigFetcher implements Closeable {
private final AtomicBoolean isClosed = new AtomicBoolean(false);
private final ConfigCatLogger logger;
private final OkHttpClient httpClient;
private final String mode;
private final String sdkKey;
private final boolean urlIsCustom;
private String url;
enum RedirectMode {
NoRedirect,
ShouldRedirect,
ForceRedirect
}
ConfigFetcher(OkHttpClient httpClient,
ConfigCatLogger logger,
String sdkKey,
String url,
boolean urlIsCustom,
String pollingIdentifier) {
this.logger = logger;
this.sdkKey = sdkKey;
this.urlIsCustom = urlIsCustom;
this.url = url;
this.httpClient = httpClient;
this.mode = pollingIdentifier;
}
public CompletableFuture<FetchResponse> fetchAsync(String eTag) {
return this.executeFetchAsync(2, eTag);
}
private CompletableFuture<FetchResponse> executeFetchAsync(int executionCount, String eTag) {
return this.getResponseAsync(eTag).thenComposeAsync(fetchResponse -> {
if (!fetchResponse.isFetched()) {
return CompletableFuture.completedFuture(fetchResponse);
}
try {
Entry entry = fetchResponse.entry();
Config config = entry.getConfig();
if (config.getPreferences() == null) {
return CompletableFuture.completedFuture(fetchResponse);
}
String newUrl = config.getPreferences().getBaseUrl();
if (newUrl.equals(this.url)) {
return CompletableFuture.completedFuture(fetchResponse);
}
int redirect = config.getPreferences().getRedirect();
// we have a custom url set, and we didn't get a forced redirect
if (this.urlIsCustom && redirect != RedirectMode.ForceRedirect.ordinal()) {
return CompletableFuture.completedFuture(fetchResponse);
}
this.url = newUrl;
if (redirect == RedirectMode.NoRedirect.ordinal()) { // no redirect
return CompletableFuture.completedFuture(fetchResponse);
} else { // redirect
if (redirect == RedirectMode.ShouldRedirect.ordinal()) {
this.logger.warn("Your builder.dataGovernance() parameter at ConfigCatClient " +
"initialization is not in sync with your preferences on the ConfigCat " +
"Dashboard: https://app.configcat.com/organization/data-governance. " +
"Only Organization Admins can access this preference.");
}
if (executionCount > 0) {
return this.executeFetchAsync(executionCount - 1, eTag);
}
}
} catch (Exception exception) {
this.logger.error("Exception while trying to fetch the config.json.", exception);
return CompletableFuture.completedFuture(fetchResponse);
}
this.logger.error("Redirect loop during config.json fetch. Please contact support@configcat.com.");
return CompletableFuture.completedFuture(fetchResponse);
});
}
private CompletableFuture<FetchResponse> getResponseAsync(final String eTag) {
Request request = this.getRequest(eTag);
CompletableFuture<FetchResponse> future = new CompletableFuture<>();
this.httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
String message = "Exception while trying to fetch the config.json.";
if (!isClosed.get()) {
if (e instanceof SocketTimeoutException) {
message = "Request timed out. Timeout values: [connect: " + httpClient.connectTimeoutMillis() + "ms, read: " + httpClient.readTimeoutMillis() + "ms, write: " + httpClient.writeTimeoutMillis() + "ms]";
}
logger.error(message, e);
}
future.complete(FetchResponse.failed(message, false));
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
try (ResponseBody body = response.body()) {
logger.error("onReposnse");
if (response.isSuccessful() && body != null) {
String content = body.string();
String eTag = response.header("ETag");
Result<Config> result = deserializeConfig(content);
if (result.error() != null) {
future.complete(FetchResponse.failed(result.error(), false));
return;
}
logger.debug("Fetch was successful: new config fetched.");
future.complete(FetchResponse.fetched(new Entry(result.value(), eTag, System.currentTimeMillis())));
} else if (response.code() == 304) {
logger.debug("Fetch was successful: config not modified.");
future.complete(FetchResponse.notModified());
} else if (response.code() == 403 || response.code() == 404) {
String message = "Double-check your API KEY at https://app.configcat.com/apikey.";
logger.error(message);
future.complete(FetchResponse.failed(message, true));
} else {
String message = "Unexpected HTTP response received: " + response.code() + " " + response.message();
logger.error(message);
future.complete(FetchResponse.failed(message, false));
}
} catch (SocketTimeoutException e) {
String message = "Request timed out. Timeout values: [connect: " + httpClient.connectTimeoutMillis() + "ms, read: " + httpClient.readTimeoutMillis() + "ms, write: " + httpClient.writeTimeoutMillis() + "ms]";
logger.error(message, e);
future.complete(FetchResponse.failed(message, false));
} catch (Exception e) {
String message = "Exception while trying to fetch the config.json.";
logger.error(message, e);
future.complete(FetchResponse.failed(message, false));
}
}
});
return future;
}
@Override
public void close() throws IOException {
if (!this.isClosed.compareAndSet(false, true)) {
return;
}
if (this.httpClient != null) {
this.httpClient.dispatcher().executorService().shutdownNow();
this.httpClient.connectionPool().evictAll();
Cache cache = this.httpClient.cache();
if (cache != null)
cache.close();
}
}
Request getRequest(String etag) {
String url = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME + ".json";
Request.Builder builder = new Request.Builder()
.addHeader("X-ConfigCat-UserAgent", "ConfigCat-Java/" + this.mode + "-" + Constants.VERSION);
if (etag != null && !etag.isEmpty())
builder.addHeader("If-None-Match", etag);
return builder.url(url).build();
}
private Result<Config> deserializeConfig(String json) {
try {
return Result.success(Utils.gson.fromJson(json, Config.class));
} catch (Exception e) {
String message = "JSON parsing failed. " + e.getMessage();
this.logger.error(message);
return Result.error(message, null);
}
}
}