-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitcherRemoteService.java
More file actions
106 lines (84 loc) · 3.57 KB
/
SwitcherRemoteService.java
File metadata and controls
106 lines (84 loc) · 3.57 KB
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
package com.switcherapi.client.service.remote;
import com.switcherapi.client.SwitcherExecutor;
import com.switcherapi.client.SwitcherExecutorImpl;
import com.switcherapi.client.exception.SwitcherRemoteException;
import com.switcherapi.client.exception.SwitchersValidationException;
import com.switcherapi.client.model.ContextKey;
import com.switcherapi.client.model.SwitcherRequest;
import com.switcherapi.client.model.SwitcherResult;
import com.switcherapi.client.remote.dto.CriteriaResponse;
import com.switcherapi.client.remote.dto.SwitchersCheck;
import com.switcherapi.client.service.SwitcherFactory;
import com.switcherapi.client.utils.Mapper;
import com.switcherapi.client.utils.SwitcherUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
/**
* @author Roger Floriano (petruki)
* @since 2019-12-24
*/
public class SwitcherRemoteService extends SwitcherExecutorImpl {
private static final Logger logger = LoggerFactory.getLogger(SwitcherRemoteService.class);
private final SwitcherExecutor switcherLocal;
private final ClientRemote clientRemote;
public SwitcherRemoteService(final ClientRemote clientRemote, final SwitcherExecutor switcherExecutor) {
super(switcherExecutor.getSwitcherProperties());
this.clientRemote = clientRemote;
this.switcherLocal = switcherExecutor;
}
@Override
public SwitcherResult executeCriteria(final SwitcherRequest switcherRequest) {
SwitcherUtils.debug(logger, "[Remote] request: {}", switcherRequest);
try {
final CriteriaResponse response = this.clientRemote.executeCriteria(Mapper.mapFrom(switcherRequest));
SwitcherUtils.debug(logger, "[Remote] response: {}", response);
return Mapper.mapFrom(response, switcherRequest);
} catch (final SwitcherRemoteException e) {
logger.error("Failed to execute criteria - Cause: {}", e.getMessage(), e.getCause());
return tryExecuteLocalCriteria(switcherRequest, e);
}
}
private SwitcherResult tryExecuteLocalCriteria(final SwitcherRequest switcher,
final SwitcherRemoteException e) {
if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.SILENT_MODE))) {
final SwitcherResult response = this.switcherLocal.executeCriteria(switcher);
SwitcherUtils.debug(logger, "[Silent] response: {}", response);
return response;
}
if (StringUtils.isNotBlank(switcher.getDefaultResult())) {
final SwitcherResult response = SwitcherFactory.buildFromDefault(switcher);
SwitcherUtils.debug(logger, "[Default] response: {}", response);
return response;
}
throw e;
}
@Override
public boolean checkSnapshotVersion() {
if (StringUtils.isNotBlank(switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION))
&& Objects.nonNull(this.switcherLocal.getDomain())) {
return super.checkSnapshotVersion(this.clientRemote, this.switcherLocal.getDomain());
}
super.initializeSnapshotFromAPI(this.clientRemote);
return Boolean.TRUE;
}
@Override
public void updateSnapshot() {
this.switcherLocal.setDomain(super.initializeSnapshotFromAPI(this.clientRemote));
}
@Override
public void checkSwitchers(final Set<String> switchers) {
SwitcherUtils.debug(logger, "switchers: {}", switchers);
final SwitchersCheck response = this.clientRemote.checkSwitchers(switchers);
if (response.getNotFound() != null && response.getNotFound().length > 0) {
throw new SwitchersValidationException(Arrays.toString(response.getNotFound()));
}
}
@Override
public long getSnapshotVersion() {
return switcherLocal.getSnapshotVersion();
}
}