Skip to content
Merged
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Define a feature class that extends SwitcherContext. This implementation will ce

The Client SDK configuration must be defined in a properties file that contains all parameters for your application to start communicating with the API.

### Inside of the resources folder, create a file named: switcherapi.properties.
### Inside the resources' folder, create a file named: switcherapi.properties.

Configure the parameters according to the definition below.
You can also use environment variables using the standard notation ${VALUE:DEFAULT_VALUE}
Expand All @@ -69,13 +69,12 @@ switcher.domain -> Domain name

#optional
switcher.environment -> Environment name
switcher.offline -> true/false When offline, it will only use a local snapshot file
switcher.offline -> true/false When offline, it will only use a local snapshot
switcher.snapshot.location -> Folder from where snapshots will be saved/read
switcher.snapshot.auto -> true/false Automated lookup for snapshot when loading the application
switcher.snapshot.auto -> true/false Automated lookup for snapshot when initializing the client
switcher.snapshot.skipvalidation -> true/false Skip snapshotValidation() that can be used for UT executions
switcher.snapshot.updateinterval -> Enable the Snapshot Auto Update given an interval of time - e.g. 1s (s: seconds, m: minutes)
switcher.silent -> true/false Contingency in case of some problem with connectivity with the API
switcher.retry -> Time given to the module to re-establish connectivity with the API - e.g. 5s (s: seconds - m: minutes - h: hours)
switcher.silent -> Enable contigency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours)
switcher.truststore.path -> Path to the truststore file
switcher.truststore.password -> Truststore password
switcher.timeout -> Time in ms given to the API to respond - 3000 default value
Expand Down
96 changes: 80 additions & 16 deletions src/main/java/com/github/switcherapi/client/ContextBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.switcherapi.client;

import org.apache.commons.lang3.StringUtils;

