Skip to content

Commit e01002e

Browse files
author
Maik Scheibler
committed
ported the NetworkModule SPI implementation to Client V5
Signed-off-by: Maik Scheibler <eclipse@scheibler-family.de>
1 parent 5d3d922 commit e01002e

11 files changed

Lines changed: 624 additions & 178 deletions

File tree

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

Lines changed: 3 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121

2222
package org.eclipse.paho.mqttv5.client;
2323

24-
import java.lang.reflect.Field;
25-
import java.net.URI;
26-
import java.net.URISyntaxException;
2724
import java.util.Hashtable;
2825
import java.util.Properties;
2926
import java.util.Timer;
@@ -32,23 +29,17 @@
3229
import java.util.concurrent.ScheduledExecutorService;
3330

3431
import javax.net.SocketFactory;
35-
import javax.net.ssl.SSLSocketFactory;
36-
3732
import org.eclipse.paho.mqttv5.client.internal.ClientComms;
3833
import org.eclipse.paho.mqttv5.client.internal.ConnectActionListener;
3934
import org.eclipse.paho.mqttv5.client.internal.DisconnectedMessageBuffer;
4035
import org.eclipse.paho.mqttv5.client.internal.MqttSession;
4136
import org.eclipse.paho.mqttv5.client.internal.NetworkModule;
42-
import org.eclipse.paho.mqttv5.client.internal.SSLNetworkModule;
43-
import org.eclipse.paho.mqttv5.client.internal.TCPNetworkModule;
37+
import org.eclipse.paho.mqttv5.client.internal.NetworkModuleService;
4438
import org.eclipse.paho.mqttv5.client.logging.Logger;
4539
import org.eclipse.paho.mqttv5.client.logging.LoggerFactory;
4640
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
4741
import org.eclipse.paho.mqttv5.client.persist.MqttDefaultFilePersistence;
48-
import org.eclipse.paho.mqttv5.client.security.SSLSocketFactoryFactory;
4942
import org.eclipse.paho.mqttv5.client.util.Debug;
50-
import org.eclipse.paho.mqttv5.client.websocket.WebSocketNetworkModule;
51-
import org.eclipse.paho.mqttv5.client.websocket.WebSocketSecureNetworkModule;
5243
import org.eclipse.paho.mqttv5.common.ExceptionHelper;
5344
import org.eclipse.paho.mqttv5.common.MqttException;
5445
import org.eclipse.paho.mqttv5.common.MqttMessage;
@@ -579,7 +570,7 @@ public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence
579570

580571

581572

582-
MqttConnectionOptions.validateURI(serverURI);
573+
NetworkModuleService.validateURI(serverURI);
583574

