Skip to content

Commit d8b0547

Browse files
aploesejpwsutton
authored andcommitted
Exposing a useful exception message when an invalid URI is provided.
Signed-off-by: Arne Plöse <aploese@gmx.de>
1 parent 1bd4932 commit d8b0547

2 files changed

Lines changed: 86 additions & 27 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.eclipse.paho.client.mqttv3.test;
2+
3+
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
4+
import static org.junit.Assert.*;
5+
import org.junit.Test;
6+
7+
public class MqttConnectOptionsTest {
8+
9+
@Test
10+
public void testValidateURI() {
11+
try {
12+
MqttConnectOptions.validateURI("");
13+
fail("Must fail: Empty URI");
14+
} catch (IllegalArgumentException e) {
15+
assertEquals("Unknown scheme \"null\" of URI \"\"", e.getMessage());
16+
}
17+
18+
try {
19+
MqttConnectOptions.validateURI("udp://localhost:1883");
20+
fail("Must fail: Unknow scheme");
21+
} catch (IllegalArgumentException e) {
22+
assertEquals("Unknown scheme \"udp\" of URI \"udp://localhost:1883\"", e.getMessage());
23+
}
24+
25+
MqttConnectOptions.validateURI("tcp://localhost:1883");
26+
27+
try {
28+
MqttConnectOptions.validateURI("tcp://localhost:1883/mqtt");
29+
fail("Must fail: URI path must be empty");
30+
} catch (IllegalArgumentException e) {
31+
assertEquals("URI path must be empty \"tcp://localhost:1883/mqtt\"", e.getMessage());
32+
}
33+
34+
MqttConnectOptions.validateURI("tcp://localhost");
35+
36+
try {
37+
MqttConnectOptions.validateURI("localhost:1883");
38+
fail("Must fail: Unknow scheme");
39+
} catch (IllegalArgumentException e) {
40+
assertEquals("Unknown scheme \"localhost\" of URI \"localhost:1883\"", e.getMessage());
41+
}
42+
43+
try {
44+
MqttConnectOptions.validateURI("localhost");
45+
fail("Must fail: URI path must be empty");
46+
} catch (IllegalArgumentException e) {
47+
assertEquals("URI path must be empty \"localhost\"", e.getMessage());
48+
}
49+
50+
try {
51+
MqttConnectOptions.validateURI("tcp://");
52+
fail("Must fail: Can't parse string to URI");
53+
} catch (IllegalArgumentException e) {
54+
assertEquals("Can't parse string to URI \"tcp://\"", e.getMessage());
55+
}
56+
}
57+
58+
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -525,35 +525,36 @@ public void setServerURIs(String[] array) {
525525
* @return the URI type
526526
*/
527527
public static int validateURI(String srvURI) {
528+
URI vURI;
528529
try {
529-
URI vURI = new URI(srvURI);
530-
if ("ws".equals(vURI.getScheme())){
531-
return URI_TYPE_WS;
532-
}
533-
else if ("wss".equals(vURI.getScheme())) {
534-
return URI_TYPE_WSS;
535-
}
536-
537-
if ((vURI.getPath() == null) || vURI.getPath().isEmpty()) {
538-
// No op path must be empty
539-
}
540-
else {
541-
throw new IllegalArgumentException(srvURI);
542-
}
543-
if ("tcp".equals(vURI.getScheme())) {
544-
return URI_TYPE_TCP;
545-
}
546-
else if ("ssl".equals(vURI.getScheme())) {
547-
return URI_TYPE_SSL;
548-
}
549-
else if ("local".equals(vURI.getScheme())) {
550-
return URI_TYPE_LOCAL;
551-
}
552-
else {
553-
throw new IllegalArgumentException(srvURI);
554-
}
530+
vURI = new URI(srvURI);
555531
} catch (URISyntaxException ex) {
556-
throw new IllegalArgumentException(srvURI);
532+
throw new IllegalArgumentException("Can't parse string to URI \"" + srvURI + "\"", ex);
533+
}
534+
535+
if ("ws".equals(vURI.getScheme())){
536+
return URI_TYPE_WS;
537+
}
538+
else if ("wss".equals(vURI.getScheme())) {
539+
return URI_TYPE_WSS;
540+
}
541+
if ((vURI.getPath() == null) || vURI.getPath().isEmpty()) {
542+
// No op path must be empty
543+
}
544+
else {
545+
throw new IllegalArgumentException("URI path must be empty \"" + srvURI + "\"");
546+
}
547+
if ("tcp".equals(vURI.getScheme())) {
548+
return URI_TYPE_TCP;
549+
}
550+
else if ("ssl".equals(vURI.getScheme())) {
551+
return URI_TYPE_SSL;
552+
}
553+
else if ("local".equals(vURI.getScheme())) {
554+
return URI_TYPE_LOCAL;
555+
}
556+
else {
557+
throw new IllegalArgumentException("Unknown scheme \"" + vURI.getScheme() + "\" of URI \"" + srvURI + "\"");
557558
}
558559
}
559560

0 commit comments

Comments
 (0)