Skip to content

Commit e5e7e93

Browse files
authored
Fixing paho java client to minimum JRE 1.4.2+ source (Issue #218) (#273)
* Fixing mqttv3 source to be compatible with Java 1.4.2+ Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent d2a4fc1 commit e5e7e93

5 files changed

Lines changed: 36 additions & 15 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,9 @@ public void messageArrivedComplete(int messageId, int qos) throws MqttException
957957
*/
958958
public static String generateClientId() {
959959
//length of nanoTime = 15, so total length = 19 < 65535(defined in spec)
960-
return CLIENT_ID_PREFIX + System.nanoTime();
960+
//return CLIENT_ID_PREFIX + System.nanoTime(); // Cannot use for Java 1.4.2
961+
return CLIENT_ID_PREFIX + (System.currentTimeMillis() * 1000000);
962+
961963
}
962964

963965
/* (non-Javadoc)
@@ -1097,7 +1099,8 @@ private void startReconnectCycle(){
10971099
String methodName = "startReconnectCycle";
10981100
//@Trace 503=Start reconnect timer for client: {0}, delay: {1}
10991101
log.fine(CLASS_NAME, methodName, "503", new Object[]{this.clientId, new Long(reconnectDelay)});
1100-
reconnectTimer = new Timer("MQTT Reconnect: " + clientId);
1102+
//reconnectTimer = new Timer("MQTT Reconnect: " + clientId); // Cannot use for Java 1.4.2
1103+
reconnectTimer = new Timer();
11011104
reconnectTimer.schedule(new ReconnectTask(), reconnectDelay);
11021105
}
11031106

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public static void validate(String topicString, boolean wildcardAllowed)
146146
try {
147147
topicLen = topicString.getBytes("UTF-8").length;
148148
} catch (UnsupportedEncodingException e) {
149-
throw new IllegalStateException(e);
149+
// throw new IllegalStateException(e); // Cannot use for Java 1.4.2
150+
throw new IllegalStateException(e.getMessage());
150151
}
151152

152153
// Spec: length check
@@ -155,8 +156,10 @@ public static void validate(String topicString, boolean wildcardAllowed)
155156
// - Topic Names and Topic Filters are UTF-8 encoded strings, they MUST
156157
// NOT encode to more than 65535 bytes
157158
if (topicLen < MIN_TOPIC_LEN || topicLen > MAX_TOPIC_LEN) {
158-
throw new IllegalArgumentException(String.format("Invalid topic length, should be in range[%d, %d]!",
159-
new Object[] { new Integer(MIN_TOPIC_LEN), new Integer(MAX_TOPIC_LEN) }));
159+
//throw new IllegalArgumentException(String.format("Invalid topic length, should be in range[%d, %d]!", // Cannot use for Java 1.4.2
160+
// new Object[] { new Integer(MIN_TOPIC_LEN), new Integer(MAX_TOPIC_LEN) }));
161+
String errorMessage = "Invalid topic length, should be in range["+ MIN_TOPIC_LEN + ", " + MAX_TOPIC_LEN + "]!";
162+
throw new IllegalArgumentException(errorMessage);
160163
}
161164

162165
// *******************************************************************************
@@ -177,7 +180,8 @@ public static void validate(String topicString, boolean wildcardAllowed)
177180
// - The multi-level wildcard must be the last character used within
178181
// the topic tree
179182
if (Strings.countMatches(topicString, MULTI_LEVEL_WILDCARD) > 1
180-
|| (topicString.contains(MULTI_LEVEL_WILDCARD) && !topicString
183+
//|| (topicString.contains(MULTI_LEVEL_WILDCARD) && !topicString// Cannot use for Java 1.4.2
184+
|| ((topicString.indexOf(MULTI_LEVEL_WILDCARD) != -1) && !topicString
181185
.endsWith(MULTI_LEVEL_WILDCARD_PATTERN))) {
182186
throw new IllegalArgumentException(
183187
"Invalid usage of multi-level wildcard in topic string: "
@@ -219,9 +223,11 @@ private static void validateSingleLevelWildcard(String topicString) {
219223
if (chars[i] == singleLevelWildcardChar) {
220224
// prev and next can be only '/' or none
221225
if (prev != topicLevelSeparatorChar && prev != NUL || next != topicLevelSeparatorChar && next != NUL) {
222-
throw new IllegalArgumentException(String.format(
223-
"Invalid usage of single-level wildcard in topic string '%s'!",
224-
new Object[] { topicString }));
226+
// throw new IllegalArgumentException(String.format( // Cannot use for Java 1.4.2
227+
// "Invalid usage of single-level wildcard in topic string '%s'!",
228+
// new Object[] { topicString }));
229+
String errorMessage = "Invalid usage of single-level wildcard in topic string '" + topicString + "'!";
230+
throw new IllegalArgumentException(errorMessage);
225231
}
226232
}
227233
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void start() {
5050
//@Trace 659=start timer for client:{0}
5151
log.fine(CLASS_NAME, methodName, "659", new Object[]{clientid});
5252

53-
timer = new Timer("MQTT Ping: " + clientid);
53+
//timer = new Timer("MQTT Ping: " + clientid); // Cannot use for Java 1.4.2
54+
timer = new Timer();
5455
//Check ping after first keep alive interval.
5556
timer.schedule(new PingTask(), comms.getKeepAlive());
5657
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketHandshake.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ private void sendHandshakeRequest(String key) throws IOException{
8484
try {
8585
String path = "/mqtt";
8686
URI srvUri = new URI(uri);
87-
if (srvUri.getRawPath() != null && !srvUri.getRawPath().isEmpty()) {
87+
//if (srvUri.getRawPath() != null && !srvUri.getRawPath().isEmpty()) { // Cannot use for Java 1.4.2
88+
if (srvUri.getRawPath() != null && srvUri.getRawPath().length() != 0) {
8889
path = srvUri.getRawPath();
89-
if (srvUri.getRawQuery() != null && !srvUri.getRawQuery().isEmpty()) {
90+
//if (srvUri.getRawQuery() != null && !srvUri.getRawQuery().isEmpty()) { // Cannot use for Java 1.4.2
91+
if (srvUri.getRawQuery() != null && srvUri.getRawQuery().length() != 0) {
9092
path += "?" + srvUri.getRawQuery();
9193
}
9294
}
@@ -114,7 +116,8 @@ private void sendHandshakeRequest(String key) throws IOException{
114116
pw.print(LINE_SEPARATOR);
115117
pw.flush();
116118
} catch (URISyntaxException e) {
117-
throw new IllegalStateException(e);
119+
// throw new IllegalStateException(e.getMessage()); // Cannot use for Java 1.4.2
120+
throw new IllegalStateException(e.getMessage());
118121
}
119122
}
120123

@@ -142,7 +145,8 @@ private void receiveHandshakeResponse(String key) throws IOException {
142145
}
143146

144147
String upgradeHeader = (String) headerMap.get(HTTP_HEADER_UPGRADE);
145-
if(!upgradeHeader.toLowerCase().contains(HTTP_HEADER_UPGRADE_WEBSOCKET)){
148+
//if(!upgradeHeader.toLowerCase().contains(HTTP_HEADER_UPGRADE_WEBSOCKET)){ // Cannot use for Java 1.4.2
149+
if(upgradeHeader.toLowerCase().indexOf(HTTP_HEADER_UPGRADE_WEBSOCKET) == -1){
146150
throw new IOException("WebSocket Response header: Incorrect upgrade.");
147151
}
148152

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/util/Strings.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public static boolean containsAny(CharSequence cs, char[] searchChars) {
7878
char ch = cs.charAt(i);
7979
for (int j = 0; j < searchLength; j++) {
8080
if (searchChars[j] == ch) {
81-
if (Character.isHighSurrogate(ch)) {
81+
//if (Character.isHighSurrogate(ch)) { // Cannot use for Java 1.4.2
82+
if(isHighSurrogate(ch)) {
8283
if (j == searchLast) {
8384
// missing low surrogate, fine, like String.indexOf(String)
8485
return true;
@@ -96,6 +97,12 @@ public static boolean containsAny(CharSequence cs, char[] searchChars) {
9697
}
9798
return false;
9899
}
100+
101+
private static boolean isHighSurrogate(char ch){
102+
char MAX = '\uDBFF';
103+
char MIN = '\uD800';
104+
return ch >= MIN && ch < (MAX +1);
105+
}
99106

100107
/**
101108
* Checks if a CharSequence is empty ("") or null.

0 commit comments

Comments
 (0)