Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/com/configcat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public Segment[] getSegments() {
public Map<String, Setting> getEntries() {
// NOTE: Deserializing a JSON like '{ "f": null }' overwrites entries with null.
// However, we want to treat null as an empty map in that case too.
return entries != null ? entries : (entries = new HashMap<>());
if(entries == null) {
entries = new HashMap<>();
}
return entries;
}

boolean isEmpty() {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/configcat/ConfigCatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ private ConfigCatClient(String sdkKey, Options options) {
createHttpClient(options.httpOptions()),
this.logger,
sdkKey,
!options.isBaseURLCustom()
? options.dataGovernance == DataGovernance.GLOBAL
? BASE_URL_GLOBAL
: BASE_URL_EU
: options.baseUrl,
determineBaseUrl(options.baseUrl, options.dataGovernance, options.isBaseURLCustom()),
options.isBaseURLCustom(),
options.pollingMode.getPollingIdentifier());

Expand Down Expand Up @@ -288,8 +284,9 @@ public CompletableFuture<List<EvaluationDetails<Object>>> getAllValueDetailsAsyn
Map<String, Setting> settings = settingResult.settings();
List<EvaluationDetails<Object>> result = new ArrayList<>();

for (String key : settings.keySet()) {
Setting setting = settings.get(key);
for (Map.Entry<String, Setting> entry : settings.entrySet()) {
String key = entry.getKey();
Setting setting = entry.getValue();

EvaluationDetails<Object> evaluationDetails = this.evaluateObject(this.classBySettingType(setting.getType()), setting,
key, effectiveUser, settingResult.fetchTime(), settings);
Expand Down Expand Up @@ -725,6 +722,13 @@ private static boolean isValidKey(final String sdkKey, final boolean isCustomBas
return splitSDKKey.length == 3 && splitSDKKey[0].equals(Constants.SDK_KEY_PREFIX) && splitSDKKey[1].length() == Constants.SDK_KEY_SECTION_LENGTH && splitSDKKey[2].length() == Constants.SDK_KEY_SECTION_LENGTH;
}

private static String determineBaseUrl(String baseUrl, DataGovernance dataGovernance, boolean isBaseUrlCustom) {
if (isBaseUrlCustom) {
return baseUrl;
}
return dataGovernance == DataGovernance.GLOBAL ? BASE_URL_GLOBAL : BASE_URL_EU;
}

private EvaluationDetails<Object> evaluateObject(Class<?> classOfT, Setting setting, String key, User effectiveUser, Long fetchTime, Map<String, Setting> settings) {
EvaluationResult evaluationResult = this.rolloutEvaluator.evaluate(setting, key, effectiveUser, settings, new EvaluateLogger(this.clientLogLevel));
EvaluationDetails<Object> details = new EvaluationDetails<>(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/configcat/ConfigCatHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void addOnClientReady(Consumer<ClientCacheState> callback) {
* onReady event fires when the auto polling's maxInitWaitTimeInSeconds is reached.
*
* @param callback the method to call when the event fires.
*
* @deprecated Use {@link #addOnClientReady(Consumer)} instead.
*/
@Deprecated
public void addOnClientReady(Runnable callback) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/configcat/ConfigFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class ConfigFetcher implements Closeable {
private String url;

enum RedirectMode {
NoRedirect,
ShouldRedirect,
ForceRedirect
NO_REDIRECT,
SHOULD_REDIRECT,
FORCE_REDIRECT
}

ConfigFetcher(OkHttpClient httpClient,
Expand Down Expand Up @@ -75,16 +75,16 @@ private CompletableFuture<FetchResponse> executeFetchAsync(int executionCount, S
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()) {
if (this.urlIsCustom && redirect != RedirectMode.FORCE_REDIRECT.ordinal()) {
return CompletableFuture.completedFuture(fetchResponse);
}

this.url = newUrl;

if (redirect == RedirectMode.NoRedirect.ordinal()) { // no redirect
if (redirect == RedirectMode.NO_REDIRECT.ordinal()) { // no redirect
return CompletableFuture.completedFuture(fetchResponse);
} else { // redirect
if (redirect == RedirectMode.ShouldRedirect.ordinal()) {
if (redirect == RedirectMode.SHOULD_REDIRECT.ordinal()) {
this.logger.warn(3002, ConfigCatLogMessages.DATA_GOVERNANCE_IS_OUT_OF_SYNC_WARN);
}

Expand Down Expand Up @@ -272,14 +272,14 @@ public void close() throws IOException {
}

Request getRequest(String etag) {
String url = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME;
String requestUrl = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME;
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();
return builder.url(requestUrl).build();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/configcat/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public String getCacheString() {
}

public Entry withFetchTime(long fetchTime) {
String cacheString = getCacheString();
int fetchTimeIndex = cacheString.indexOf("\n");
String entryCacheString = getCacheString();
int fetchTimeIndex = entryCacheString.indexOf("\n");
if (fetchTimeIndex == -1) {
return this;
}
int eTagIndex = cacheString.indexOf("\n", fetchTimeIndex + 1);
int eTagIndex = entryCacheString.indexOf("\n", fetchTimeIndex + 1);
if (eTagIndex == -1) {
return this;
}
String configJson = cacheString.substring(eTagIndex + 1);
String configJson = entryCacheString.substring(eTagIndex + 1);
return new Entry(getConfig(), getETag(), configJson, fetchTime);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/EvaluationErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum EvaluationErrorCode implements ErrorCode {
*/
SETTING_KEY_MISSING(1001);

public final int code;
private final int code;

EvaluationErrorCode(int code) {
this.code = code;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/configcat/NullConfigCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ protected String read(String key) {

@Override
protected void write(String key, String value) {
// Null cache does not store anything.
}
}
1 change: 1 addition & 0 deletions src/main/java/com/configcat/OverrideDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public Map<String, Setting> getLocalConfiguration() {

@Override
public void close() throws IOException {
// No resources to close in this implementation.
}
}

4 changes: 4 additions & 0 deletions src/main/java/com/configcat/PollingModes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
public final class PollingModes {

private PollingModes() {
// Private constructor to prevent instantiation.
}

private static final int DEFAULT_AUTO_POLL_INTERVAL_IN_SECONDS = 60;
private static final int DEFAULT_MAX_INIT_WAIT_TIME_IN_SECONDS = 5;
private static final int DEFAULT_CACHE_REFRESH_INTERVAL_IN_SECONDS = 60;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/RefreshErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public enum RefreshErrorCode implements ErrorCode {
/** Client initialization could not complete within `maxInitWaitTimeSeconds`. */
CLIENT_INIT_TIMED_OUT(4200);

public final int code;
private final int code;

RefreshErrorCode(int code) {
this.code = code;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/configcat/UserAttributeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

final class UserAttributeConverter {

private UserAttributeConverter() {
// Private constructor to prevent instantiation.
}

public static String userAttributeToString(Object userAttribute) {
if (userAttribute == null) {
return null;
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/com/configcat/DataGovernanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DataGovernanceTest {
private static final String JsonTemplate = "{ p: { u: \"%s\", r: %d, s: \"test-salt\" }, f: {} }";
private static final String JSON_TEMPLATE = "{ p: { u: \"%s\", r: %d, s: \"test-salt\" }, f: {} }";
private final ConfigCatLogger logger = new ConfigCatLogger(LoggerFactory.getLogger(DataGovernanceTest.class));

@Test
Expand All @@ -21,7 +21,7 @@ public void shouldStayOnGivenUrl() throws IOException, ExecutionException, Inter
String url = server.url("/").toString();
ConfigFetcher fetcher = this.createFetcher(url, false);
// Arrange
String body = String.format(JsonTemplate, url, 0);
String body = String.format(JSON_TEMPLATE, url, 0);
server.enqueue(new MockResponse().setResponseCode(200).setBody(body));

// Act
Expand All @@ -42,7 +42,7 @@ public void shouldStayOnSameUrl() throws IOException, ExecutionException, Interr
String url = server.url("/").toString();
ConfigFetcher fetcher = this.createFetcher(url, false);
// Arrange
String body = String.format(JsonTemplate, url, 1);
String body = String.format(JSON_TEMPLATE, url, 1);
server.enqueue(new MockResponse().setResponseCode(200).setBody(body));

// Act
Expand All @@ -64,7 +64,7 @@ public void shouldStayOnSameUrlEvenWithForce() throws IOException, ExecutionExce
ConfigFetcher fetcher = this.createFetcher(url, false);

// Arrange
String body = String.format(JsonTemplate, url, 2);
String body = String.format(JSON_TEMPLATE, url, 2);
server.enqueue(new MockResponse().setResponseCode(200).setBody(body));

// Act
Expand All @@ -88,10 +88,10 @@ public void shouldRedirectToAnotherServer() throws IOException, ExecutionExcepti
ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false);

// Arrange
String firstBody = String.format(JsonTemplate, secondServerUrl, 1);
String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1);
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));

String secondBody = String.format(JsonTemplate, secondServerUrl, 0);
String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0);
secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody));

// Act
Expand All @@ -118,10 +118,10 @@ public void shouldRedirectToAnotherServerWhenForced() throws IOException, Execut
ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false);

// Arrange
String firstBody = String.format(JsonTemplate, secondServerUrl, 2);
String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 2);
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));

String secondBody = String.format(JsonTemplate, secondServerUrl, 0);
String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0);
secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody));

// Act
Expand All @@ -148,11 +148,11 @@ public void shouldBreakTheRedirectLoop() throws IOException, ExecutionException,
ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false);

// Arrange
String firstBody = String.format(JsonTemplate, secondServerUrl, 1);
String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1);
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));

String secondBody = String.format(JsonTemplate, firstServerUrl, 1);
String secondBody = String.format(JSON_TEMPLATE, firstServerUrl, 1);
secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody));

// Act
Expand All @@ -179,7 +179,7 @@ public void shouldRespectCustomUrlWhenNotForced() throws IOException, ExecutionE
ConfigFetcher fetcher = this.createFetcher(firstServerUrl, true);

// Arrange
String firstBody = String.format(JsonTemplate, secondServerUrl, 1);
String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1);
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));

Expand Down Expand Up @@ -216,10 +216,10 @@ public void shouldNotRespectCustomUrlWhenForced() throws IOException, ExecutionE
ConfigFetcher fetcher = this.createFetcher(firstServerUrl, true);

// Arrange
String firstBody = String.format(JsonTemplate, secondServerUrl, 2);
String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 2);
firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody));

String secondBody = String.format(JsonTemplate, secondServerUrl, 0);
String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0);
secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody));

// Act
Expand Down
Loading