public class ContextBuilder {

private static ContextBuilder context;
Expand Down Expand Up @@ -34,42 +36,74 @@ void preBuild(SwitcherProperties properties) {
SwitcherProperties build() {
return this.properties;
}


/**
* @param contextLocation Feature class that extends SwitcherContext
* @return ContextBuilder
*/
public ContextBuilder contextLocation(String contextLocation) {
properties.setContextLocation(contextLocation);
return this;
}


/**
* @param url Switcher API URL
* @return ContextBuilder
*/
public ContextBuilder url(String url) {
properties.setUrl(url);
return this;
}


/**
* @param apiKey Switcher API Key
* @return ContextBuilder
*/
public ContextBuilder apiKey(String apiKey) {
properties.setApiKey(apiKey);
return this;
}


/**
* @param domain Switcher Domain
* @return ContextBuilder
*/
public ContextBuilder domain(String domain) {
properties.setDomain(domain);
return this;
}


/**
* @param component Switcher Component
* @return ContextBuilder
*/
public ContextBuilder component(String component) {
properties.setComponent(component);
return this;
}


/**
* @param environment Switcher Environment
* @return ContextBuilder
*/
public ContextBuilder environment(String environment) {
properties.setEnvironment(environment);
return this;
}


/**
* @param snapshotLocation Folder path from where snapshots will be saved/read
* @return ContextBuilder
*/
public ContextBuilder snapshotLocation(String snapshotLocation) {
properties.setSnapshotLocation(snapshotLocation);
return this;
}

/**
* @param snapshotAutoUpdateInterval Enable the Snapshot Auto Update given an interval of time - e.g. 1s (s: seconds, m: minutes)
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
properties.setSnapshotAutoUpdateInterval(snapshotAutoUpdateInterval);

Expand All @@ -79,50 +113,80 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv
return this;
}

/**
* Java 8 only
*
* @param regexTimeout Time in ms given to Timed Match Worker used for offline Regex (ReDoS safety mechanism) - 3000 default value
* @return ContextBuilder
*/
public ContextBuilder regexTimeout(String regexTimeout) {
properties.setRegexTimeout(regexTimeout);
return this;
}

public ContextBuilder retryAfter(String retryAfter) {
properties.setRetryAfter(retryAfter);
return this;
}

/**
* @param snapshotAutoLoad true/false Automated lookup for snapshot when initializing the client
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
properties.setSnapshotAutoLoad(snapshotAutoLoad);
return this;
}


/**
* @param snapshotSkipValidation true/false Skip snapshotValidation() that can be used for UT executions
* @return ContextBuilder
*/
public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
properties.setSnapshotSkipValidation(snapshotSkipValidation);
return this;
}

public ContextBuilder silentMode(boolean silentMode) {
properties.setSilentMode(silentMode);
/**
* @param retryAfter Enable contigency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours)
* @return ContextBuilder
*/
public ContextBuilder silentMode(String retryAfter) {
properties.setSilentMode(retryAfter);

if (silentMode)
if (StringUtils.isNotBlank(retryAfter)) {
properties.setSnapshotAutoLoad(true);
}

return this;
}

/**
* @param offlineMode true/false When offline, it will only use a local snapshot
* @return ContextBuilder
*/
public ContextBuilder offlineMode(boolean offlineMode) {
properties.setOfflineMode(offlineMode);
return this;
}

/**
* @param truststorePath Path to the truststore file
* @return ContextBuilder
*/
public ContextBuilder truststorePath(String truststorePath) {
properties.setTruststorePath(truststorePath);
return this;
}

/**
* @param truststorePassword Password to the truststore file
* @return ContextBuilder
*/
public ContextBuilder truststorePassword(String truststorePassword) {
properties.setTruststorePassword(truststorePassword);
return this;
}

/**
* @param timeoutMs Time in ms given to the API to respond - 3000 default value
* @return ContextBuilder
*/
public ContextBuilder timeoutMs(String timeoutMs) {
properties.setTimeoutMs(timeoutMs);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
class SwitcherContextValidator {

public static final String ERR_FORMAT = "Invalid parameter format for [%s]. Expected %s.";
public static final String ERR_RETRY = "Retry not defined [add: switcher.retry]";
public static final String ERR_URL = "URL not defined [add: switcher.url]";
public static final String ERR_API = "API Key not defined [add: switcher.apikey]";
public static final String ERR_DOMAIN = "Domain not defined [add: switcher.domain]";
Expand Down Expand Up @@ -46,10 +45,6 @@ public static void validate(final SwitcherProperties prop) {
* @param prop Configured properties
*/
public static void validateOptionals(final SwitcherProperties prop) {
if (prop.isSilentMode() && StringUtils.isBlank(prop.getRetryAfter())) {
throw new SwitcherContextException(ERR_RETRY);
}

try {
Integer.parseInt(prop.getRegexTimeout());
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ class SwitcherProperties {

private String snapshotAutoUpdateInterval;

private String retryAfter;

private String regexTimeout;

private String silentMode;

private boolean snapshotAutoLoad;

private boolean snapshotSkipValidation;

private boolean silentMode;

private boolean offlineMode;

private String truststorePath;
Expand All @@ -74,9 +72,8 @@ public void loadFromProperties(Properties prop) {
setSnapshotSkipValidation(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_SKIP_VALIDATION.getParam(), prop)));
setSnapshotAutoLoad(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_LOAD.getParam(), prop)));
setSnapshotAutoUpdateInterval(SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop));
setSilentMode(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop)));
setSilentMode(SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop));
setOfflineMode(Boolean.parseBoolean(SwitcherUtils.resolveProperties(ContextKey.OFFLINE_MODE.getParam(), prop)));
setRetryAfter(SwitcherUtils.resolveProperties(ContextKey.RETRY_AFTER.getParam(), prop));
setRegexTimeout(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop));
setTruststorePath(SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));
setTruststorePassword(SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop));
Expand Down Expand Up @@ -160,14 +157,6 @@ public void setSnapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
this.snapshotAutoUpdateInterval = snapshotAutoUpdateInterval;
}

public String getRetryAfter() {
return retryAfter;
}

public void setRetryAfter(String retryAfter) {
this.retryAfter = retryAfter;
}

