Skip to content

Commit 8d208a0

Browse files
committed
Refactor URL validation in client-demo
1 parent 9c97f50 commit 8d208a0

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/LeshanClientDemoCLI.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import java.io.File;
1919
import java.net.InetAddress;
2020
import java.net.UnknownHostException;
21+
import java.util.Arrays;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
import java.util.stream.Stream;
2326

2427
import org.eclipse.californium.core.coap.CoAP;
2528
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;
@@ -31,6 +34,7 @@
3134
import org.eclipse.leshan.core.demo.cli.converters.InetAddressConverter;
3235
import org.eclipse.leshan.core.demo.cli.converters.ResourcePathConverter;
3336
import org.eclipse.leshan.core.demo.cli.converters.StrictlyPositiveIntegerConverter;
37+
import org.eclipse.leshan.core.endpoint.Protocol;
3438
import org.eclipse.leshan.core.node.LwM2mPath;
3539
import org.eclipse.leshan.core.util.StringUtils;
3640

@@ -316,24 +320,33 @@ public void run() {
316320
// extract scheme
317321
int indexOf = main.url.indexOf("://");
318322
String scheme = main.url.substring(0, indexOf);
319-
// we support only coap and coaps
320-
if (!"coap".equals(scheme) && !"coaps".equals(scheme) && !"coap+tcp".equals(scheme)) {
321-
throw new MultiParameterException(spec.commandLine(), String.format(
322-
"Invalid URL %s : unknown scheme '%s', we support only 'coap' or 'coaps' or 'coap+tcp' for now",
323-
main.url, scheme), "-u");
323+
// check URI scheme is supported
324+
List<String> supportedUnsecuredProtocol = Arrays.asList(Protocol.COAP, Protocol.COAP_TCP) //
325+
.stream().map(Protocol::getUriScheme).collect(Collectors.toList());
326+
List<String> supportedTlsBasedProtocol = Arrays.asList(Protocol.COAPS) //
327+
.stream().map(Protocol::getUriScheme).collect(Collectors.toList());
328+
List<String> allSupportedProtocol = Stream
329+
.concat(supportedUnsecuredProtocol.stream(), supportedTlsBasedProtocol.stream())
330+
.collect(Collectors.toList());
331+
332+
if (!allSupportedProtocol.contains(scheme)) {
333+
throw new MultiParameterException(spec.commandLine(),
334+
String.format("Invalid URL %s : unknown scheme '%s', we support only %s for now", main.url, scheme,
335+
String.join(" or ", allSupportedProtocol)),
336+
"-u");
324337
}
325338
// check scheme matches configuration
326339
if (identity.hasIdentity()) {
327-
if (!scheme.equals("coaps")) {
340+
if (!supportedTlsBasedProtocol.contains(scheme)) {
328341
throw new MultiParameterException(spec.commandLine(), String.format(
329-
"Invalid URL %s : '%s' scheme must be used without PSK, RPK or x509 option. Do you mean 'coaps' ? ",
330-
main.url, scheme), "-u");
342+
"Invalid URL %s : '%s' scheme must be used without PSK, RPK or x509 option. Do you mean %s ? ",
343+
main.url, scheme, String.join(" or ", supportedTlsBasedProtocol)), "-u");
331344
}
332345
} else {
333-
if (!scheme.equals("coap") && !scheme.equals("coap+tcp")) {
346+
if (!supportedUnsecuredProtocol.contains(scheme)) {
334347
throw new MultiParameterException(spec.commandLine(), String.format(
335-
"Invalid URL %s : '%s' scheme must be used with PSK, RPK or x509 option. Do you mean 'coap' ? ",
336-
main.url, scheme), "-u");
348+
"Invalid URL %s : '%s' scheme must be used with PSK, RPK or x509 option. Do you mean %s ? ",
349+
main.url, scheme, String.join(" or ", supportedUnsecuredProtocol)), "-u");
337350
}
338351
}
339352
}

0 commit comments

Comments
 (0)