|
21 | 21 |
|
22 | 22 | package org.eclipse.paho.mqttv5.client; |
23 | 23 |
|
24 | | -import java.lang.reflect.Field; |
25 | | -import java.net.URI; |
26 | | -import java.net.URISyntaxException; |
27 | 24 | import java.util.Hashtable; |
28 | 25 | import java.util.Properties; |
29 | 26 | import java.util.Timer; |
|
32 | 29 | import java.util.concurrent.ScheduledExecutorService; |
33 | 30 |
|
34 | 31 | import javax.net.SocketFactory; |
35 | | -import javax.net.ssl.SSLSocketFactory; |
36 | | - |
37 | 32 | import org.eclipse.paho.mqttv5.client.internal.ClientComms; |
38 | 33 | import org.eclipse.paho.mqttv5.client.internal.ConnectActionListener; |
39 | 34 | import org.eclipse.paho.mqttv5.client.internal.DisconnectedMessageBuffer; |
40 | 35 | import org.eclipse.paho.mqttv5.client.internal.MqttSession; |
41 | 36 | 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; |
44 | 38 | import org.eclipse.paho.mqttv5.client.logging.Logger; |
45 | 39 | import org.eclipse.paho.mqttv5.client.logging.LoggerFactory; |
46 | 40 | import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence; |
47 | 41 | import org.eclipse.paho.mqttv5.client.persist.MqttDefaultFilePersistence; |
48 | | -import org.eclipse.paho.mqttv5.client.security.SSLSocketFactoryFactory; |
49 | 42 | 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; |
52 | 43 | import org.eclipse.paho.mqttv5.common.ExceptionHelper; |
53 | 44 | import org.eclipse.paho.mqttv5.common.MqttException; |
54 | 45 | import org.eclipse.paho.mqttv5.common.MqttMessage; |
@@ -579,7 +570,7 @@ public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence |
579 | 570 |
|
580 | 571 |
|
581 | 572 |
|
582 | | - MqttConnectionOptions.validateURI(serverURI); |
| 573 | + NetworkModuleService.validateURI(serverURI); |
583 | 574 |
|
584 | 575 | this.serverURI = serverURI; |
585 | 576 | this.mqttSession.setClientId(clientId); |
@@ -674,127 +665,7 @@ private NetworkModule createNetworkModule(String address, MqttConnectionOptions |
674 | 665 | // @TRACE 115=URI={0} |
675 | 666 | log.fine(CLASS_NAME, methodName, "115", new Object[] { address }); |
676 | 667 |
|
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()); |
798 | 669 | return netModule; |
799 | 670 | } |
800 | 671 |
|
|
0 commit comments