Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 22 additions & 12 deletions src/main/java/com/coveo/pushapiclient/PlatformClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class PlatformClient {
private final String organizationId;
private final ApiCore api;
private final PlatformUrl platformUrl;
private UserAgent userAgent;

/**
* Construct a PlatformClient
Expand Down Expand Up @@ -571,21 +572,22 @@ private String[] getAuthorizationHeader() {

private String[] getContentTypeApplicationJSONHeader() {
MavenXpp3Reader reader = new MavenXpp3Reader();
String version = "";
try {
Model model = reader.read(new FileReader("pom.xml"));
version = model.getVersion();
} catch (Exception e) {
version = "Not-Available";
String userAgentValue = "";
if (userAgent != null) {
userAgentValue = userAgent.toString();
} else {
String version = "";
try {
Model model = reader.read(new FileReader("pom.xml"));
version = model.getVersion();
} catch (Exception e) {
version = "Not-Available";
}
userAgentValue = String.format("CoveoSDKJava/%s", version);
}

return new String[] {
"Content-Type",
"application/json",
"Accept",
"application/json",
"User-Agent",
String.format("CoveoSDKJava/%s", version)
"Content-Type", "application/json", "Accept", "application/json", "User-Agent", userAgentValue
};
}

Expand All @@ -600,4 +602,12 @@ private String[] getContentTypeApplicationOctetStreamHeader() {
private String toJSON(HashMap<String, Object> hashMap) {
return new Gson().toJson(hashMap, new TypeToken<HashMap<String, Object>>() {}.getType());
}

public UserAgent getUserAgent() {
return userAgent;
}

public void setUserAgent(UserAgent userAgent) {
this.userAgent = userAgent;
}
}
35 changes: 34 additions & 1 deletion src/main/java/com/coveo/pushapiclient/StreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ public class StreamService {
private String streamId;
private DocumentUploadQueue queue;

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API.
*
* <p>To perform <a href="https://docs.coveo.com/en/l62e0540">full document updates or
* deletions</a>, use the {@UpdateStreamService}, since pushing documents with the
* {@StreamService} is equivalent to triggering a full source rebuild. The {@StreamService} can
* also be used for an initial catalog upload.
*
* @param source The source to which you want to send your documents.
* @param userAgent The user agent to use for the requests.
*/
public StreamService(StreamEnabledSource source, UserAgent userAgent) {
this(source, new BackoffOptionsBuilder().build(), userAgent);
}

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API.
Expand Down Expand Up @@ -42,6 +58,23 @@ public StreamService(StreamEnabledSource source) {
* @param options The configuration options for exponential backoff.
*/
public StreamService(StreamEnabledSource source, BackoffOptions options) {
this(source, options, null);
}

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API.
*
* <p>To perform <a href="https://docs.coveo.com/en/l62e0540">full document updates or
* deletions</a>, use the {@UpdateStreamService}, since pushing documents with the
* {@StreamService} is equivalent to triggering a full source rebuild. The {@StreamService} can
* also be used for an initial catalog upload.
*
* @param source The source to which you want to send your documents.
* @param options The configuration options for exponential backoff.
* @param userAgent The user agent to use for the requests.
*/
public StreamService(StreamEnabledSource source, BackoffOptions options, UserAgent userAgent) {
String apiKey = source.getApiKey();
String organizationId = source.getOrganizationId();
PlatformUrl platformUrl = source.getPlatformUrl();
Expand All @@ -51,7 +84,7 @@ public StreamService(StreamEnabledSource source, BackoffOptions options) {
this.source = source;
this.queue = new DocumentUploadQueue(uploader);
this.platformClient = new PlatformClient(apiKey, organizationId, platformUrl, options);

platformClient.setUserAgent(userAgent);
this.service = new StreamServiceInternal(this.source, this.queue, this.platformClient, logger);
}

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/coveo/pushapiclient/UpdateStreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public class UpdateStreamService {

private FileContainer fileContainer;

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API. This provides the ability to incrementally add, update, or delete documents via a
* stream.
*
* <p>To perform <a href="https://docs.coveo.com/en/lb4a0344">a full source rebuild</a>, use the
* {@StreamService}
*
* @param source The source to which you want to send your documents.
* @param userAgent The user agent to use for the requests.
*/
public UpdateStreamService(StreamEnabledSource source, UserAgent userAgent) {
this(source, new BackoffOptionsBuilder().build(), userAgent);
}

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API. This provides the ability to incrementally add, update, or delete documents via a
Expand All @@ -40,10 +55,28 @@ public UpdateStreamService(StreamEnabledSource source) {
* @param options The configuration options for exponential backoff.
*/
public UpdateStreamService(StreamEnabledSource source, BackoffOptions options) {
this(source, options, null);
}

/**
* Creates a service to stream your documents to the provided source by interacting with the
* Stream API. This provides the ability to incrementally add, update, or delete documents via a
* stream.
*
* <p>To perform <a href="https://docs.coveo.com/en/lb4a0344">a full source rebuild</a>, use the
* {@StreamService}
*
* @param source The source to which you want to send your documents.
* @param options The configuration options for exponential backoff.
* @param userAgent The user agent to use for the requests.
*/
public UpdateStreamService(
StreamEnabledSource source, BackoffOptions options, UserAgent userAgent) {
Logger logger = LogManager.getLogger(UpdateStreamService.class);
this.platformClient =
new PlatformClient(
source.getApiKey(), source.getOrganizationId(), source.getPlatformUrl(), options);
this.platformClient.setUserAgent(userAgent);
this.updateStreamServiceInternal =
new UpdateStreamServiceInternal(
source,
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/coveo/pushapiclient/UserAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.coveo.pushapiclient;

public enum UserAgent {
SAP_COMMERCE_CLOUD_V1 {
@Override
public String toString() {
return "SAPCommerceCloud/v1";
}
},
SAP_COMMERCE_CLOUD_V2 {
@Override
public String toString() {
return "SAPCommerceCloud/v2";
}
},
SAP_COMMERCE_CLOUD_V3 {
@Override
public String toString() {
return "SAPCommerceCloud/v3";
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/coveo/pushapiclient/PlatformClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import static org.mockito.Mockito.verify;

import com.google.gson.Gson;
import java.io.FileReader;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -39,6 +43,10 @@ public void assertApplicationJsonHeader() {
assertTrue(this.argument.getValue().headers().map().get("Accept").contains("application/json"));
}

public void assertUserAgentHeader(String userAgentValue) {
assertTrue(this.argument.getValue().headers().map().get("User-Agent").contains(userAgentValue));
}

public SecurityIdentityModel securityIdentityModel() {
return new SecurityIdentityModel(identityModels(), identityModel(), identityModels());
}
Expand Down Expand Up @@ -472,4 +480,26 @@ public void testDeleteDocument() throws IOException, InterruptedException {
assertApplicationJsonHeader();
assertAuthorizationHeader();
}

@Test
public void testCorrectUserAgentHeader() throws IOException, InterruptedException {
client.setUserAgent(UserAgent.SAP_COMMERCE_CLOUD_V1);
client.createSource("the_name", SourceType.PUSH, SourceVisibility.SECURED);
verify(httpClient)
.send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));

assertUserAgentHeader(UserAgent.SAP_COMMERCE_CLOUD_V1.toString());
}

@Test
public void testDefaultUserAgentHeader()
throws IOException, InterruptedException, XmlPullParserException {
client.createSource("the_name", SourceType.PUSH, SourceVisibility.SECURED);
verify(httpClient)
.send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass()));
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
String version = model.getVersion();
assertUserAgentHeader(String.format("CoveoSDKJava/%s", version));
}
}