Skip to content

Commit 4b805fa

Browse files
authored
Merge pull request #17 from petruki/master
fix: Fixed code smells - updated commons-net
2 parents 7039245 + 1820142 commit 4b805fa

File tree

6 files changed

+38
-47
lines changed

6 files changed

+38
-47
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
<!-- utils -->
5454
<commons-lang3.version>3.11</commons-lang3.version>
55-
<commons-net.version>3.6</commons-net.version>
55+
<commons-net.version>3.7</commons-net.version>
5656

5757
<!-- test -->
5858
<log4j.version>2.13.3</log4j.version>

readme.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ switcher.isItOn(); // Now, it's going to return the result retrieved from the AP
153153
- Fixed error when using only access to online API
154154
- Improved validation when verifying whether API is accessible
155155
- Added validations when preparing the Switcher Context
156+
- Updated dependency commons-net.version from 3.6 to 3.7
156157
- 1.0.7: Added Regex Validation
157158
- 1.0.6: Updated depencencies & new features
158159
- Updated dependency jersey-hk2 from 2.28 to 2.31
159-
- Updated dependency commons-net from 3.3 to 3.6.
160-
- Updated dependency commons-lang3 from 3.8.1 to 3.10.
161-
- Updated dependency gson from 2.8.5 to 2.8.6.
162-
- Added execution log to Switcher.
163-
- Added bypass metrics and show detailed criteria evaluation options to Switcher objects.
160+
- Updated dependency commons-net from 3.3 to 3.6
161+
- Updated dependency commons-lang3 from 3.8.1 to 3.10
162+
- Updated dependency gson from 2.8.5 to 2.8.6
163+
- Added execution log to Switcher
164+
- Added bypass metrics and show detailed criteria evaluation options to Switcher objects
164165
- 1.0.5: Security patch - Jersey has been updated - 2.28 to 2.31
165166
- 1.0.4: Added Numeric Validation
166167
- 1.0.3: Security patch - Log4J has been updated - 2.13.1 to 2.13.3

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,15 @@ public static void validateContext(final Map<String, Object> properties, boolean
161161
}
162162

163163
if (properties.containsKey(SwitcherContextParam.SNAPSHOT_AUTO_LOAD) &&
164-
Boolean.parseBoolean(properties.get(SwitcherContextParam.SNAPSHOT_AUTO_LOAD).toString())) {
165-
if (!properties.containsKey(SwitcherContextParam.SNAPSHOT_LOCATION)) {
164+
Boolean.parseBoolean(properties.get(SwitcherContextParam.SNAPSHOT_AUTO_LOAD).toString()) &&
165+
!properties.containsKey(SwitcherContextParam.SNAPSHOT_LOCATION)) {
166166
throw new SwitcherFactoryContextException("SwitcherContextParam.SNAPSHOT_LOCATION not found");
167-
}
168167
}
169168

170169
if (properties.containsKey(SwitcherContextParam.SILENT_MODE) &&
171-
Boolean.parseBoolean(properties.get(SwitcherContextParam.SILENT_MODE).toString())) {
172-
if (!properties.containsKey(SwitcherContextParam.RETRY_AFTER)) {
170+
Boolean.parseBoolean(properties.get(SwitcherContextParam.SILENT_MODE).toString()) &&
171+
!properties.containsKey(SwitcherContextParam.RETRY_AFTER)) {
173172
throw new SwitcherFactoryContextException("SwitcherContextParam.RETRY_AFTER not found");
174-
}
175173
}
176174
}
177175

