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
12 changes: 12 additions & 0 deletions src/main/java/com/coveo/pushapiclient/AliasMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.coveo.pushapiclient;

import java.util.Map;

public class AliasMapping extends IdentityModel {
public String provider;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing the variable, you never change the value. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public String provider;
public final String provider;


public AliasMapping(String provider, String name, SecurityIdentityType type, Map<String, String> additionalInfo) {
super(name, type, additionalInfo);
this.provider = provider;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/coveo/pushapiclient/IdentityModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.coveo.pushapiclient;

import java.util.Map;

public class IdentityModel {
public Map<String, String> additionalInfo;
public String name;
public SecurityIdentityType type;
Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing those variables, you never change their values. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public Map<String, String> additionalInfo;
public String name;
public SecurityIdentityType type;
public final Map<String, String> additionalInfo;
public final String name;
public final SecurityIdentityType type;


public IdentityModel(String name, SecurityIdentityType type, Map<String, String> additionalInfo) {
this.name = name;
this.type = type;
this.additionalInfo = additionalInfo;
}
}
79 changes: 69 additions & 10 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,75 @@ public HttpResponse<String> createSource(String name, SourceVisibility sourceVis
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void createOrUpdateSecurityIdentity(String securityProviderId, Object securityIdentityModel) {
// TODO
public HttpResponse<String> createOrUpdateSecurityIdentity(String securityProviderId, SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException {
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + "/permissions");

String json = new Gson().toJson(securityIdentityModel);

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
.PUT(HttpRequest.BodyPublishers.ofString(json))
.uri(uri)
.build();

return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void createOrUpdateSecurityIdentityAlias(String securityProviderId, Object securityIdentityAlias) {
// TODO
public HttpResponse<String> createOrUpdateSecurityIdentityAlias(String securityProviderId, SecurityIdentityAliasModel securityIdentityAlias) throws IOException, InterruptedException {
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + "/mappings");

String json = new Gson().toJson(securityIdentityAlias);

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
.PUT(HttpRequest.BodyPublishers.ofString(json))
.uri(uri)
.build();

return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void deleteSecurityIdentity(String securityProviderId, Object securityIdentityToDelete) {
// TODO
public HttpResponse<String> deleteSecurityIdentity(String securityProviderId, SecurityIdentityDelete securityIdentityToDelete) throws IOException, InterruptedException {
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + "/permissions");

String json = new Gson().toJson(securityIdentityToDelete);

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
.method("DELETE", HttpRequest.BodyPublishers.ofString(json))
.uri(uri)
.build();

return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void deleteOldSecurityIdentities(String securityProviderId, Object batchDelete) {
// TODO
public HttpResponse<String> deleteOldSecurityIdentities(String securityProviderId, SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException {
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/olderthan?orderingId=%s&queueDelay=%s", batchDelete.orderingId, batchDelete.queueDelay));

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
.DELETE()
.uri(uri)
.build();

return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void manageSecurityIdentities(String securityProviderId, Object batchConfig) {
// TODO
public HttpResponse<String> manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException {
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/batch?fileId=%s&orderingId=%s", batchConfig.fileId, batchConfig.orderingId));

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
.PUT(HttpRequest.BodyPublishers.ofString(""))
.uri(uri)
.build();

return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

public void pushDocument(String sourceId, Document doc) {
Expand Down Expand Up @@ -90,6 +141,14 @@ private String getBasePlatformURL() {
return String.format("https://platform.cloud.coveo.com/rest/organizations/%s", this.organizationId);
}

private String getBasePushURL() {
return String.format("https://api.cloud.coveo.com/push/v1/organizations/%s", this.organizationId);
}

private String getBaseProviderURL(String providerId) {
return String.format("%s/providers/%s", this.getBasePushURL(), providerId);
}

private String[] getHeaders(String[]... headers) {
String[] out = new String[]{};
for (String[] header : headers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coveo.pushapiclient;

public class SecurityIdentityAliasModel extends SecurityIdentityModelBase {
public AliasMapping[] mappings;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing the variable, you never change its values. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public AliasMapping[] mappings;
public final AliasMapping[] mappings;


public SecurityIdentityAliasModel(AliasMapping[] mappings, IdentityModel identity, IdentityModel[] wellKnowns) {
super(identity, wellKnowns);
this.mappings = mappings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coveo.pushapiclient;

public class SecurityIdentityBatchConfig {
public String fileId;
public Long orderingId;
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing those variables, you never change their values. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public String fileId;
public Long orderingId;
public final String fileId;
public final Long orderingId;


public SecurityIdentityBatchConfig(String fileId, Long orderingId) {
this.fileId = fileId;
this.orderingId = orderingId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coveo.pushapiclient;

public class SecurityIdentityDelete {
public IdentityModel identity;

public SecurityIdentityDelete(IdentityModel identity) {
this.identity = identity;
}
}
Comment on lines +1 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the goal of this class?
It doesn't seem to me to do anything meaningful

Copy link
Member Author

@olamothe olamothe Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a data class. It is ultimately just serialized in JSON.

Used for source.deleteSecurityIdentity

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coveo.pushapiclient;

public class SecurityIdentityDeleteOptions {
public Integer queueDelay;
public Long orderingId;
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing those variables, you never change their values. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public Integer queueDelay;
public Long orderingId;
public final Integer queueDelay;
public final Long orderingId;


public SecurityIdentityDeleteOptions(Integer queueDelay, Long orderingId) {
this.queueDelay = queueDelay;
this.orderingId = orderingId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coveo.pushapiclient;

public class SecurityIdentityModel extends SecurityIdentityModelBase {
public IdentityModel[] members;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing the variable, you never change itsvalue. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public IdentityModel[] members;
public final IdentityModel[] members;


public SecurityIdentityModel(IdentityModel[] members, IdentityModel identity, IdentityModel[] wellKnowns) {
super(identity, wellKnowns);
this.members = members;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coveo.pushapiclient;

public class SecurityIdentityModelBase {
public IdentityModel identity;
public IdentityModel[] wellKnowns;
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After initializing those variables, you never change their values. You should therefore transcribe that behaviour semantically with the final keyword. It can help the JVM and thus increase the performance.

Suggested change
public IdentityModel identity;
public IdentityModel[] wellKnowns;
public final IdentityModel identity;
public final IdentityModel[] wellKnowns;


public SecurityIdentityModelBase(IdentityModel identity, IdentityModel[] wellKnowns) {
this.identity = identity;
this.wellKnowns = wellKnowns;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/coveo/pushapiclient/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,24 @@ public Source(String apiKey, String organizationId) {
public HttpResponse<String> create(String name, SourceVisibility sourceVisibility) throws IOException, InterruptedException {
return this.platformClient.createSource(name, sourceVisibility);
}

public HttpResponse<String> createOrUpdateSecurityIdentity(String securityProviderId, SecurityIdentityModel securityIdentityModel) throws IOException, InterruptedException {
return this.platformClient.createOrUpdateSecurityIdentity(securityProviderId, securityIdentityModel);
}

public HttpResponse<String> createOrUpdateSecurityIdentityAlias(String securityProviderId, SecurityIdentityAliasModel securityIdentityAliasModel) throws IOException, InterruptedException {
return this.platformClient.createOrUpdateSecurityIdentityAlias(securityProviderId, securityIdentityAliasModel);
}

public HttpResponse<String> deleteSecurityIdentity(String securityProviderId, SecurityIdentityDelete securityIdentityDelete) throws IOException, InterruptedException {
return this.platformClient.deleteSecurityIdentity(securityProviderId, securityIdentityDelete);
}

public HttpResponse<String> deleteOldSecurityIdentities(String securityProviderId, SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException {
return this.platformClient.deleteOldSecurityIdentities(securityProviderId, batchDelete);
}

public HttpResponse<String> manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException {
return this.platformClient.manageSecurityIdentities(securityProviderId, batchConfig);
}
}
51 changes: 45 additions & 6 deletions src/main/java/com/coveo/testlocally/TestingLocally.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.coveo.testlocally;

import com.coveo.pushapiclient.DocumentBuilder;
import com.coveo.pushapiclient.Source;
import com.coveo.pushapiclient.SourceVisibility;
import com.coveo.pushapiclient.*;
import com.google.gson.Gson;
import io.github.cdimascio.dotenv.Dotenv;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.HashMap;

public class TestingLocally {
public static void main(String[] args) {
Expand All @@ -18,12 +18,51 @@ public static void main(String[] args) {
try {
HttpResponse res = source.create("testlocaljava", SourceVisibility.SECURED);
System.out.println(String.format("Status: %s %s", res.statusCode(), res.body().toString()));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}


IdentityModel identityModel = new IdentityModel("the_name", SecurityIdentityType.USER, new HashMap() {
});

IdentityModel[] identityModels = {
identityModel,
};

AliasMapping[] aliasMapping = {new AliasMapping("the_provider_name", "the_name", SecurityIdentityType.USER, new HashMap<>())};

try {
SecurityIdentityModel securityIdentityModel = new SecurityIdentityModel(identityModels, identityModel, identityModels);
String jsonSecurityIdentityModel = new Gson().toJson(securityIdentityModel);
System.out.println(jsonSecurityIdentityModel);
HttpResponse<String> resCreateOrUpdateSecurityIdentity = source.createOrUpdateSecurityIdentity("the_provider_id", securityIdentityModel);
System.out.println(resCreateOrUpdateSecurityIdentity.body());


SecurityIdentityAliasModel securityIdentityAliasModel = new SecurityIdentityAliasModel(aliasMapping, identityModel, identityModels);
String jsonSecurityIdentityAliasModel = new Gson().toJson(securityIdentityAliasModel);
System.out.println(jsonSecurityIdentityAliasModel);
HttpResponse<String> resCreateOrUpdateSecurityIdentityAlias = source.createOrUpdateSecurityIdentityAlias("the_provider_id_mappings", securityIdentityAliasModel);
System.out.println(resCreateOrUpdateSecurityIdentityAlias.body());


SecurityIdentityDelete securityIdentityDelete = new SecurityIdentityDelete(identityModel);
String jsonSecurityIdentityDelete = new Gson().toJson(securityIdentityDelete);
System.out.println(jsonSecurityIdentityDelete);
HttpResponse<String> resDeleteSecurityIdentity = source.deleteSecurityIdentity("the_provider_id_delete", securityIdentityDelete);
System.out.println(resDeleteSecurityIdentity.body());

SecurityIdentityDeleteOptions batchDelete = new SecurityIdentityDeleteOptions(123, 987l);
HttpResponse<String> resDeleteOlderThan = source.deleteOldSecurityIdentities("the_provider_id_delete_older_than", batchDelete);
System.out.println(resDeleteOlderThan.body());

SecurityIdentityBatchConfig batchConfig = new SecurityIdentityBatchConfig("the_file_id", 123l);
HttpResponse<String> resBatchConfig = source.manageSecurityIdentities("the_provider_id_batch_config", batchConfig);
System.out.println(resBatchConfig.body());

} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}