Skip to content

Commit

Permalink
Merge branch 'master' into feature/search-protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Yufei Cai <[email protected]>
  • Loading branch information
Yufei Cai committed Apr 16, 2020
2 parents dd172a2 + 21efb53 commit 9c1743f
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
java: [ '1.8' ]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: set up JDK ${{ matrix.java }}
uses: actions/setup-java@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
node-version: [10.x, 12.x]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ AuthenticationProvider authenticationProvider =
```java
MessagingProvider messagingProvider = MessagingProviders.webSocket(WebSocketMessagingConfiguration.newBuilder()
.endpoint("wss://ditto.eclipse.org")
.jsonSchemaVersion(JsonSchemaVersion.V_1)
.jsonSchemaVersion(JsonSchemaVersion.V_2)
.proxyConfiguration(proxyConfig) // optionally configure a proxy server
// optionally configure a truststore containing the trusted CAs for SSL connection establishment
.trustStoreConfiguration(TrustStoreConfiguration.newBuilder()
Expand Down
14 changes: 11 additions & 3 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
<ditto.thirdPartyLicences.excludedGroups>
(org\.eclipse\.ditto.*)
</ditto.thirdPartyLicences.excludedGroups>

<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<build>
Expand Down Expand Up @@ -382,7 +384,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<version>0.8.5</version>
</plugin>
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
Expand Down Expand Up @@ -714,15 +716,15 @@
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare</id>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report-aggregate</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
Expand Down Expand Up @@ -1210,6 +1212,12 @@
<goals>
<goal>sign</goal>
</goals>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -90,6 +92,7 @@ private static final class WebSocketMessagingConfigurationBuilder implements Mes

private static final List<String> ALLOWED_URI_SCHEME = Arrays.asList("wss", "ws");
private static final String WS_PATH = "/ws/";
private static final String WS_PATH_REGEX = "/ws/([12])/?";

private Duration timeout = Duration.ofSeconds(60L);
private JsonSchemaVersion jsonSchemaVersion = JsonSchemaVersion.LATEST;
Expand Down Expand Up @@ -144,15 +147,42 @@ public MessagingConfiguration.Builder trustStoreConfiguration(

@Override
public MessagingConfiguration build() {
final URI wsEndpointUri = appendWsPath(this.endpointUri, jsonSchemaVersion);
final URI wsEndpointUri = appendWsPathIfNecessary(this.endpointUri, jsonSchemaVersion);
return new WebSocketMessagingConfiguration(timeout, jsonSchemaVersion, wsEndpointUri, reconnectEnabled,
proxyConfiguration, trustStoreConfiguration);
}

private static URI appendWsPath(final URI baseUri, final JsonSchemaVersion schemaVersion) {
final String pathWithoutTrailingSlashes = baseUri.getPath().replaceFirst("/+$", "");
final String newPath = pathWithoutTrailingSlashes + WS_PATH + schemaVersion.toString();
return baseUri.resolve(newPath);
private static URI appendWsPathIfNecessary(final URI baseUri, final JsonSchemaVersion schemaVersion) {
if (needToAppendWsPath(baseUri)) {
final String pathWithoutTrailingSlashes = removeTrailingSlashFromPath(baseUri.getPath());
final String newPath = pathWithoutTrailingSlashes + WS_PATH + schemaVersion.toString();
return baseUri.resolve(newPath);
} else {
checkIfBaseUriAndSchemaVersionMatch(baseUri, schemaVersion);
return baseUri;
}
}

private static boolean needToAppendWsPath(final URI baseUri) {
final Pattern pattern = Pattern.compile(WS_PATH_REGEX);
final Matcher matcher = pattern.matcher(baseUri.toString());
return !matcher.find();
}

private static void checkIfBaseUriAndSchemaVersionMatch(final URI baseUri,
final JsonSchemaVersion schemaVersion) {
final String path = removeTrailingSlashFromPath(baseUri.getPath());
final String apiVersion = path.substring(path.length() - 1);
if (!schemaVersion.toString().equals(apiVersion)) {
throw new IllegalArgumentException(
"The jsonSchemaVersion and apiVersion of the endpoint do not match. " +
"Either remove the ws path from the endpoint or " +
"use the same jsonSchemaVersion as in the ws path of the endpoint.");
}
}

private static String removeTrailingSlashFromPath(final String path) {
return path.replaceFirst("/+$", "");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public abstract class CommonManagementImpl<T extends ThingHandle<F>, F extends F
extends AbstractHandle
implements CommonManagement<T, F> {

private static final String ARGUMENT_THING_ID = "thingId";
private static final String ARGUMENT_THING = "thing";
private static final String ARGUMENT_INITIAL_POLICY = "initialPolicy";
private static final Logger LOGGER = LoggerFactory.getLogger(CommonManagementImpl.class);

protected final OutgoingMessageFactory outgoingMessageFactory;
Expand Down Expand Up @@ -280,20 +283,21 @@ public CompletableFuture<Thing> create(final JsonObject jsonObject, final Option
}

@Override
public CompletableFuture<Thing> create(final Policy policy, final Option<?>... options) {
public CompletableFuture<Thing> create(final Policy initialPolicy, final Option<?>... options) {
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);
// as the backend adds the default namespace, we can here simply use the empty namespace.
final Thing thing = ThingsModelFactory.newThingBuilder()
.setId(ThingId.generateRandom())
.build();
return processCreate(thing, policy.toJson(), options);
return processCreate(thing, initialPolicy.toJson(), options);
}

@Override
public CompletableFuture<Thing> create(final ThingId thingId, final JsonObject initialPolicy,
final Option<?>... options) {
argumentNotNull(thingId);
argumentNotNull(thingId, ARGUMENT_THING_ID);
argumentNotEmpty(thingId);
argumentNotNull(initialPolicy);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

final Thing thing = ThingsModelFactory.newThingBuilder()
.setId(ThingId.of(thingId))
Expand All @@ -305,9 +309,9 @@ public CompletableFuture<Thing> create(final ThingId thingId, final JsonObject i
@Override
public CompletableFuture<Thing> create(final ThingId thingId, final Policy initialPolicy,
final Option<?>... options) {
argumentNotNull(thingId);
argumentNotNull(thingId, ARGUMENT_THING_ID);
argumentNotEmpty(thingId);
argumentNotNull(initialPolicy);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

final Thing thing = ThingsModelFactory.newThingBuilder()
.setId(ThingId.of(thingId))
Expand All @@ -316,37 +320,40 @@ public CompletableFuture<Thing> create(final ThingId thingId, final Policy initi
}

@Override
public CompletableFuture<Thing> create(final JsonObject jsonObject, final JsonObject initialPolicy,
public CompletableFuture<Thing> create(final JsonObject thing, final JsonObject initialPolicy,
final Option<?>... options) {
argumentNotNull(jsonObject);
argumentNotNull(initialPolicy);
argumentNotNull(thing, ARGUMENT_THING);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

final Thing thing = ThingsModelFactory.newThing(jsonObject);

return processCreate(thing, initialPolicy, options);
return processCreate(ThingsModelFactory.newThing(thing), initialPolicy, options);
}

@Override
public CompletableFuture<Thing> create(final JsonObject jsonObject, final Policy initialPolicy,
public CompletableFuture<Thing> create(final JsonObject thing, final Policy initialPolicy,
final Option<?>... options) {
argumentNotNull(jsonObject);
argumentNotNull(initialPolicy);

final Thing thing = ThingsModelFactory.newThing(jsonObject);
argumentNotNull(thing, ARGUMENT_THING);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

return processCreate(thing, initialPolicy.toJson(), options);
return processCreate(ThingsModelFactory.newThing(thing), initialPolicy.toJson(), options);
}

@Override
public CompletableFuture<Thing> create(final Thing thing, final JsonObject initialPolicy,
final Option<?>... options) {
argumentNotNull(thing, ARGUMENT_THING);
assertThatThingHasId(thing);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

return processCreate(thing, initialPolicy, options);
}


@Override
public CompletableFuture<Thing> create(final Thing thing, final Policy initialPolicy,
final Option<?>... options) {
argumentNotNull(thing, ARGUMENT_THING);
assertThatThingHasId(thing);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

return processCreate(thing, initialPolicy.toJson(), options);
}

Expand All @@ -363,6 +370,8 @@ private CompletableFuture<Thing> processCreate(final Thing thing, @Nullable fina

@Override
public CompletableFuture<Optional<Thing>> put(final Thing thing, final Option<?>... options) {
argumentNotNull(thing, ARGUMENT_THING);
assertThatThingHasId(thing);
return processPut(thing, null, options);
}

Expand All @@ -377,32 +386,40 @@ public CompletableFuture<Optional<Thing>> put(final JsonObject jsonObject, final
}

@Override
public CompletableFuture<Optional<Thing>> put(final JsonObject jsonObject, final JsonObject initialPolicy,
public CompletableFuture<Optional<Thing>> put(final JsonObject thing, final JsonObject initialPolicy,
final Option<?>... options) {
argumentNotNull(jsonObject);
argumentNotNull(thing, ARGUMENT_THING);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

final Thing thing = ThingsModelFactory.newThing(jsonObject);
return processPut(thing, initialPolicy, options);
return processPut(ThingsModelFactory.newThing(thing), initialPolicy, options);
}

@Override
public CompletableFuture<Optional<Thing>> put(final JsonObject jsonObject, final Policy initialPolicy,
public CompletableFuture<Optional<Thing>> put(final JsonObject thing, final Policy initialPolicy,
final Option<?>... options) {
argumentNotNull(jsonObject);
argumentNotNull(thing, ARGUMENT_THING);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

final Thing thing = ThingsModelFactory.newThing(jsonObject);
return processPut(thing, initialPolicy.toJson(), options);
return processPut(ThingsModelFactory.newThing(thing), initialPolicy.toJson(), options);
}

@Override
public CompletableFuture<Optional<Thing>> put(final Thing thing, final JsonObject initialPolicy,
final Option<?>... options) {
argumentNotNull(thing, ARGUMENT_THING);
assertThatThingHasId(thing);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

return processPut(thing, initialPolicy, options);
}

@Override
public CompletableFuture<Optional<Thing>> put(final Thing thing, final Policy initialPolicy,
final Option<?>... options) {
argumentNotNull(thing, ARGUMENT_THING);
assertThatThingHasId(thing);
argumentNotNull(initialPolicy, ARGUMENT_INITIAL_POLICY);

return processPut(thing, initialPolicy.toJson(), options);
}

Expand Down
Loading

0 comments on commit 9c1743f

Please sign in to comment.