Skip to content

Commit

Permalink
Added IB Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Yong Sheng Tan authored and Yong Sheng committed Jul 29, 2019
1 parent 5a2a608 commit 2835414
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.symphony.platformsolutions</groupId>
<artifactId>symphony-api-client-java</artifactId>
<version>1.0.35</version>
<version>1.0.36-SNAPSHOT</version>
<name>Symphony API Client</name>
<url>https://github.com/SymphonyPlatformSolutions/symphony-api-client-java</url>
<description>Symphony API Client provided by Symphony Platform Solutions team</description>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/clients/SymBotClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class SymBotClient implements ISymClient {
private FirehoseClient firehoseClient;
private FirehoseService firehoseService;
private HealthcheckClient healthcheckClient;
private InformationBarriersClient informationBarriersClient;

public static SymBotClient initBotRsa(String configPath) throws NoConfigException {
return initBotRsa(configPath, SymConfig.class);
Expand Down Expand Up @@ -319,6 +320,13 @@ public HealthcheckClient getHealthcheckClient() {
return healthcheckClient;
}

public InformationBarriersClient getInformationBarriersClient() {
if (informationBarriersClient == null) {
informationBarriersClient = new InformationBarriersClient(this);
}
return informationBarriersClient;
}

public static void clearBotClient() {
botClient = null;
}
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/clients/symphony/api/InformationBarriersClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package clients.symphony.api;

import clients.ISymClient;
import clients.symphony.api.constants.CommonConstants;
import clients.symphony.api.constants.PodConstants;
import exceptions.SymClientException;
import exceptions.UnauthorizedException;
import model.InformationBarrierGroup;
import model.InformationBarrierGroupStatus;
import model.Policy;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

public class InformationBarriersClient extends APIClient {
private ISymClient botClient;
private static String podTarget;

public InformationBarriersClient(ISymClient client) {
botClient = client;
podTarget = CommonConstants.HTTPS_PREFIX + botClient.getConfig().getPodHost();
if (botClient.getConfig().getPodPort() != 443) {
podTarget += ":" + botClient.getConfig().getPodPort();
}
}

public List<InformationBarrierGroup> listGroups() throws SymClientException {
Invocation.Builder builder = botClient.getPodClient()
.target(podTarget)
.path(PodConstants.LISTIBGROUPS)
.request(MediaType.APPLICATION_JSON)
.header("sessionToken", botClient.getSymAuth().getSessionToken());

try (Response response = builder.get()) {
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
try {
handleError(response, botClient);
} catch (UnauthorizedException ex) {
return listGroups();
}
return null;
} else {
return response.readEntity(new GenericType<List<InformationBarrierGroup>>() {});
}
}
}

public List<Long> listGroupMembers(String groupId) throws SymClientException {
Invocation.Builder builder = botClient.getPodClient()
.target(podTarget)
.path(PodConstants.LISTIBGROUPMEMBERS.replace("{gid}", groupId))
.request(MediaType.APPLICATION_JSON)
.header("sessionToken", botClient.getSymAuth().getSessionToken());

try (Response response = builder.get()) {
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
try {
handleError(response, botClient);
} catch (UnauthorizedException ex) {
return listGroupMembers(groupId);
}
return null;
} else {
return response.readEntity(new GenericType<List<Long>>() {});
}
}
}

public InformationBarrierGroupStatus addGroupMembers(String groupId, List<Long> members) throws SymClientException {
Invocation.Builder builder = botClient.getPodClient()
.target(podTarget)
.path(PodConstants.ADDIBGROUPMEMBERS.replace("{gid}", groupId))
.request(MediaType.APPLICATION_JSON)
.header("sessionToken", botClient.getSymAuth().getSessionToken());

try (Response response = builder.post(Entity.entity(members, MediaType.APPLICATION_JSON))) {
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
try {
handleError(response, botClient);
} catch (UnauthorizedException ex) {
return addGroupMembers(groupId, members);
}
return null;
} else {
return response.readEntity(InformationBarrierGroupStatus.class);
}
}
}

