Skip to content

Commit 40ededc

Browse files
authored
feat: add support for compressed binary data (#10)
https://coveord.atlassian.net/browse/CDX-434
1 parent 3b5dcc4 commit 40ededc

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
/**
44
* The original binary item content, compressed using one of the supported compression types (Deflate, GZip, LZMA, Uncompressed, or ZLib), and then Base64 encoded.
5-
*
5+
* <p>
66
* You can use this parameter when you're pushing a compressed binary item (such as XML/HTML, PDF, Word, or binary) whose size is less than 5 MB.
7-
*
7+
* <p>
88
* Whenever you're pushing an item whose size is 5 MB or more, use the CompressedBinaryDataFileIdproperty instead.
9-
*
9+
* <p>
1010
* If you're pushing less than 5 MB of textual (non-binary) content, you can use the data property instead.
11-
*
11+
* <p>
1212
* See https://docs.coveo.com/en/73 for more information.
13+
*
14+
* @param data The base64 encoded binary data. Example: `eJxzrUjMLchJBQAK4ALN`
15+
* @param compressionType The compression type that was applied to your document.
1316
*/
14-
public class CompressedBinaryData {
15-
/**
16-
* The compression type that was applied to your document.
17-
*/
18-
public CompressionType compressionType;
19-
/**
20-
* The base64 encoded binary data.
21-
*
22-
* Example: `eJxzrUjMLchJBQAK4ALN`
23-
*/
24-
public String data;
17+
public record CompressedBinaryData(String data, CompressionType compressionType) {
2518
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public DocumentBuilder withPermanentId(String permanentId) {
8585
}
8686

8787
public DocumentBuilder withCompressedBinaryData(CompressedBinaryData compressedBinaryData) {
88-
this.validateCompressedBinaryData(compressedBinaryData.data);
8988
this.document.compressedBinaryData = compressedBinaryData;
9089
return this;
9190
}
@@ -161,6 +160,11 @@ public JsonObject marshalJsonObject() {
161160
jsonDocument.add(key, new Gson().toJsonTree(value));
162161
});
163162
jsonDocument.remove("metadata");
163+
164+
if (this.document.compressedBinaryData != null) {
165+
jsonDocument.addProperty("compressedBinaryData", this.document.compressedBinaryData.data());
166+
}
167+
164168
jsonDocument.addProperty("documentId", this.document.uri);
165169
return jsonDocument;
166170
}
@@ -174,10 +178,6 @@ private void setMetadataValue(String key, Object metadataValue) {
174178
this.document.metadata.put(key, metadataValue);
175179
}
176180

177-
private void validateCompressedBinaryData(String data) {
178-
// TODO
179-
}
180-
181181
private void validateFileExtension(String fileExtension) {
182182
if (!fileExtension.startsWith(".")) {
183183
throw new RuntimeException(String.format("%s is not a valid file extension. It should start with a leading ."));

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

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

116-
public HttpResponse<String> pushDocument(String sourceId, String documentJSON, String documentId) throws IOException, InterruptedException {
116+
public HttpResponse<String> pushDocument(String sourceId, String documentJSON, String documentId, CompressionType compressionType) throws IOException, InterruptedException {
117117
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
118-
URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents?documentId=%s", sourceId, documentId));
118+
URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents?documentId=%s&compressionType=%s", sourceId, documentId, compressionType.toString()));
119119

120120
HttpRequest request = HttpRequest.newBuilder()
121121
.headers(headers)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public HttpResponse<String> manageSecurityIdentities(String securityProviderId,
3737
}
3838

3939
public HttpResponse<String> addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException {
40-
return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri);
40+
CompressionType compressionType = docBuilder.getDocument().compressedBinaryData != null ? docBuilder.getDocument().compressedBinaryData.compressionType() : CompressionType.UNCOMPRESSED;
41+
return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri, compressionType);
4142
}
4243

4344
public HttpResponse<String> deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.IOException;
88
import java.net.http.HttpResponse;
99
import java.util.ArrayList;
10+
import java.util.Base64;
1011
import java.util.Date;
1112
import java.util.HashMap;
1213

@@ -42,6 +43,10 @@ public static void testPushDocument(String sourceId, Source source) {
4243
.withAllowedPermissions(new UserSecurityIdentityBuilder("[email protected]"))
4344
.withDeniedPermissions(new UserSecurityIdentityBuilder(new String[]{"[email protected]", "[email protected]"}));
4445

46+
String encoded = Base64.getEncoder().encodeToString("this is binary data".getBytes());
47+
DocumentBuilder docWithBinaryData = new DocumentBuilder("https://perdu.com/binarydata", "the title binary data")
48+
.withCompressedBinaryData(new CompressedBinaryData(encoded, CompressionType.UNCOMPRESSED)).withFileExtension(".txt");
49+
4550
ArrayList<DocumentBuilder> docToAdd = new ArrayList<>();
4651
ArrayList<DocumentBuilder> docToRemove = new ArrayList<>();
4752
for (int i = 0; i < 10; i++) {
@@ -56,13 +61,14 @@ public static void testPushDocument(String sourceId, Source source) {
5661
HttpResponse<String> resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata);
5762
HttpResponse<String> resDelete = source.deleteDocument(sourceId, simpleDoc.getDocument().uri, true);
5863
HttpResponse<String> resBatch = source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove));
64+
HttpResponse<String> resBinaryDoc = source.addOrUpdateDocument(sourceId, docWithBinaryData);
5965

6066

6167
System.out.println(resAddSimpleDoc.statusCode());
6268
System.out.println(resAddOrUpdateMetadata.statusCode());
6369
System.out.println(resDelete.statusCode());
6470
System.out.println(resBatch.statusCode());
65-
71+
System.out.println(resBinaryDoc.statusCode());
6672

6773
} catch (IOException | InterruptedException e) {
6874
System.out.println(e);

0 commit comments

Comments
 (0)