|
12 | 12 | */
|
13 | 13 | package org.eclipse.ditto.client.configuration;
|
14 | 14 |
|
15 |
| -import static org.eclipse.ditto.model.base.common.ConditionChecker.checkArgument; |
16 |
| -import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; |
| 15 | +import org.eclipse.ditto.model.base.json.JsonSchemaVersion; |
17 | 16 |
|
| 17 | +import javax.annotation.Nullable; |
18 | 18 | import java.net.URI;
|
19 | 19 | import java.text.MessageFormat;
|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.List;
|
22 | 22 | import java.util.Optional;
|
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
23 | 25 |
|
24 |
| -import javax.annotation.Nullable; |
25 |
| - |
26 |
| -import org.eclipse.ditto.model.base.json.JsonSchemaVersion; |
| 26 | +import static org.eclipse.ditto.model.base.common.ConditionChecker.checkArgument; |
| 27 | +import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; |
27 | 28 |
|
28 | 29 | /**
|
29 | 30 | * Provides Ditto WebSocket messaging specific configuration.
|
@@ -81,6 +82,7 @@ private static final class WebSocketMessagingConfigurationBuilder implements Mes
|
81 | 82 |
|
82 | 83 | private static final List<String> ALLOWED_URI_SCHEME = Arrays.asList("wss", "ws");
|
83 | 84 | private static final String WS_PATH = "/ws/";
|
| 85 | + private static final String WS_PATH_REGEX = "/ws/(1|2)/?"; |
84 | 86 |
|
85 | 87 | private JsonSchemaVersion jsonSchemaVersion = JsonSchemaVersion.LATEST;
|
86 | 88 | private URI endpointUri;
|
@@ -128,15 +130,40 @@ public MessagingConfiguration.Builder trustStoreConfiguration(
|
128 | 130 |
|
129 | 131 | @Override
|
130 | 132 | public MessagingConfiguration build() {
|
131 |
| - final URI wsEndpointUri = appendWsPath(this.endpointUri, jsonSchemaVersion); |
| 133 | + final URI wsEndpointUri= appendWsPathIfNecessary(this.endpointUri, jsonSchemaVersion); |
132 | 134 | return new WebSocketMessagingConfiguration(jsonSchemaVersion, wsEndpointUri, reconnectEnabled,
|
133 | 135 | proxyConfiguration, trustStoreConfiguration);
|
134 | 136 | }
|
135 | 137 |
|
136 |
| - private static URI appendWsPath(final URI baseUri, final JsonSchemaVersion schemaVersion) { |
137 |
| - final String pathWithoutTrailingSlashes = baseUri.getPath().replaceFirst("/+$", ""); |
138 |
| - final String newPath = pathWithoutTrailingSlashes + WS_PATH + schemaVersion.toString(); |
139 |
| - return baseUri.resolve(newPath); |
| 138 | + private static URI appendWsPathIfNecessary(final URI baseUri, final JsonSchemaVersion schemaVersion) { |
| 139 | + if (needToAppendWsPath(baseUri)) { |
| 140 | + final String pathWithoutTrailingSlashes = removeTrailingSlashFromPath(baseUri.getPath()); |
| 141 | + final String newPath = pathWithoutTrailingSlashes + WS_PATH + schemaVersion.toString(); |
| 142 | + return baseUri.resolve(newPath); |
| 143 | + } else { |
| 144 | + checkIfBaseUriAndSchemaVersionMatch(baseUri, schemaVersion); |
| 145 | + return baseUri; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static boolean needToAppendWsPath(final URI baseUri) { |
| 150 | + final Pattern pattern = Pattern.compile(WS_PATH_REGEX); |
| 151 | + final Matcher matcher = pattern.matcher(baseUri.toString()); |
| 152 | + return !matcher.find(); |
| 153 | + } |
| 154 | + |
| 155 | + private static void checkIfBaseUriAndSchemaVersionMatch(final URI baseUri, final JsonSchemaVersion schemaVersion) { |
| 156 | + final String path = removeTrailingSlashFromPath(baseUri.getPath()); |
| 157 | + final String apiVersion = path.substring(path.length() - 1, path.length()); |
| 158 | + if (!schemaVersion.toString().equals(apiVersion)) { |
| 159 | + throw new IllegalArgumentException("The jsonSchemaVersion and apiVersion of the endpoint do not match. " + |
| 160 | + "Either remove the ws path from the endpoint or " + |
| 161 | + "use the same jsonSchemaVersion as in the ws path of the endpoint."); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static String removeTrailingSlashFromPath(final String path) { |
| 166 | + return path.replaceFirst("/+$", ""); |
140 | 167 | }
|
141 | 168 |
|
142 | 169 | }
|
|
0 commit comments