|
21 | 21 |
|
22 | 22 | package org.eclipse.paho.client.mqttv3; |
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.client.mqttv3.internal.ClientComms; |
38 | 33 | import org.eclipse.paho.client.mqttv3.internal.ConnectActionListener; |
39 | 34 | import org.eclipse.paho.client.mqttv3.internal.DisconnectedMessageBuffer; |
40 | 35 | import org.eclipse.paho.client.mqttv3.internal.ExceptionHelper; |
41 | 36 | import org.eclipse.paho.client.mqttv3.internal.NetworkModule; |
42 | | -import org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule; |
43 | | -import org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule; |
44 | | -import org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory; |
45 | | -import org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketNetworkModule; |
46 | | -import org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule; |
| 37 | +import org.eclipse.paho.client.mqttv3.internal.NetworkModuleService; |
47 | 38 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttDisconnect; |
48 | 39 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttPublish; |
49 | 40 | import org.eclipse.paho.client.mqttv3.internal.wire.MqttSubscribe; |
@@ -460,7 +451,7 @@ public MqttAsyncClient(String serverURI, String clientId, MqttClientPersistence |
460 | 451 | throw new IllegalArgumentException("ClientId longer than 65535 characters"); |
461 | 452 | } |
462 | 453 |
|
463 | | - MqttConnectOptions.validateURI(serverURI); |
| 454 | + NetworkModuleService.validateURI(serverURI); |
464 | 455 |
|
465 | 456 | this.serverURI = serverURI; |
466 | 457 | this.clientId = clientId; |
@@ -547,129 +538,7 @@ private NetworkModule createNetworkModule(String address, MqttConnectOptions opt |
547 | 538 | // @TRACE 115=URI={0} |
548 | 539 | log.fine(CLASS_NAME,methodName, "115", new Object[] {address}); |
549 | 540 |
|
550 | | - NetworkModule netModule; |
551 | | - SocketFactory factory = options.getSocketFactory(); |
552 | | - |
553 | | - int serverURIType = MqttConnectOptions.validateURI(address); |
554 | | - |
555 | | - URI uri; |
556 | | - try { |
557 | | - uri = new URI(address); |
558 | | - // If the returned uri contains no host and the address contains underscores, |
559 | | - // then it's likely that Java did not parse the URI |
560 | | - if(uri.getHost() == null && address.contains("_")){ |
561 | | - try { |
562 | | - final Field hostField = URI.class.getDeclaredField("host"); |
563 | | - hostField.setAccessible(true); |
564 | | - // Get everything after the scheme:// |
565 | | - String shortAddress = address.substring(uri.getScheme().length() + 3); |
566 | | - hostField.set(uri, getHostName(shortAddress)); |
567 | | - |
568 | | - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { |
569 | | - throw ExceptionHelper.createMqttException(e.getCause()); |
570 | | - } |
571 | | - |
572 | | - } |
573 | | - } catch (URISyntaxException e) { |
574 | | - throw new IllegalArgumentException("Malformed URI: " + address + ", " + e.getMessage()); |
575 | | - } |
576 | | - |
577 | | - String host = uri.getHost(); |
578 | | - int port = uri.getPort(); // -1 if not defined |
579 | | - |
580 | | - switch (serverURIType) { |
581 | | - case MqttConnectOptions.URI_TYPE_TCP : |
582 | | - if (port == -1) { |
583 | | - port = 1883; |
584 | | - } |
585 | | - if (factory == null) { |
586 | | - factory = SocketFactory.getDefault(); |
587 | | - } |
588 | | - else if (factory instanceof SSLSocketFactory) { |
589 | | - throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH); |
590 | | - } |
591 | | - netModule = new TCPNetworkModule(factory, host, port, clientId); |
592 | | - ((TCPNetworkModule)netModule).setConnectTimeout(options.getConnectionTimeout()); |
593 | | - break; |
594 | | - case MqttConnectOptions.URI_TYPE_SSL: |
595 | | - if (port == -1) { |
596 | | - port = 8883; |
597 | | - } |
598 | | - SSLSocketFactoryFactory factoryFactory = null; |
599 | | - if (factory == null) { |
600 | | -// try { |
601 | | - factoryFactory = new SSLSocketFactoryFactory(); |
602 | | - Properties sslClientProps = options.getSSLProperties(); |
603 | | - if (null != sslClientProps) |
604 | | - factoryFactory.initialize(sslClientProps, null); |
605 | | - factory = factoryFactory.createSocketFactory(null); |
606 | | -// } |
607 | | -// catch (MqttDirectException ex) { |
608 | | -// throw ExceptionHelper.createMqttException(ex.getCause()); |
609 | | -// } |
610 | | - } |
611 | | - else if ((factory instanceof SSLSocketFactory) == false) { |
612 | | - throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH); |
613 | | - } |
614 | | - |
615 | | - // Create the network module... |
616 | | - netModule = new SSLNetworkModule((SSLSocketFactory) factory, host, port, clientId); |
617 | | - ((SSLNetworkModule)netModule).setSSLhandshakeTimeout(options.getConnectionTimeout()); |
618 | | - ((SSLNetworkModule)netModule).setSSLHostnameVerifier(options.getSSLHostnameVerifier()); |
619 | | - // Ciphers suites need to be set, if they are available |
620 | | - if (factoryFactory != null) { |
621 | | - String[] enabledCiphers = factoryFactory.getEnabledCipherSuites(null); |
622 | | - if (enabledCiphers != null) { |
623 | | - ((SSLNetworkModule) netModule).setEnabledCiphers(enabledCiphers); |
624 | | - } |
625 | | - } |
626 | | - break; |
627 | | - case MqttConnectOptions.URI_TYPE_WS: |
628 | | - if (port == -1) { |
629 | | - port = 80; |
630 | | - } |
631 | | - if (factory == null) { |
632 | | - factory = SocketFactory.getDefault(); |
633 | | - } |
634 | | - else if (factory instanceof SSLSocketFactory) { |
635 | | - throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH); |
636 | | - } |
637 | | - netModule = new WebSocketNetworkModule(factory, address, host, port, clientId); |
638 | | - ((WebSocketNetworkModule)netModule).setConnectTimeout(options.getConnectionTimeout()); |
639 | | - break; |
640 | | - case MqttConnectOptions.URI_TYPE_WSS: |
641 | | - if (port == -1) { |
642 | | - port = 443; |
643 | | - } |
644 | | - SSLSocketFactoryFactory wSSFactoryFactory = null; |
645 | | - if (factory == null) { |
646 | | - wSSFactoryFactory = new SSLSocketFactoryFactory(); |
647 | | - Properties sslClientProps = options.getSSLProperties(); |
648 | | - if (null != sslClientProps) |
649 | | - wSSFactoryFactory.initialize(sslClientProps, null); |
650 | | - factory = wSSFactoryFactory.createSocketFactory(null); |
651 | | - |
652 | | - } |
653 | | - else if ((factory instanceof SSLSocketFactory) == false) { |
654 | | - throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH); |
655 | | - } |
656 | | - |
657 | | - // Create the network module... |
658 | | - netModule = new WebSocketSecureNetworkModule((SSLSocketFactory) factory, address, host, port, clientId); |
659 | | - ((WebSocketSecureNetworkModule)netModule).setSSLhandshakeTimeout(options.getConnectionTimeout()); |
660 | | - // Ciphers suites need to be set, if they are available |
661 | | - if (wSSFactoryFactory != null) { |
662 | | - String[] enabledCiphers = wSSFactoryFactory.getEnabledCipherSuites(null); |
663 | | - if (enabledCiphers != null) { |
664 | | - ((SSLNetworkModule) netModule).setEnabledCiphers(enabledCiphers); |
665 | | - } |
666 | | - } |
667 | | - break; |
668 | | - default: |
669 | | - // This shouldn't happen, as long as validateURI() has been called. |
670 | | - log.fine(CLASS_NAME,methodName, "119", new Object[] {address}); |
671 | | - netModule = null; |
672 | | - } |
| 541 | + NetworkModule netModule = NetworkModuleService.createInstance(address, options, clientId); |
673 | 542 | return netModule; |
674 | 543 | } |
675 | 544 |
|
|
0 commit comments