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
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.github.switcherapi</groupId>
<artifactId>switcher-client</artifactId>
<packaging>jar</packaging>
<version>2.0.1</version>
<version>2.0.2-SNAPSHOT</version>

<name>Switcher Client</name>
<description>Switcher Client SDK for working with Switcher API</description>
Expand Down Expand Up @@ -51,9 +51,9 @@
<maven.compiler.target>${java.version}</maven.compiler.target>

<!-- rest/json libs -->
<jersey-client.version>3.1.1</jersey-client.version>
<jersey-hk2.version>3.1.1</jersey-hk2.version>
<jersey-media-json-jackson.version>3.1.1</jersey-media-json-jackson.version>
<jersey-client.version>3.1.2</jersey-client.version>
<jersey-hk2.version>3.1.2</jersey-hk2.version>
<jersey-media-json-jackson.version>3.1.2</jersey-media-json-jackson.version>
<gson.version>2.10.1</gson.version>

<!-- utils -->
Expand Down Expand Up @@ -170,19 +170,19 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.2</version>
<version>2.15.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
<version>2.15.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jakarta-xmlbind-annotations</artifactId>
<version>2.14.2</version>
<version>2.15.1</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.github.switcherapi.client.exception.SwitcherAPIConnectionException;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.github.switcherapi.client.exception.SwitcherSnapshotWriteException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
Expand Down Expand Up @@ -99,7 +99,7 @@ protected Domain initializeSnapshotFromAPI() {
environment);

return snapshot.getDomain();
} catch (SwitcherAPIConnectionException | SwitcherSnapshotWriteException e) {
} catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) {
logger.error(e);
throw e;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.switcherapi.client.exception;

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

public SwitcherRemoteException(final String url, final Exception e) {
super(String.format("It was not possible to reach the Switcher-API on this endpoint: %s", url), e);
}

public SwitcherRemoteException(final String url, int status) {
super(String.format("It was not possible to reach the Switcher-API on this endpoint: %s - status: %s", url, status), null);
}

public SwitcherRemoteException(final String url) {
super(String.format("It was not possible to reach the Switcher-API on this endpoint: %s", url), null);
}

}

This file was deleted.

This file was deleted.

107 changes: 50 additions & 57 deletions src/main/java/com/github/switcherapi/client/remote/ClientWSImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;
import java.util.Set;

import com.github.switcherapi.client.exception.*;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
Expand All @@ -13,10 +14,6 @@
import org.apache.logging.log4j.Logger;

import com.github.switcherapi.client.SwitcherContextBase;
import com.github.switcherapi.client.exception.SwitcherException;
import com.github.switcherapi.client.exception.SwitcherKeyNotAvailableForComponentException;
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
import com.github.switcherapi.client.exception.SwitcherSnapshoException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
import com.github.switcherapi.client.model.criteria.Snapshot;
Expand Down Expand Up @@ -64,19 +61,16 @@ public CriteriaResponse executeCriteriaService(final Switcher switcher, final St
.header(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token))
.post(Entity.json(switcher.getInputRequest()));

if (response.getStatus() == 401) {
throw new SwitcherKeyNotAvailableForComponentException(
SwitcherContextBase.contextStr(ContextKey.COMPONENT), switcher.getSwitcherKey());
} else if (response.getStatus() != 200) {
throw new SwitcherKeyNotFoundException(switcher.getSwitcherKey());
if (response.getStatus() == 200) {
final CriteriaResponse criteriaResponse = response.readEntity(CriteriaResponse.class);
criteriaResponse.setSwitcherKey(switcher.getSwitcherKey());
criteriaResponse.setEntry(switcher.getEntry());
response.close();

return criteriaResponse;
}

final CriteriaResponse criteriaResponse = response.readEntity(CriteriaResponse.class);
criteriaResponse.setSwitcherKey(switcher.getSwitcherKey());
criteriaResponse.setEntry(switcher.getEntry());
response.close();

return criteriaResponse;

throw new SwitcherRemoteException(url, response.getStatus());
}

@Override
Expand All @@ -86,62 +80,62 @@ public Optional<AuthResponse> auth() {
authRequest.setComponent(SwitcherContextBase.contextStr(ContextKey.COMPONENT));
authRequest.setEnvironment(SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT));

final WebTarget myResource = client.target(String.format(AUTH_URL,
SwitcherContextBase.contextStr(ContextKey.URL)));
final String url = SwitcherContextBase.contextStr(ContextKey.URL);
final WebTarget myResource = client.target(String.format(AUTH_URL, url));

final Response response = myResource.request(MediaType.APPLICATION_JSON)
.header(HEADER_APIKEY, SwitcherContextBase.contextStr(ContextKey.APIKEY))
.post(Entity.json(authRequest));

if (response.getStatus() == 401) {
throw new SwitcherException("Unauthorized API access", null);

if (response.getStatus() == 200) {
Optional<AuthResponse> authResponse = Optional.of(response.readEntity(AuthResponse.class));
response.close();

return authResponse;
}

Optional<AuthResponse> authResponse = Optional.of(response.readEntity(AuthResponse.class));
response.close();

return authResponse;

throw new SwitcherRemoteException(url, response.getStatus());
}

@Override
public Snapshot resolveSnapshot(final String token) {
final WebTarget myResource = client.target(String.format(SNAPSHOT_URL,
SwitcherContextBase.contextStr(ContextKey.URL)));
final String url = SwitcherContextBase.contextStr(ContextKey.URL);
final WebTarget myResource = client.target(String.format(SNAPSHOT_URL, url));

final Response response = myResource.request(MediaType.APPLICATION_JSON)
.header(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token))
.post(Entity.json(String.format(QUERY,
SwitcherContextBase.contextStr(ContextKey.DOMAIN),
SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT),
SwitcherContextBase.contextStr(ContextKey.COMPONENT))));

