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
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'

- name: Validate code format
run: mvn spotless:check

- name: Build with Maven
run: mvn -B package --file pom.xml
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public class PushOneDocument {

```

## Local Setup to Contribute

### Formatting

This project uses [Google Java Format](https://github.com/google/google-java-format), so make sure your code is properly formatted before opening a pull request.
```bash
mvn spotless:apply
```

## Release

* Tag the commit following semver.
Expand Down
55 changes: 40 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,50 @@
</repository>
</distributionManagement>

<build>
<plugins>
<!-- Allows generating JAR files containing sources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration />
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Format code -->
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<java>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>

<googleJavaFormat />

</java>
</configuration>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down Expand Up @@ -169,5 +193,6 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotless.version>2.37.0</spotless.version>
</properties>
</project>
11 changes: 6 additions & 5 deletions src/main/java/com/coveo/pushapiclient/AliasMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.util.Map;

public class AliasMapping extends IdentityModel {
public final String provider;
public final String provider;

public AliasMapping(String provider, String name, SecurityIdentityType type, Map<String, String> additionalInfo) {
super(name, type, additionalInfo);
this.provider = provider;
}
public AliasMapping(
String provider, String name, SecurityIdentityType type, Map<String, String> additionalInfo) {
super(name, type, additionalInfo);
this.provider = provider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
import java.util.Arrays;

public class AnySecurityIdentityBuilder implements SecurityIdentityBuilder {
private final String[] identities;
private final SecurityIdentityType securityIdentityType;
private final String securityProvider;
private final String[] identities;
private final SecurityIdentityType securityIdentityType;
private final String securityProvider;

public AnySecurityIdentityBuilder(String identity, SecurityIdentityType securityIdentityType, String securityProvider) {
this.identities = new String[]{identity};
this.securityIdentityType = securityIdentityType;
this.securityProvider = securityProvider;
}
public AnySecurityIdentityBuilder(
String identity, SecurityIdentityType securityIdentityType, String securityProvider) {
this.identities = new String[] {identity};
this.securityIdentityType = securityIdentityType;
this.securityProvider = securityProvider;
}

public AnySecurityIdentityBuilder(String[] identities, SecurityIdentityType securityIdentityType, String securityProvider) {
this.identities = identities;
this.securityIdentityType = securityIdentityType;
this.securityProvider = securityProvider;
}
public AnySecurityIdentityBuilder(
String[] identities, SecurityIdentityType securityIdentityType, String securityProvider) {
this.identities = identities;
this.securityIdentityType = securityIdentityType;
this.securityProvider = securityProvider;
}

public SecurityIdentity[] build() {
return Arrays.stream(this.identities)
.map(identity -> new SecurityIdentity(identity, this.securityIdentityType, this.securityProvider))
.toArray(SecurityIdentity[]::new);
}
public SecurityIdentity[] build() {
return Arrays.stream(this.identities)
.map(
identity ->
new SecurityIdentity(identity, this.securityIdentityType, this.securityProvider))
.toArray(SecurityIdentity[]::new);
}
}
204 changes: 104 additions & 100 deletions src/main/java/com/coveo/pushapiclient/ApiUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,114 +9,118 @@
import java.util.regex.Pattern;

/**
* Private util class to extract dynamic parts from a API URL
* Handles extraction of identifiers and platform URL from a source URL.
*
* @See https://docs.coveo.com/en/1546#push-api-url
* https://docs.coveo.com/en/3295#stream-api-url
* Private util class to extract dynamic parts from a API URL Handles extraction of identifiers and
* platform URL from a source URL. @See https://docs.coveo.com/en/1546#push-api-url
* https://docs.coveo.com/en/3295#stream-api-url
*/
class ApiUrl {
private final String organizationId;
private final String sourceId;
private final PlatformUrl platformUrl;
private final String sourceUrl;

public ApiUrl(URL sourceUrl) throws MalformedURLException {
List<String> identifiers = this.extractIdentifiers(sourceUrl);
this.organizationId = identifiers.get(0);
this.sourceId = identifiers.get(1);
this.sourceUrl = sourceUrl.toString();
this.platformUrl = this.extractPlatformUrl(sourceUrl);
private final String organizationId;
private final String sourceId;
private final PlatformUrl platformUrl;
private final String sourceUrl;

public ApiUrl(URL sourceUrl) throws MalformedURLException {
List<String> identifiers = this.extractIdentifiers(sourceUrl);
this.organizationId = identifiers.get(0);
this.sourceId = identifiers.get(1);
this.sourceUrl = sourceUrl.toString();
this.platformUrl = this.extractPlatformUrl(sourceUrl);
}

public ApiUrl(String organizationId, String sourceId, PlatformUrl platformUrl) {
this.organizationId = organizationId;
this.sourceId = sourceId;
this.platformUrl = platformUrl;
this.sourceUrl =
String.format(
"https://api.cloud.coveo.com/push/v1/organizations/%s/sources/%s",
this.organizationId, this.sourceId);
}

public String getUrl() {
return this.sourceUrl;
}

public String getOrganizationId() {
return this.organizationId;
}

public String getSourceId() {
return this.sourceId;
}

public PlatformUrl getPlatformUrl() {
return this.platformUrl;
}

private List<String> extractIdentifiers(URL sourceUrl) throws MalformedURLException {
String host = sourceUrl.getPath();
Pattern pattern = Pattern.compile("/push/v1/organizations/([^/]+)/sources/([^/]+)");
Matcher matcher = pattern.matcher(host);

if (matcher.find()) {
String organizationId = matcher.group(1);
String sourceId = matcher.group(2);
return Arrays.asList(organizationId, sourceId);
}

public ApiUrl(String organizationId, String sourceId, PlatformUrl platformUrl) {
this.organizationId = organizationId;
this.sourceId = sourceId;
this.platformUrl = platformUrl;
this.sourceUrl = String.format("https://api.cloud.coveo.com/push/v1/organizations/%s/sources/%s",
this.organizationId, this.sourceId);
String errorMessage =
this.getErrorMessage(
"Unable to find organization and source ids from the provided API url");
throw new MalformedURLException(errorMessage);
}

private PlatformUrl extractPlatformUrl(URL sourceUrl) throws MalformedURLException {
String host = sourceUrl.getHost();
Pattern pattern = Pattern.compile("api([a-z]*)([a-z-]*)\\.cloud\\.coveo\\.com");
Matcher matcher = pattern.matcher(host);

if (matcher.find()) {
String extractedEnvironment = matcher.group(1);
String extractedRegion = matcher.group(2).replace("-", "");

Environment urlEnvironment =
extractedEnvironment.isEmpty()
? PlatformUrl.DEFAULT_ENVIRONMENT
: EnumSet.allOf(Environment.class).stream()
.filter(e -> e.getValue().equalsIgnoreCase(extractedEnvironment))
.findFirst()
.orElseThrow(
() ->
new MalformedURLException(
String.format(
"Invalid platform environment '%s'", extractedEnvironment)));

Region urlRegion =
extractedRegion.isEmpty()
? PlatformUrl.DEFAULT_REGION
: EnumSet.allOf(Region.class).stream()
.filter(r -> r.getValue().equalsIgnoreCase(extractedRegion))
.findFirst()
.orElseThrow(
() ->
new MalformedURLException(
String.format("Invalid platform region '%s'", extractedRegion)));

return new PlatformUrl(urlEnvironment, urlRegion);
}

public String getUrl() {
return this.sourceUrl;
}

public String getOrganizationId() {
return this.organizationId;
}

public String getSourceId() {
return this.sourceId;
}

public PlatformUrl getPlatformUrl() {
return this.platformUrl;
}
String invalidHostMessage = this.getErrorMessage("Invalid API URL host");
throw new MalformedURLException(invalidHostMessage);
}

private List<String> extractIdentifiers(URL sourceUrl) throws MalformedURLException {
String host = sourceUrl.getPath();
Pattern pattern = Pattern.compile("/push/v1/organizations/([^/]+)/sources/([^/]+)");
Matcher matcher = pattern.matcher(host);
private String getErrorMessage(String reason) {
String newLine = System.getProperty("line.separator");
String message = "The provided API URL is invalid";

if (matcher.find()) {
String organizationId = matcher.group(1);
String sourceId = matcher.group(2);
return Arrays.asList(organizationId, sourceId);
}
message.concat(newLine).concat(reason);

String errorMessage = this
.getErrorMessage("Unable to find organization and source ids from the provided API url");
throw new MalformedURLException(errorMessage);
}

private PlatformUrl extractPlatformUrl(URL sourceUrl) throws MalformedURLException {
String host = sourceUrl.getHost();
Pattern pattern = Pattern.compile("api([a-z]*)([a-z-]*)\\.cloud\\.coveo\\.com");
Matcher matcher = pattern.matcher(host);

if (matcher.find()) {
String extractedEnvironment = matcher.group(1);
String extractedRegion = matcher.group(2).replace("-", "");

Environment urlEnvironment = extractedEnvironment.isEmpty()
? PlatformUrl.DEFAULT_ENVIRONMENT
: EnumSet.allOf(Environment.class)
.stream()
.filter(e -> e.getValue().equalsIgnoreCase(extractedEnvironment))
.findFirst()
.orElseThrow(() -> new MalformedURLException(
String.format("Invalid platform environment '%s'", extractedEnvironment)));

Region urlRegion = extractedRegion.isEmpty()
? PlatformUrl.DEFAULT_REGION
: EnumSet.allOf(Region.class)
.stream()
.filter(r -> r.getValue().equalsIgnoreCase(extractedRegion))
.findFirst()
.orElseThrow(() -> new MalformedURLException(
String.format("Invalid platform region '%s'", extractedRegion)));

return new PlatformUrl(urlEnvironment, urlRegion);

}

String invalidHostMessage = this.getErrorMessage("Invalid API URL host");
throw new MalformedURLException(invalidHostMessage);
}

private String getErrorMessage(String reason) {
String newLine = System.getProperty("line.separator");
String message = "The provided API URL is invalid";

message.concat(newLine).concat(reason);

message
.concat(newLine)
.concat("For a Push Source, visit: https://docs.coveo.com/en/1546")
.concat(newLine)
.concat("For a Catalog Source, visit:https://docs.coveo.com/en/3295");

return message;
}
message
.concat(newLine)
.concat("For a Push Source, visit: https://docs.coveo.com/en/1546")
.concat(newLine)
.concat("For a Catalog Source, visit:https://docs.coveo.com/en/3295");

return message;
}
}
Loading