src/main/java/com/github/switcherapi/client/facade/ClientServiceFacade.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,23 @@ public static ClientServiceFacade getInstance() {
4747

4848
public CriteriaResponse executeCriteria(final Map<String, Object> properties, final Switcher switcher)
4949
throws SwitcherException {
50+
if (!this.isTokenValid(properties)) {
51+
this.auth(properties);
52+
}
53+
54+
final Response response = this.clientService.executeCriteriaService(properties, switcher);
5055

51-
try {
52-
if (!this.isTokenValid(properties)) {
53-
this.auth(properties);
54-
}
55-
56-
final Response response = this.clientService.executeCriteriaService(properties, switcher);
57-
58-
if (response.getStatus() == 401) {
59-
throw new SwitcherKeyNotAvailableForComponentException(
60-
properties.get(SwitcherContextParam.COMPONENT).toString(), switcher.getSwitcherKey());
61-
} else if (response.getStatus() != 200) {
62-
throw new SwitcherKeyNotFoundException(switcher.getSwitcherKey());
63-
}
64-
65-
final CriteriaResponse criteriaReponse = response.readEntity(CriteriaResponse.class);
66-
criteriaReponse.setSwitcherKey(switcher.getSwitcherKey());
67-
response.close();
68-
return criteriaReponse;
69-
} catch (final SwitcherException e) {
70-
throw e;
56+
if (response.getStatus() == 401) {
57+
throw new SwitcherKeyNotAvailableForComponentException(
58+
properties.get(SwitcherContextParam.COMPONENT).toString(), switcher.getSwitcherKey());
59+
} else if (response.getStatus() != 200) {
60+
throw new SwitcherKeyNotFoundException(switcher.getSwitcherKey());
7161
}
7262

63+
final CriteriaResponse criteriaReponse = response.readEntity(CriteriaResponse.class);
64+
criteriaReponse.setSwitcherKey(switcher.getSwitcherKey());
65+
response.close();
66+
return criteriaReponse;
7367
}
7468

7569
public Snapshot resolveSnapshot(final Map<String, Object> properties)
@@ -88,8 +82,7 @@ public Snapshot resolveSnapshot(final Map<String, Object> properties)
8882
} catch (final SwitcherException e) {
8983
throw e;
9084
} catch (final Exception e) {
91-
throw new SwitcherAPIConnectionException(properties.containsKey(SwitcherContextParam.URL) ?
92-
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY, e);
85+
throw new SwitcherAPIConnectionException(getAPIConnectionException(properties), e);
9386
}
9487

9588
}
@@ -111,8 +104,7 @@ public boolean checkSnapshotVersion(final Map<String, Object> properties, final
111104
} catch (final SwitcherException e) {
112105
throw e;
113106
} catch (final Exception e) {
114-
throw new SwitcherAPIConnectionException(properties.containsKey(SwitcherContextParam.URL) ?
115-
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY, e);
107+
throw new SwitcherAPIConnectionException(getAPIConnectionException(properties), e);
116108
}
117109

118110
}
@@ -132,8 +124,7 @@ private void auth(final Map<String, Object> properties) throws SwitcherException
132124
throw e;
133125
} catch (final Exception e) {
134126
this.setSilentModeExpiration(properties);
135-
throw new SwitcherAPIConnectionException(properties.containsKey(SwitcherContextParam.URL) ?
136-
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY, e);
127+
throw new SwitcherAPIConnectionException(getAPIConnectionException(properties), e);
137128
}
138129
}
139130

@@ -143,16 +134,12 @@ private boolean isTokenValid(final Map<String, Object> properties)
143134
if (properties.containsKey(ClientService.AUTH_RESPONSE)) {
144135
final AuthResponse authResponse = (AuthResponse) properties.get(ClientService.AUTH_RESPONSE);
145136

146-
if (authResponse.getToken().equals(SwitcherContextParam.SILENT_MODE)) {
147-
if (!authResponse.isExpired()) {
148-
throw new SwitcherAPIConnectionException(properties.containsKey(SwitcherContextParam.URL) ?
149-
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY);
150-
}
137+
if (authResponse.getToken().equals(SwitcherContextParam.SILENT_MODE) && !authResponse.isExpired()) {
138+
throw new SwitcherAPIConnectionException(getAPIConnectionException(properties));
151139
} else {
152140
if (!this.clientService.isAlive(properties)) {
153141
this.setSilentModeExpiration(properties);
154-
throw new SwitcherAPIConnectionException(properties.containsKey(SwitcherContextParam.URL) ?
155-
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY);
142+
throw new SwitcherAPIConnectionException(getAPIConnectionException(properties));
156143
}
157144

158145
return !authResponse.isExpired();
@@ -161,6 +148,11 @@ private boolean isTokenValid(final Map<String, Object> properties)
161148

162149
return false;
163150
}
151+
152+
private String getAPIConnectionException(final Map<String, Object> properties) {
153+
return properties.containsKey(SwitcherContextParam.URL) ?
154+
(String) properties.get(SwitcherContextParam.URL) : StringUtils.EMPTY;
155+
}
164156

165157
private void setSilentModeExpiration(final Map<String, Object> properties)
166158
throws SwitcherInvalidDateTimeArgumentException {

src/test/java/com/github/switcherapi/client/SwitcherOnlineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void shouldInvokeClientCheckWithNoErrors() throws Exception {
309309
clientService.setClient(clientMock);
310310

311311
//test
312-
clientService.isAlive(properties);
312+
assertTrue(clientService.isAlive(properties));
313313
}
314314

315315

src/test/resources/generated_watcher_default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
}
342342
],
343343
"description": "Fixture #1",
344-
"activated": true
344+
"activated": false
345345
}
346346
}
347347
}

0 commit comments

Comments
 (0)