Skip to content

Commit 92d9dc0

Browse files
authored
feat: support pushing single document with no permissions (#4)
https://coveord.atlassian.net/browse/CDX-420
1 parent 7b4785c commit 92d9dc0

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

src/main/java/com/coveo/pushapiclient/Document.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class Document {
9090
* <p>
9191
* See https://docs.coveo.com/en/107 for more information.
9292
*/
93-
public DocumentPermissions permissions;
93+
public DocumentPermissions[] permissions;
9494
/**
9595
* The file extension of the data you're pushing.
9696
* <p>
@@ -101,5 +101,9 @@ public class Document {
101101
* Example: `.html`
102102
*/
103103
public String fileExtension;
104+
105+
public Document() {
106+
this.permissions = new DocumentPermissions[]{new DocumentPermissions()};
107+
}
104108
}
105109

src/main/java/com/coveo/pushapiclient/DocumentBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public DocumentBuilder(String uri, String title) {
1616
this.document.title = title;
1717
}
1818

19+
public Document getDocument() {
20+
return this.document;
21+
}
22+
1923
public DocumentBuilder withData(String data) {
2024
this.document.data = data;
2125
return this;

src/main/java/com/coveo/pushapiclient/DocumentPermissions.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
public class DocumentPermissions {
44
/**
55
* Whether to allow anonymous users in this permission set.
6-
*
76
* Default value is false.
87
*/
98
public boolean allowAnonymous;
@@ -15,4 +14,10 @@ public class DocumentPermissions {
1514
* The list of denied permissions for this permission set.
1615
*/
1716
public SecurityIdentity[] deniedPermissions;
18-
}
17+
18+
public DocumentPermissions() {
19+
this.allowAnonymous = true;
20+
this.allowedPermissions = new SecurityIdentity[]{};
21+
this.deniedPermissions = new SecurityIdentity[]{};
22+
}
23+
}

src/main/java/com/coveo/pushapiclient/PlatformClient.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,17 @@ public HttpResponse<String> manageSecurityIdentities(String securityProviderId,
113113
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
114114
}
115115

116-
public void pushDocument(String sourceId, Document doc) {
117-
// TODO
116+
public HttpResponse<String> pushDocument(String sourceId, String documentJSON, String documentId) throws IOException, InterruptedException {
117+
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
118+
URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents?documentId=%s", sourceId, documentId));
119+
120+
HttpRequest request = HttpRequest.newBuilder()
121+
.headers(headers)
122+
.PUT(HttpRequest.BodyPublishers.ofString(documentJSON))
123+
.uri(uri)
124+
.build();
125+
126+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
118127
}
119128

120129
public void deleteDocument(String sourceId, String documentId, Boolean deleteChildren) {

src/main/java/com/coveo/pushapiclient/Source.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ public HttpResponse<String> deleteOldSecurityIdentities(String securityProviderI
3333
public HttpResponse<String> manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException {
3434
return this.platformClient.manageSecurityIdentities(securityProviderId, batchConfig);
3535
}
36+
37+
public HttpResponse<String> addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException {
38+
return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri);
39+
}
3640
}

src/main/java/com/coveo/testlocally/TestingLocally.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestingLocally {
1313
public static void main(String[] args) {
1414
Dotenv dotenv = Dotenv.load();
1515

16-
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
16+
1717
Source source = new Source(dotenv.get("API_KEY"), dotenv.get("ORG_ID"));
1818
try {
1919
HttpResponse res = source.create("testlocaljava", SourceVisibility.SECURED);
@@ -22,7 +22,22 @@ public static void main(String[] args) {
2222
e.printStackTrace();
2323
}
2424

25+
testManageIdentities(source);
26+
testPushDocument(dotenv.get("SOURCE_ID"), source);
27+
}
2528

29+
public static void testPushDocument(String sourceId, Source source) {
30+
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
31+
System.out.println(doc.marshal());
32+
try {
33+
source.addOrUpdateDocument(sourceId, doc);
34+
} catch (IOException | InterruptedException e) {
35+
System.out.println(e);
36+
}
37+
38+
}
39+
40+
public static void testManageIdentities(Source source) {
2641
IdentityModel identityModel = new IdentityModel("the_name", SecurityIdentityType.USER, new HashMap() {
2742
});
2843

@@ -31,7 +46,6 @@ public static void main(String[] args) {
3146
};
3247

3348
AliasMapping[] aliasMapping = {new AliasMapping("the_provider_name", "the_name", SecurityIdentityType.USER, new HashMap<>())};
34-
3549
try {
3650
SecurityIdentityModel securityIdentityModel = new SecurityIdentityModel(identityModels, identityModel, identityModels);
3751
String jsonSecurityIdentityModel = new Gson().toJson(securityIdentityModel);

0 commit comments

Comments
 (0)