Skip to content

Commit 9d5eaf0

Browse files
authored
Closes #342 - Added switcher.relay.restrict to allow or bypass Relay check when local (#343)
1 parent fb83d45 commit 9d5eaf0

26 files changed

+537
-495
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ https://github.com/switcherapi/switcher-api
2929
- Able to work local using a snapshot file pulled from your remote Switcher-API Domain.
3030
- Silent mode is a hybrid configuration that automatically enables contingent sub-processes in case of any connectivity issue.
3131
- Built-in test annotation for clear and easy implementation of automated testing.
32-
- Easy to setup. Switcher Context is responsible to manage all the configuration complexity between your application and API.
32+
- Easy to set up. Switcher Context is responsible to manage all the configuration complexity between your application and API.
3333

3434
# Usage
3535

@@ -70,6 +70,7 @@ switcher.domain -> Domain name
7070
#optional
7171
switcher.environment -> Environment name
7272
switcher.local -> true/false When local, it will only use a local snapshot
73+
switcher.relay.restrict -> true/false When true, it will check snapshot relay status
7374
switcher.snapshot.location -> Folder from where snapshots will be saved/read
7475
switcher.snapshot.auto -> true/false Automated lookup for snapshot when initializing the client
7576
switcher.snapshot.skipvalidation -> true/false Skip snapshotValidation() that can be used for UT executions
@@ -250,9 +251,7 @@ MyAppFeatures.scheduleSnapshotAutoUpdate("5s", new SnapshotCallback() {
250251
});
251252
```
252253

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

src/main/java/com/github/switcherapi/client/ContextBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ public ContextBuilder local(boolean local) {
170170
return this;
171171
}
172172

173+
/**
174+
* @param restrictRelay true/false When true, it will check snapshot relay status
175+
* @return ContextBuilder
176+
*/
177+
public ContextBuilder restrictRelay(boolean restrictRelay) {
178+
switcherProperties.setValue(ContextKey.RESTRICT_RELAY, restrictRelay);
179+
return this;
180+
}
181+
173182
/**
174183
* @param truststorePath Path to the truststore file
175184
* @return ContextBuilder

src/main/java/com/github/switcherapi/client/SwitcherPropertiesImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public SwitcherPropertiesImpl() {
2323
setValue(ContextKey.SNAPSHOT_AUTO_LOAD, false);
2424
setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, false);
2525
setValue(ContextKey.LOCAL_MODE, false);
26+
setValue(ContextKey.RESTRICT_RELAY, true);
2627
}
2728

2829
@Override
@@ -39,6 +40,7 @@ public void loadFromProperties(Properties prop) {
3940
setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, SwitcherUtils.resolveProperties(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL.getParam(), prop));
4041
setValue(ContextKey.SILENT_MODE, SwitcherUtils.resolveProperties(ContextKey.SILENT_MODE.getParam(), prop));
4142
setValue(ContextKey.LOCAL_MODE, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.LOCAL_MODE.getParam(), prop), false));
43+
setValue(ContextKey.RESTRICT_RELAY, getBoolDefault(SwitcherUtils.resolveProperties(ContextKey.RESTRICT_RELAY.getParam(), prop), true));
4244
setValue(ContextKey.REGEX_TIMEOUT, getIntDefault(SwitcherUtils.resolveProperties(ContextKey.REGEX_TIMEOUT.getParam(), prop), DEFAULT_REGEX_TIMEOUT));
4345
setValue(ContextKey.TRUSTSTORE_PATH, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));
4446
setValue(ContextKey.TRUSTSTORE_PASSWORD, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop));

src/main/java/com/github/switcherapi/client/model/ContextKey.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public enum ContextKey {
6969
*/
7070
LOCAL_MODE("switcher.local"),
7171

72+
/**
73+
* (boolean) Defines if client will trigger local snapshot relay verification (default is true)
74+
*/
75+
RESTRICT_RELAY("switcher.relay.restrict"),
76+
7277
/**
7378
* (Number) Defines the Timed Match regex time out.
7479
*/

src/main/java/com/github/switcherapi/client/model/EntryOperation.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.switcherapi.client.model;
22

33
public enum EntryOperation {
4-
54
EQUAL,
65
NOT_EQUAL,
76
EXIST,
@@ -12,5 +11,4 @@ public enum EntryOperation {
1211
HAS_ONE,
1312
HAS_ALL,
1413
INVALID
15-
1614
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.switcherapi.client.model;
2+
3+
/**
4+
* @author Roger Floriano (petruki)
5+
* @since 2025-06-13
6+
*/
7+
public enum RelayType {
8+
NOTIFICATION,
9+
WEBHOOK
10+
}

src/main/java/com/github/switcherapi/client/model/SwitcherBuilder.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.Objects;
910

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

2425
protected boolean bypassMetrics;
2526

27+
protected Boolean restrictRelay;
28+
2629
protected String defaultResult;
2730

2831
protected List<Entry> entry;
@@ -71,7 +74,18 @@ public SwitcherBuilder defaultResult(boolean defaultResult) {
7174
this.defaultResult = String.valueOf(defaultResult);
7275
return this;
7376
}
74-
77+
78+
/**
79+
* Allow local snapshots to ignore or require Relay verification.
80+
*
81+
* @param restrictRelay true to restrict Relay verification
82+
* @return {@link SwitcherBuilder}
83+
*/
84+
public SwitcherBuilder restrictRelay(boolean restrictRelay) {
85+
this.restrictRelay = restrictRelay;
86+
return this;
87+
}
88+
7589
/**
7690
* Add a validation to the entry stack
7791
*
@@ -171,6 +185,14 @@ public boolean isRemote() {
171185
return remote;
172186
}
173187

188+
public boolean isRelayRestricted() {
189+
return restrictRelay;
190+
}
191+
192+
public boolean isRestrictRelaySet() {
193+
return Objects.nonNull(restrictRelay);
194+
}
195+
174196
public String getDefaultResult() {
175197
return defaultResult;
176198
}

src/main/java/com/github/switcherapi/client/model/SwitcherRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public SwitcherResult submit() throws SwitcherException {
111111

112112
@Override
113113
public SwitcherResult executeCriteria() {
114+
if (!isRestrictRelaySet()) {
115+
this.restrictRelay(properties.getBoolean(ContextKey.RESTRICT_RELAY));
116+
}
117+
114118
return this.switcherExecutor.executeCriteria(this);
115119
}
116120

src/main/java/com/github/switcherapi/client/model/criteria/Config.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
package com.github.switcherapi.client.model.criteria;
22

33
import java.util.Arrays;
4+
import java.util.Objects;
45

56
/**
67
* @author Roger Floriano (petruki)
78
* @since 2019-12-24
89
*/
910
public class Config extends SwitcherElement {
1011

11-
private String key;
12+
private final String key;
1213

13-
private Strategy[] strategies;
14+
private final Strategy[] strategies;
1415

15-
private String[] components;
16+
private final String[] components;
1617

17-
public String getKey() {
18-
return key;
18+
private final Relay relay;
19+
20+
public Config(String key, String description, boolean activated, Strategy[] strategies, String[] components,
21+
Relay relay) {
22+
super(description, activated);
23+
this.key = key;
24+
this.strategies = strategies;
25+
this.components = components;
26+
this.relay = relay;
1927
}
2028

21-
public Strategy[] getStrategies() {
22-
return strategies;
29+
public boolean hasRelayEnabled() {
30+
return Objects.nonNull(relay) && relay.isActivated();
2331
}
2432

25-
public void setKey(String key) {
26-
this.key = key;
33+
public String getKey() {
34+
return key;
2735
}
2836

29-
public void setStrategies(Strategy[] strategies) {
30-
this.strategies = strategies;
37+
public Relay getRelay() {
38+
return relay;
3139
}
3240

33-
public String[] getComponents() {
34-
return components;
41+
public Strategy[] getStrategies() {
42+
return strategies;
3543
}
3644

37-
public void setComponents(String[] components) {
38-
this.components = components;
45+
public String[] getComponents() {
46+
return components;
3947
}
4048

4149
@Override

src/main/java/com/github/switcherapi/client/model/criteria/Domain.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,35 @@
88
*/
99
public class Domain extends SwitcherElement {
1010

11-
private String name;
11+
private final String name;
1212

13-
private long version;
13+
private final long version;
1414

15-
private Group[] group;
15+
private final Group[] group;
1616

17-
public Group[] getGroup() {
18-
return group;
17+
public Domain(String name, String description, boolean activated, long version, Group[] group) {
18+
super(description, activated);
19+
this.name = name;
20+
this.version = version;
21+
this.group = group;
1922
}
2023

21-
public void setGroup(Group[] group) {
22-
this.group = group;
24+
public Domain() {
25+
this(null, null, false, 0L, null);
2326
}
2427

25-
public String getName() {
26-
return name;
28+
public Group[] getGroup() {
29+
return group;
2730
}
2831

29-
public void setName(String name) {
30-
this.name = name;
32+
public String getName() {
33+
return name;
3134
}
3235

3336
public long getVersion() {
3437
return version;
3538
}
3639

37-
public void setVersion(long version) {
38-
this.version = version;
39-
}
40-
4140
@Override
4241
public String toString() {
4342
return String.format("Domain [name = %s, description = %s, activated = %s, version = %s, group = %s]", name,

0 commit comments

Comments
 (0)