diff --git a/src/main/java/com/coveo/pushapiclient/CompressedBinaryData.java b/src/main/java/com/coveo/pushapiclient/CompressedBinaryData.java index 56810b38..c8352e40 100644 --- a/src/main/java/com/coveo/pushapiclient/CompressedBinaryData.java +++ b/src/main/java/com/coveo/pushapiclient/CompressedBinaryData.java @@ -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. - * + *

* 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. - * + *

* Whenever you're pushing an item whose size is 5 MB or more, use the CompressedBinaryDataFileIdproperty instead. - * + *

* If you're pushing less than 5 MB of textual (non-binary) content, you can use the data property instead. - * + *

* 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) { } \ No newline at end of file diff --git a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java index e384b041..08561574 100644 --- a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java +++ b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java @@ -85,7 +85,6 @@ public DocumentBuilder withPermanentId(String permanentId) { } public DocumentBuilder withCompressedBinaryData(CompressedBinaryData compressedBinaryData) { - this.validateCompressedBinaryData(compressedBinaryData.data); this.document.compressedBinaryData = compressedBinaryData; return this; } @@ -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; } @@ -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 .")); diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 574a17ec..4d6d3083 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -113,9 +113,9 @@ public HttpResponse manageSecurityIdentities(String securityProviderId, return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } - public HttpResponse pushDocument(String sourceId, String documentJSON, String documentId) throws IOException, InterruptedException { + public HttpResponse 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) diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index c77b30f6..2d74caa9 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -37,7 +37,8 @@ public HttpResponse manageSecurityIdentities(String securityProviderId, } public HttpResponse 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 deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java index 8d16ccc7..248f1821 100644 --- a/src/main/java/com/coveo/testlocally/TestingLocally.java +++ b/src/main/java/com/coveo/testlocally/TestingLocally.java @@ -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; @@ -42,6 +43,10 @@ public static void testPushDocument(String sourceId, Source source) { .withAllowedPermissions(new UserSecurityIdentityBuilder("olamothe@coveo.com")) .withDeniedPermissions(new UserSecurityIdentityBuilder(new String[]{"lbompart@coveo.com", "ylakhdar@coveo.com"})); + 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 docToAdd = new ArrayList<>(); ArrayList docToRemove = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -56,13 +61,14 @@ public static void testPushDocument(String sourceId, Source source) { HttpResponse resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata); HttpResponse resDelete = source.deleteDocument(sourceId, simpleDoc.getDocument().uri, true); HttpResponse resBatch = source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove)); + HttpResponse 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);