public String getRegexTimeout() {
return regexTimeout;
}
Expand Down Expand Up @@ -196,11 +185,11 @@ public void setSnapshotSkipValidation(boolean snapshotSkipValidation) {
this.snapshotSkipValidation = snapshotSkipValidation;
}

public boolean isSilentMode() {
public String getSilentMode() {
return silentMode;
}

public void setSilentMode(boolean silentMode) {
public void setSilentMode(String silentMode) {
this.silentMode = silentMode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,10 @@ public enum ContextKey {
SNAPSHOT_AUTO_UPDATE_INTERVAL("switcher.snapshot.updateinterval", "snapshotAutoUpdateInterval"),

/**
* (boolean) Activate silent mode when the Switcher API becomes offline.
* (String) Defines if client will work in silent mode by specifying the time interval to retry
*/
SILENT_MODE("switcher.silent", "silentMode"),

/**
* (String) Time given to the library retry reaching the online Switcher API after using the silent mode.
*/
RETRY_AFTER("switcher.retry", "retryAfter"),

/**
* (boolean) Defines if client will work offline.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.switcherapi.client.remote.ClientWS;
import com.github.switcherapi.client.remote.ClientWSImpl;
import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Optional;
Expand Down Expand Up @@ -130,8 +131,8 @@ private TokenStatus isTokenValid() throws SwitcherRemoteException,
}

private void setSilentModeExpiration() throws SwitcherInvalidDateTimeArgumentException {
if (SwitcherContextBase.contextBol(ContextKey.SILENT_MODE)) {
final String addValue = SwitcherContextBase.contextStr(ContextKey.RETRY_AFTER);
if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) {
final String addValue = SwitcherContextBase.contextStr(ContextKey.SILENT_MODE);
final AuthResponse response = new AuthResponse();

response.setToken(ContextKey.SILENT_MODE.getParam());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CriteriaResponse executeCriteria(final Switcher switcher) {

private CriteriaResponse executeSilentCriteria(final Switcher switcher,
final SwitcherRemoteException e) {
if (SwitcherContextBase.contextBol(ContextKey.SILENT_MODE)) {
if (StringUtils.isNotBlank(SwitcherContextBase.contextStr(ContextKey.SILENT_MODE))) {
CriteriaResponse response = this.switcherOffline.executeCriteria(switcher);
if (logger.isDebugEnabled()) {
logger.debug(String.format("[Silent] response: %s", response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ void resetSwitcherContextState() {
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.silentMode(false)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null)
.retryAfter(null));
.snapshotAutoUpdateInterval(null));

Switchers.initializeClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ void resetSwitcherContextState() {
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.silentMode(false)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null)
.retryAfter(null));
.snapshotAutoUpdateInterval(null));

Switchers.initializeClient();
}
Expand Down Expand Up @@ -273,8 +272,7 @@ void shouldReturnTrue_silentMode() throws InterruptedException {
Switchers.configure(ContextBuilder.builder()
.snapshotLocation(SNAPSHOTS_LOCAL)
.environment("fixture1")
.silentMode(true)
.retryAfter("5s"));
.silentMode("5s"));

Switchers.initializeClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ void shouldThrowError_noComponent() {
CONTEXT_ERROR, SwitcherContextValidator.ERR_COMPONENT), ex.getMessage());
}

@Test
void shouldThrowErrorWhenSilentMode_noRetryTimer() {
Switchers.configure(ContextBuilder.builder()
.silentMode(true)
.retryAfter(null));

Exception ex = assertThrows(SwitcherContextException.class,
Switchers::initializeClient);

assertEquals(String.format(
CONTEXT_ERROR, SwitcherContextValidator.ERR_RETRY), ex.getMessage());
}

@Test
void shouldThrowError_invalidSwitcher() {
Exception ex = assertThrows(SwitcherKeyNotFoundException.class, () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ void resetSwitcherContextState() {
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.silentMode(false)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null)
.retryAfter(null)
.timeoutMs(null));

Switchers.initializeClient();
Expand Down
Loading