Skip to content

Commit

Permalink
Merge pull request #52 from bosch-io/bugfix/append-ws-path
Browse files Browse the repository at this point in the history
Append ws path only if needed
  • Loading branch information
thjaeckle authored Apr 14, 2020
2 parents 33269b0 + 892c593 commit 21efb53
Showing 1 changed file with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
*/
package org.eclipse.ditto.client.configuration;

import static org.eclipse.ditto.model.base.common.ConditionChecker.checkArgument;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;

import javax.annotation.Nullable;
import java.net.URI;
import java.text.MessageFormat;
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;

import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkArgument;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;

/**
* Provides Ditto WebSocket messaging specific configuration.
Expand Down Expand Up @@ -81,6 +82,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/(1|2)/?";

private JsonSchemaVersion jsonSchemaVersion = JsonSchemaVersion.LATEST;
private URI endpointUri;
Expand Down Expand Up @@ -128,15 +130,40 @@ 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(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, path.length());
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

0 comments on commit 21efb53

Please sign in to comment.