Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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) {
}
8 changes: 3 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 @@ -152,6 +151,9 @@ public String marshal() {
jsonDocument.add(key, new Gson().toJsonTree(value));
});
jsonDocument.remove("metadata");
if (this.document.compressedBinaryData != null) {
jsonDocument.addProperty("compressedBinaryData", this.document.compressedBinaryData.data());
}
return jsonDocument.toString();

}
Expand All @@ -165,10 +167,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) {
// TODO
}
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 @@ -35,6 +35,7 @@ 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);
}
}
16 changes: 14 additions & 2 deletions src/main/java/com/coveo/testlocally/TestingLocally.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

public class TestingLocally {
Expand All @@ -27,22 +27,34 @@ public static void main(String[] args) {
}

public static void testPushDocument(String sourceId, Source source) {
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title").withData("this is searchable").withDate(new Date());
DocumentBuilder doc = new DocumentBuilder("https://perdu.com", "the title")
.withData("this is searchable")
.withDate(123l)
.withModifiedDate(0l);
DocumentBuilder docWithMetadata = new DocumentBuilder("https://perdu.com/3", "the title 3").withMetadata(new HashMap<>() {{
put("foo", "bar");
put("my_field_1", "1");
put("my_field_2", false);
put("my_field_3", 1234);
put("my_field_4", new String[]{"a", "b", "c"});
}});
String encoded = Base64.getEncoder().encodeToString("this is binary data".getBytes());
String decoded = new String(Base64.getDecoder().decode(encoded));
DocumentBuilder docWithBinaryData = new DocumentBuilder("https://perdu.com/binarydata", "the title binary data")
.withCompressedBinaryData(new CompressedBinaryData(encoded, CompressionType.UNCOMPRESSED)).withFileExtension(".txt");


System.out.println(doc.marshal());
System.out.println(docWithMetadata.marshal());
System.out.println(docWithBinaryData.marshal());
try {
source.addOrUpdateDocument(sourceId, doc);
source.addOrUpdateDocument(sourceId, docWithMetadata);
source.addOrUpdateDocument(sourceId, docWithBinaryData);
} catch (IOException | InterruptedException e) {
System.out.println(e);
}

}

public static void testManageIdentities(Source source) {
Expand Down