Skip to content

Closes #342 - Added switcher.relay.restrict to allow or bypass Relay check when local #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2025
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ https://github.com/switcherapi/switcher-api
- Able to work local using a snapshot file pulled from your remote Switcher-API Domain.
- Silent mode is a hybrid configuration that automatically enables contingent sub-processes in case of any connectivity issue.
- Built-in test annotation for clear and easy implementation of automated testing.
- Easy to setup. Switcher Context is responsible to manage all the configuration complexity between your application and API.
- Easy to set up. Switcher Context is responsible to manage all the configuration complexity between your application and API.

# Usage

Expand Down Expand Up @@ -70,6 +70,7 @@ switcher.domain -> Domain name
#optional
switcher.environment -> Environment name
switcher.local -> true/false When local, it will only use a local snapshot
switcher.relay.restrict -> true/false When true, it will check snapshot relay status
switcher.snapshot.location -> Folder from where snapshots will be saved/read
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
Expand Down Expand Up @@ -250,9 +251,7 @@ MyAppFeatures.scheduleSnapshotAutoUpdate("5s", new SnapshotCallback() {
});
```



## Real-time snapshot updater
## Real-time snapshot reload
Let the Switcher Client manage your application local snapshot.<br>
These features allow you to configure the SDK to automatically update the snapshot in the background.

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<jersey-client.version>2.47</jersey-client.version>
<jersey-hk2.version>2.47</jersey-hk2.version>
<jersey-media-json-jackson.version>2.47</jersey-media-json-jackson.version>
<javax.activation-api.version>1.2.0</javax.activation-api.version>
<gson.version>2.13.1</gson.version>

<!-- utils -->
Expand Down Expand Up @@ -109,6 +110,11 @@
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey-media-json-jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>${javax.activation-api.version}</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ public ContextBuilder local(boolean local) {
return this;
}

/**
* @param restrictRelay true/false When true, it will check snapshot relay status
* @return ContextBuilder
*/
public ContextBuilder restrictRelay(boolean restrictRelay) {
switcherProperties.setValue(ContextKey.RESTRICT_RELAY, restrictRelay);
return this;
}

/**
* @param truststorePath Path to the truststore file
* @return ContextBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public SwitcherPropertiesImpl() {
setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false);
setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false);
setValue(ContextKey.LOCAL_MODE, false);
setValue(ContextKey.RESTRICT_RELAY, true);
}

@Override
Expand All @@ -39,6 +40,7 @@ public void loadFromProperties(Properties prop) {
setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop));
setValue(ContextKey.SILENT_MODE, SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop));
setValue(ContextKey.LOCAL_MODE, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false));
setValue(ContextKey.RESTRICT_RELAY, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.RESTRICT_RELAY.getParam(), prop), true));
setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT));
setValue(ContextKey.TRUSTSTORE_PATH, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));
setValue(ContextKey.TRUSTSTORE_PASSWORD, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public enum ContextKey {
*/
LOCAL_MODE("switcher.local"),

/**
* (boolean) Defines if client will trigger local snapshot relay verification (default is true)
*/
RESTRICT_RELAY("switcher.relay.restrict"),

/**
* (Number) Defines the Timed Match regex time out.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.switcherapi.client.model;

public enum EntryOperation {

EQUAL,
NOT_EQUAL,
EXIST,
Expand All @@ -12,5 +11,4 @@ public enum EntryOperation {
HAS_ONE,
HAS_ALL,
INVALID

}
10 changes: 10 additions & 0 deletions src/main/java/com/github/switcherapi/client/model/RelayType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.switcherapi.client.model;

/**
* @author Roger Floriano (petruki)
* @since 2025-06-13
*/
public enum RelayType {
NOTIFICATION,
WEBHOOK
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Builder class that simplifies how input are programmatically wrapped inside the Switcher.
Expand All @@ -23,6 +24,8 @@ public abstract class SwitcherBuilder implements Switcher {

protected boolean bypassMetrics;

protected Boolean restrictRelay;

protected String defaultResult;

protected List<Entry> entry;
Expand Down Expand Up @@ -71,7 +74,18 @@ public SwitcherBuilder defaultResult(boolean defaultResult) {
this.defaultResult = String.valueOf(defaultResult);
return this;
}


/**
* Allow local snapshots to ignore or require Relay verification.
*
* @param restrictRelay true to restrict Relay verification
* @return {@link SwitcherBuilder}
*/
public SwitcherBuilder restrictRelay(boolean restrictRelay) {
this.restrictRelay = restrictRelay;
return this;
}

/**
* Add a validation to the entry stack
*
Expand Down Expand Up @@ -171,6 +185,14 @@ public boolean isRemote() {
return remote;
}

public boolean isRelayRestricted() {
return restrictRelay;
}

public boolean isRestrictRelaySet() {
return Objects.nonNull(restrictRelay);
}

public String getDefaultResult() {
return defaultResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public SwitcherResult submit() throws SwitcherException {

@Override
public SwitcherResult executeCriteria() {
if (!isRestrictRelaySet()) {
this.restrictRelay(properties.getBoolean(ContextKey.RESTRICT_RELAY));
}

return this.switcherExecutor.executeCriteria(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
package com.github.switcherapi.client.model.criteria;

import java.util.Arrays;
import java.util.Objects;

/**
* @author Roger Floriano (petruki)
* @since 2019-12-24
*/
public class Config extends SwitcherElement {

private String key;
private final String key;

private Strategy[] strategies;
private final Strategy[] strategies;

private String[] components;
private final String[] components;

public String getKey() {
return key;
private final Relay relay;

public Config(String key, String description, boolean activated, Strategy[] strategies, String[] components,
Relay relay) {
super(description, activated);
this.key = key;
this.strategies = strategies;
this.components = components;
this.relay = relay;
}

public Strategy[] getStrategies() {
return strategies;
public Config() {
this(null, null, false, null, null, null);
}

public void setKey(String key) {
this.key = key;
public boolean hasRelayEnabled() {
return Objects.nonNull(relay) && relay.isActivated();
}

public void setStrategies(Strategy[] strategies) {
this.strategies = strategies;
public String getKey() {
return key;
}

public String[] getComponents() {
return components;
public Relay getRelay() {
return relay;
}

public void setComponents(String[] components) {
this.components = components;
public Strategy[] getStrategies() {
return strategies;
}

public String[] getComponents() {
return components;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@
*/
public class Domain extends SwitcherElement {

private String name;
private final String name;

private long version;
private final long version;

private Group[] group;
private final Group[] group;

public Group[] getGroup() {
return group;
public Domain(String name, String description, boolean activated, long version, Group[] group) {
super(description, activated);
this.name = name;
this.version = version;
this.group = group;
}

public void setGroup(Group[] group) {
this.group = group;
public Domain() {
this(null, null, false, 0L, null);
}

public String getName() {
return name;
public Group[] getGroup() {
return group;
}

public void setName(String name) {
this.name = name;
public String getName() {
return name;
}

public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}

@Override
public String toString() {
return String.format("Domain [name = %s, description = %s, activated = %s, version = %s, group = %s]", name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
*/
public class Group extends SwitcherElement {

private String name;
private final String name;

private Config[] config;
private final Config[] config;

public Config[] getConfig() {
return config;
public Group(String name, String description, boolean activated, Config[] config) {
super(description, activated);
this.name = name;
this.config = config;
}

public void setConfig(Config[] config) {
this.config = config;
public Group() {
this(null, null, false, null);
}

public String getName() {
return name;
public Config[] getConfig() {
return config;
}

public void setName(String name) {
this.name = name;
public String getName() {
return name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.switcherapi.client.model.criteria;

/**
* @author Roger Floriano (petruki)
* @since 2025-06-13
*/
public class Relay {

private final String type;

private final boolean activated;

public Relay(String type, boolean activated) {
this.type = type;
this.activated = activated;
}

public Relay() {
this(null, false);
}

public boolean isActivated() {
return activated;
}

public String getType() {
return type;
}

@Override
public String toString() {
return String.format("Relay [type = %s, activated = %s]", type, activated);
}

}
Loading