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
23 changes: 8 additions & 15 deletions src/main/java/com/coveo/pushapiclient/CompressedBinaryData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

/**
* The original binary item content, compressed using one of the supported compression types (Deflate, GZip, LZMA, Uncompressed, or ZLib), and then Base64 encoded.
*
* <p>
* 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.
*
* <p>
* Whenever you're pushing an item whose size is 5 MB or more, use the CompressedBinaryDataFileIdproperty instead.
*
* <p>
* If you're pushing less than 5 MB of textual (non-binary) content, you can use the data property instead.
*
* <p>
* See https://docs.coveo.com/en/73 for more information.
*
* @param data The base64 encoded binary data. Example: `eJxzrUjMLchJBQAK4ALN`
* @param compressionType The compression type that was applied to your document.
*/
public class CompressedBinaryData {
/**
* The compression type that was applied to your document.
*/
public CompressionType compressionType;
/**
* The base64 encoded binary data.
*
* Example: `eJxzrUjMLchJBQAK4ALN`
*/
public String data;
public record CompressedBinaryData(String data, CompressionType compressionType) {
}
10 changes: 5 additions & 5 deletions src/main/java/com/coveo/pushapiclient/DocumentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public DocumentBuilder withPermanentId(String permanentId) {
}

public DocumentBuilder withCompressedBinaryData(CompressedBinaryData compressedBinaryData) {
this.validateCompressedBinaryData(compressedBinaryData.data);
this.document.compressedBinaryData = compressedBinaryData;
return this;
}
Expand Down Expand Up @@ -161,6 +160,11 @@ public JsonObject marshalJsonObject() {
jsonDocument.add(key, new Gson().toJsonTree(value));
});
jsonDocument.remove("metadata");

if (this.document.compressedBinaryData != null) {
jsonDocument.addProperty("compressedBinaryData", this.document.compressedBinaryData.data());
}

jsonDocument.addProperty("documentId", this.document.uri);
return jsonDocument;
}
Expand All @@ -174,10 +178,6 @@ private void setMetadataValue(String key, Object metadataValue) {
this.document.metadata.put(key, metadataValue);
}

private void validateCompressedBinaryData(String data) {
// TODO
}

private void validateFileExtension(String fileExtension) {
if (!fileExtension.startsWith(".")) {
throw new RuntimeException(String.format("%s is not a valid file extension. It should start with a leading ."));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public HttpResponse<String> manageSecurityIdentities(String securityProviderId,
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

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

HttpRequest request = HttpRequest.newBuilder()
.headers(headers)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/coveo/pushapiclient/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public HttpResponse<String> manageSecurityIdentities(String securityProviderId,
}

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

public HttpResponse<String> deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/coveo/testlocally/TestingLocally.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;

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

String encoded = Base64.getEncoder().encodeToString("this is binary data".getBytes());
DocumentBuilder docWithBinaryData = new DocumentBuilder("https://perdu.com/binarydata", "the title binary data")
.withCompressedBinaryData(new CompressedBinaryData(encoded, CompressionType.UNCOMPRESSED)).withFileExtension(".txt");

ArrayList<DocumentBuilder> docToAdd = new ArrayList<>();
ArrayList<DocumentBuilder> docToRemove = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Expand All @@ -56,13 +61,14 @@ public static void testPushDocument(String sourceId, Source source) {
HttpResponse<String> resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata);
HttpResponse<String> resDelete = source.deleteDocument(sourceId, simpleDoc.getDocument().uri, true);
HttpResponse<String> resBatch = source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove));
HttpResponse<String> resBinaryDoc = source.addOrUpdateDocument(sourceId, docWithBinaryData);


System.out.println(resAddSimpleDoc.statusCode());
System.out.println(resAddOrUpdateMetadata.statusCode());
System.out.println(resDelete.statusCode());
System.out.println(resBatch.statusCode());

System.out.println(resBinaryDoc.statusCode());

} catch (IOException | InterruptedException e) {
System.out.println(e);
Expand Down