public InformationBarrierGroupStatus removeGroupMembers(String groupId, List<Long> members) throws SymClientException {
Invocation.Builder builder = botClient.getPodClient()
.target(podTarget)
.path(PodConstants.REMOVEIBGROUPMEMBERS.replace("{gid}", groupId))
.request(MediaType.APPLICATION_JSON)
.header("sessionToken", botClient.getSymAuth().getSessionToken());

try (Response response = builder.post(Entity.entity(members, MediaType.APPLICATION_JSON))) {
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
try {
handleError(response, botClient);
} catch (UnauthorizedException ex) {
return removeGroupMembers(groupId, members);
}
return null;
} else {
return response.readEntity(InformationBarrierGroupStatus.class);
}
}
}

public List<Policy> listPolicies() throws SymClientException {
Invocation.Builder builder = botClient.getPodClient()
.target(podTarget)
.path(PodConstants.LISTPOLICIES)
.request(MediaType.APPLICATION_JSON)
.header("sessionToken", botClient.getSymAuth().getSessionToken());

try (Response response = builder.get()) {
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
try {
handleError(response, botClient);
} catch (UnauthorizedException ex) {
return listPolicies();
}
return null;
} else {
return response.readEntity(new GenericType<List<Policy>>() {});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public class PodConstants {
public static final String ADMINUPDATEUSER = POD + "/v2/admin/user/{uid}/update";
public static final String ADMINUPDATEAVATAR = POD + "/v1/admin/user/{uid}/avatar/update";
public static final String GETSESSIONUSER = POD + "/v2/sessioninfo";
public static final String LISTIBGROUPS = POD + "/v1/admin/group/list";
public static final String LISTIBGROUPMEMBERS = POD + "/v1/admin/group/{gid}/membership/list";
public static final String ADDIBGROUPMEMBERS = POD + "/v1/admin/group/{gid}/membership/add";
public static final String REMOVEIBGROUPMEMBERS = POD + "/v1/admin/group/{gid}/membership/remove";
public static final String LISTPOLICIES = POD + "/v1/admin/policy/list";
}
71 changes: 71 additions & 0 deletions src/main/java/model/InformationBarrierGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class InformationBarrierGroup {
private String id;
private String name;
private boolean active;
private int memberCount;
private List<String> policies;
private long createdDate;
private long modifiedDate;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

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

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public int getMemberCount() {
return memberCount;
}

public void setMemberCount(int memberCount) {
this.memberCount = memberCount;
}

public List<String> getPolicies() {
return policies;
}

public void setPolicies(List<String> policies) {
this.policies = policies;
}

public long getCreatedDate() {
return createdDate;
}

public void setCreatedDate(long createdDate) {
this.createdDate = createdDate;
}

public long getModifiedDate() {
return modifiedDate;
}

public void setModifiedDate(long modifiedDate) {
this.modifiedDate = modifiedDate;
}
}
26 changes: 26 additions & 0 deletions src/main/java/model/InformationBarrierGroupStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class InformationBarrierGroupStatus {
private String overallResult;
private List<String> results;

public String getOverallResult() {
return overallResult;
}

public void setOverallResult(String overallResult) {
this.overallResult = overallResult;
}

public List<String> getResults() {
return results;
}

public void setResults(List<String> results) {
this.results = results;
}
}
62 changes: 62 additions & 0 deletions src/main/java/model/Policy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Policy {
private String id;
private String policyType;
private boolean active;
private List<String> groups;
private long createdDate;
private long modifiedDate;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPolicyType() {
return policyType;
}

public void setPolicyType(String policyType) {
this.policyType = policyType;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public List<String> getGroups() {
return groups;
}

public void setGroups(List<String> groups) {
this.groups = groups;
}

public long getCreatedDate() {
return createdDate;
}

public void setCreatedDate(long createdDate) {
this.createdDate = createdDate;
}

public long getModifiedDate() {
return modifiedDate;
}

public void setModifiedDate(long modifiedDate) {
this.modifiedDate = modifiedDate;
}
}
Loading

0 comments on commit 2835414

Please sign in to comment.