584575
this.serverURI = serverURI;
585576
this.mqttSession.setClientId(clientId);
@@ -674,127 +665,7 @@ private NetworkModule createNetworkModule(String address, MqttConnectionOptions
674665
// @TRACE 115=URI={0}
675666
log.fine(CLASS_NAME, methodName, "115", new Object[] { address });
676667

677-
NetworkModule netModule;
678-
SocketFactory factory = options.getSocketFactory();
679-
680-
MqttConnectionOptions.UriType serverURIType = MqttConnectionOptions.validateURI(address);
681-
682-
URI uri;
683-
try {
684-
uri = new URI(address);
685-
// If the returned uri contains no host and the address contains underscores,
686-
// then it's likely that Java did not parse the URI
687-
if (uri.getHost() == null && address.contains("_")) {
688-
try {
689-
final Field hostField = URI.class.getDeclaredField("host");
690-
hostField.setAccessible(true);
691-
// Get everything after the scheme://
692-
String shortAddress = address.substring(uri.getScheme().length() + 3);
693-
hostField.set(uri, getHostName(shortAddress));
694-
695-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException
696-
| IllegalAccessException e) {
697-
throw ExceptionHelper.createMqttException(e.getCause());
698-
}
699-
700-
}
701-
} catch (URISyntaxException e) {
702-
throw new IllegalArgumentException("Malformed URI: " + address + ", " + e.getMessage());
703-
}
704-
705-
String host = uri.getHost();
706-
int port = uri.getPort(); // -1 if not defined
707-
708-
switch (serverURIType) {
709-
case TCP:
710-
if (port == -1) {
711-
port = 1883;
712-
}
713-
if (factory == null) {
714-
factory = SocketFactory.getDefault();
715-
} else if (factory instanceof SSLSocketFactory) {
716-
throw ExceptionHelper.createMqttException(MqttClientException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
717-
}
718-
netModule = new TCPNetworkModule(factory, host, port, this.mqttSession.getClientId());
719-
((TCPNetworkModule) netModule).setConnectTimeout(options.getConnectionTimeout());
720-
break;
721-
case SSL:
722-
if (port == -1) {
723-
port = 8883;
724-
}
725-
SSLSocketFactoryFactory factoryFactory = null;
726-
if (factory == null) {
727-
// try {
728-
factoryFactory = new SSLSocketFactoryFactory();
729-
Properties sslClientProps = options.getSSLProperties();
730-
if (null != sslClientProps)
731-
factoryFactory.initialize(sslClientProps, null);
732-
factory = factoryFactory.createSocketFactory(null);
733-
// }
734-
// catch (MqttDirectException ex) {
735-
// throw ExceptionHelper.createMqttException(ex.getCause());
736-
// }
737-
} else if ((factory instanceof SSLSocketFactory) == false) {
738-
throw ExceptionHelper.createMqttException(MqttClientException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
739-
}
740-
741-
// Create the network module...
742-
netModule = new SSLNetworkModule((SSLSocketFactory) factory, host, port, this.mqttSession.getClientId());
743-
((SSLNetworkModule) netModule).setSSLhandshakeTimeout(options.getConnectionTimeout());
744-
((SSLNetworkModule) netModule).setSSLHostnameVerifier(options.getSSLHostnameVerifier());
745-
((SSLNetworkModule)netModule).setHttpsHostnameVerificationEnabled(options.isHttpsHostnameVerificationEnabled());
746-
// Ciphers suites need to be set, if they are available
747-
if (factoryFactory != null) {
748-
String[] enabledCiphers = factoryFactory.getEnabledCipherSuites(null);
749-
if (enabledCiphers != null) {
750-
((SSLNetworkModule) netModule).setEnabledCiphers(enabledCiphers);
751-
}
752-
}
753-
break;
754-
case WS:
755-
if (port == -1) {
756-
port = 80;
757-
}
758-
if (factory == null) {
759-
factory = SocketFactory.getDefault();
760-
} else if (factory instanceof SSLSocketFactory) {
761-
throw ExceptionHelper.createMqttException(MqttClientException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
762-
}
763-
netModule = new WebSocketNetworkModule(factory, address, host, port, this.mqttSession.getClientId());
764-
((WebSocketNetworkModule) netModule).setConnectTimeout(options.getConnectionTimeout());
765-
break;
766-
case WSS:
767-
if (port == -1) {
768-
port = 443;
769-
}
770-
SSLSocketFactoryFactory wSSFactoryFactory = null;
771-
if (factory == null) {
772-
wSSFactoryFactory = new SSLSocketFactoryFactory();
773-
Properties sslClientProps = options.getSSLProperties();
774-
if (null != sslClientProps)
775-
wSSFactoryFactory.initialize(sslClientProps, null);
776-
factory = wSSFactoryFactory.createSocketFactory(null);
777-
778-
} else if ((factory instanceof SSLSocketFactory) == false) {
779-
throw ExceptionHelper.createMqttException(MqttClientException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
780-
}
781-
782-
// Create the network module...
783-
netModule = new WebSocketSecureNetworkModule((SSLSocketFactory) factory, address, host, port, this.mqttSession.getClientId());
784-
((WebSocketSecureNetworkModule) netModule).setSSLhandshakeTimeout(options.getConnectionTimeout());
785-
// Ciphers suites need to be set, if they are available
786-
if (wSSFactoryFactory != null) {
787-
String[] enabledCiphers = wSSFactoryFactory.getEnabledCipherSuites(null);
788-
if (enabledCiphers != null) {
789-
((SSLNetworkModule) netModule).setEnabledCiphers(enabledCiphers);
790-
}
791-
}
792-
break;
793-
default:
794-
// This shouldn't happen, as long as validateURI() has been called.
795-
log.fine(CLASS_NAME, methodName, "119", new Object[] { address });
796-
netModule = null;
797-
}
668+
NetworkModule netModule = NetworkModuleService.createInstance(address, options, mqttSession.getClientId());
798669
return netModule;
799670
}
800671

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/MqttConnectionOptions.java

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
*/
1919
package org.eclipse.paho.mqttv5.client;
2020

21-
import java.net.URI;
22-
import java.net.URISyntaxException;
2321
import java.util.List;
2422
import java.util.Properties;
2523

2624
import javax.net.SocketFactory;
2725
import javax.net.ssl.HostnameVerifier;
28-
26+
import org.eclipse.paho.mqttv5.client.internal.NetworkModuleService;
2927
import org.eclipse.paho.mqttv5.client.util.Debug;
3028
import org.eclipse.paho.mqttv5.common.MqttMessage;
3129
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
@@ -52,10 +50,6 @@
5250
*/
5351
public class MqttConnectionOptions {
5452

55-
enum UriType {
56-
TCP, SSL, WS, WSS
57-
}
58-
5953
private static final String CLIENT_ID_PREFIX = "paho";
6054

6155
// Connection Behaviour Properties
@@ -396,14 +390,13 @@ public String[] getServerURIs() {
396390
* </li>
397391
* </ol>
398392
*
399-
* @param array
400-
* of serverURIs
393+
* @param serverURIs to be used by the client
401394
*/
402-
public void setServerURIs(String[] array) {
403-
for (int i = 0; i < array.length; i++) {
404-
validateURI(array[i]);
395+
public void setServerURIs(String[] serverURIs) {
396+
for (String serverURI:serverURIs) {
397+
NetworkModuleService.validateURI(serverURI);
405398
}
406-
this.serverURIs = array;
399+
this.serverURIs = serverURIs.clone();
407400
}
408401

409402
/**
@@ -718,39 +711,6 @@ public void setAuthData(byte[] authData) {
718711
this.authData = authData;
719712
}
720713

721-
/**
722-
* Validate a URI
723-
*
724-
* @param srvURI
725-
* The Server URI
726-
* @return the URI type
727-
*/
728-
public static UriType validateURI(String srvURI) {
729-
try {
730-
URI vURI = new URI(srvURI);
731-
if ("ws".equals(vURI.getScheme())) {
732-
return UriType.WS;
733-
} else if ("wss".equals(vURI.getScheme())) {
734-
return UriType.WSS;
735-
}
736-
737-
if ((vURI.getPath() == null) || vURI.getPath().isEmpty()) {
738-
// No op path must be empty
739-
} else {
740-
throw new IllegalArgumentException(srvURI);
741-
}
742-
if ("tcp".equals(vURI.getScheme())) {
743-
return UriType.TCP;
744-
} else if ("ssl".equals(vURI.getScheme())) {
745-
return UriType.SSL;
746-
} else {
747-
throw new IllegalArgumentException(srvURI);
748-
}
749-
} catch (URISyntaxException ex) {
750-
throw new IllegalArgumentException(srvURI);
751-
}
752-
}
753-
754714
/**
755715
* Returns the socket factory that will be used when connecting, or
756716
* <code>null</code> if one has not been set.

0 commit comments

Comments
 (0)