if (response.getStatus() != 200) {
throw new SwitcherSnapshoException("resolveSnapshot");

if (response.getStatus() == 200) {
final Snapshot snapshot = response.readEntity(Snapshot.class);
response.close();

return snapshot;
}

final Snapshot snapshot = response.readEntity(Snapshot.class);
response.close();

return snapshot;

throw new SwitcherRemoteException(url, response.getStatus());
}

@Override
public SnapshotVersionResponse checkSnapshotVersion(final long version, final String token) {
final WebTarget myResource = client.target(String.format(SNAPSHOT_VERSION_CHECK,
SwitcherContextBase.contextStr(ContextKey.URL), version));
final String url = SwitcherContextBase.contextStr(ContextKey.URL);
final WebTarget myResource = client.target(String.format(SNAPSHOT_VERSION_CHECK, url, version));

final Response response = myResource.request(MediaType.APPLICATION_JSON)
.header(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token))
.get();

if (response.getStatus() != 200) {
throw new SwitcherSnapshoException("resolveSnapshot");

if (response.getStatus() == 200) {
final SnapshotVersionResponse snapshotVersionResponse = response.readEntity(SnapshotVersionResponse.class);
response.close();

return snapshotVersionResponse;
}

final SnapshotVersionResponse snapshotVersionResponse = response.readEntity(SnapshotVersionResponse.class);
response.close();

return snapshotVersionResponse;

throw new SwitcherRemoteException(url, response.getStatus());
}

@Override
Expand All @@ -158,22 +152,21 @@ public boolean isAlive() {

@Override
public SwitchersCheck checkSwitchers(Set<String> switchers, final String token) {
final WebTarget myResource = client.target(String.format(CHECK_SWITCHERS,
SwitcherContextBase.contextStr(ContextKey.URL)));
final String url = SwitcherContextBase.contextStr(ContextKey.URL);
final WebTarget myResource = client.target(String.format(CHECK_SWITCHERS, url));

final Response response = myResource.request(MediaType.APPLICATION_JSON)
.header(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token))
.post(Entity.json(new SwitchersCheck(switchers)));

if (response.getStatus() != 200) {
throw new SwitcherException(
String.format("API returned an HTTP/1.1 %s", response.getStatus()), null);

if (response.getStatus() == 200) {
final SwitchersCheck switchersResponse = response.readEntity(SwitchersCheck.class);
response.close();

return switchersResponse;
}

final SwitchersCheck switchersResponse = response.readEntity(SwitchersCheck.class);
response.close();

return switchersResponse;

throw new SwitcherRemoteException(url, response.getStatus());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Set;

import com.github.switcherapi.client.SwitcherContextBase;
import com.github.switcherapi.client.exception.SwitcherAPIConnectionException;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.github.switcherapi.client.exception.SwitcherException;
import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException;
import com.github.switcherapi.client.model.ContextKey;
Expand Down Expand Up @@ -80,7 +80,7 @@ public SwitchersCheck checkSwitchers(final Set<String> switchers) {
return this.clientWs.checkSwitchers(switchers,
this.authResponse.orElseGet(AuthResponse::new).getToken());
} catch (final Exception e) {
throw new SwitcherAPIConnectionException(SwitcherContextBase.contextStr(ContextKey.URL), e);
throw new SwitcherRemoteException(SwitcherContextBase.contextStr(ContextKey.URL), e);
}
}

Expand All @@ -91,21 +91,21 @@ private void auth() {
throw e;
} catch (final Exception e) {
this.setSilentModeExpiration();
throw new SwitcherAPIConnectionException(SwitcherContextBase.contextStr(ContextKey.URL), e);
throw new SwitcherRemoteException(SwitcherContextBase.contextStr(ContextKey.URL), e);
}
}

private boolean isTokenValid() throws SwitcherAPIConnectionException,
private boolean isTokenValid() throws SwitcherRemoteException,
SwitcherInvalidDateTimeArgumentException {

if (this.authResponse.isPresent()) {
if (this.authResponse.get().getToken().equals(ContextKey.SILENT_MODE.getParam())
&& !this.authResponse.get().isExpired()) {
throw new SwitcherAPIConnectionException(SwitcherContextBase.contextStr(ContextKey.URL));
throw new SwitcherRemoteException(SwitcherContextBase.contextStr(ContextKey.URL));
} else {
if (!this.clientWs.isAlive()) {
this.setSilentModeExpiration();
throw new SwitcherAPIConnectionException(SwitcherContextBase.contextStr(ContextKey.URL));
throw new SwitcherRemoteException(SwitcherContextBase.contextStr(ContextKey.URL));
}

return !this.authResponse.orElseGet(AuthResponse::new).isExpired();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import com.github.switcherapi.client.SwitcherContextBase;
import com.github.switcherapi.client.SwitcherExecutor;
import com.github.switcherapi.client.exception.SwitcherAPIConnectionException;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.github.switcherapi.client.exception.SwitchersValidationException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
Expand Down Expand Up @@ -46,14 +46,14 @@ public CriteriaResponse executeCriteria(final Switcher switcher) {
}

return response;
} catch (final SwitcherAPIConnectionException e) {
} catch (final SwitcherRemoteException e) {
logger.error("Failed to execute criteria - {}", e.getMessage());
return executeSilentCriteria(switcher, e);
}
}

private CriteriaResponse executeSilentCriteria(final Switcher switcher,
final SwitcherAPIConnectionException e) {
final SwitcherRemoteException e) {
if (SwitcherContextBase.contextBol(ContextKey.SILENT_MODE)) {
CriteriaResponse response = this.switcherOffline.executeCriteria(switcher);
if (logger.isDebugEnabled()) {
Expand Down
Loading