2121
2222package org .eclipse .paho .client .mqttv3 ;
2323
24+ import java .net .URI ;
25+ import java .net .URISyntaxException ;
2426import java .util .Hashtable ;
2527import java .util .Properties ;
2628import java .util .Timer ;
@@ -358,7 +360,7 @@ protected NetworkModule[] createNetworkModules(String address, MqttConnectOption
358360 * supplied address URI.
359361 *
360362 * @param address the URI for the server.
361- * @param Connect options
363+ * @param options Connect options
362364 * @return a network module appropriate to the specified address.
363365 */
364366 private NetworkModule createNetworkModule (String address , MqttConnectOptions options ) throws MqttException , MqttSecurityException {
@@ -367,18 +369,25 @@ private NetworkModule createNetworkModule(String address, MqttConnectOptions opt
367369 log .fine (CLASS_NAME ,methodName , "115" , new Object [] {address });
368370
369371 NetworkModule netModule ;
370- String shortAddress ;
371- String host ;
372- int port ;
373372 SocketFactory factory = options .getSocketFactory ();
374373
375374 int serverURIType = MqttConnectOptions .validateURI (address );
376375
376+ URI uri ;
377+ try {
378+ uri = new URI (address );
379+ } catch (URISyntaxException e ) {
380+ throw new IllegalArgumentException ("Malformed URI: " + address , e );
381+ }
382+
383+ String host = uri .getHost ();
384+ int port = uri .getPort (); // -1 if not defined
385+
377386 switch (serverURIType ) {
378387 case MqttConnectOptions .URI_TYPE_TCP :
379- shortAddress = address . substring ( 6 );
380- host = getHostName ( shortAddress ) ;
381- port = getPort ( shortAddress , 1883 );
388+ if ( port == - 1 ) {
389+ port = 1883 ;
390+ }
382391 if (factory == null ) {
383392 factory = SocketFactory .getDefault ();
384393 }
@@ -389,9 +398,9 @@ else if (factory instanceof SSLSocketFactory) {
389398 ((TCPNetworkModule )netModule ).setConnectTimeout (options .getConnectionTimeout ());
390399 break ;
391400 case MqttConnectOptions .URI_TYPE_SSL :
392- shortAddress = address . substring ( 6 );
393- host = getHostName ( shortAddress ) ;
394- port = getPort ( shortAddress , 8883 );
401+ if ( port == - 1 ) {
402+ port = 8883 ;
403+ }
395404 SSLSocketFactoryFactory factoryFactory = null ;
396405 if (factory == null ) {
397406// try {
@@ -421,9 +430,9 @@ else if ((factory instanceof SSLSocketFactory) == false) {
421430 }
422431 break ;
423432 case MqttConnectOptions .URI_TYPE_WS :
424- shortAddress = address . substring ( 5 );
425- host = getHostName ( shortAddress ) ;
426- port = getPort ( shortAddress , 80 );
433+ if ( port == - 1 ) {
434+ port = 80 ;
435+ }
427436 if (factory == null ) {
428437 factory = SocketFactory .getDefault ();
429438 }
@@ -434,9 +443,9 @@ else if (factory instanceof SSLSocketFactory) {
434443 ((WebSocketNetworkModule )netModule ).setConnectTimeout (options .getConnectionTimeout ());
435444 break ;
436445 case MqttConnectOptions .URI_TYPE_WSS :
437- shortAddress = address . substring ( 6 );
438- host = getHostName ( shortAddress ) ;
439- port = getPort ( shortAddress , 443 );
446+ if ( port == - 1 ) {
447+ port = 443 ;
448+ }
440449 SSLSocketFactoryFactory wSSFactoryFactory = null ;
441450 if (factory == null ) {
442451 wSSFactoryFactory = new SSLSocketFactoryFactory ();
@@ -471,33 +480,6 @@ else if ((factory instanceof SSLSocketFactory) == false) {
471480 return netModule ;
472481 }
473482
474- private int getPort (String uri , int defaultPort ) {
475- int port ;
476- int portIndex = uri .lastIndexOf (':' );
477- if (portIndex == -1 ) {
478- port = defaultPort ;
479- }
480- else {
481- int slashIndex = uri .indexOf ('/' );
482- if (slashIndex == -1 ) {
483- slashIndex = uri .length ();
484- }
485- port = Integer .parseInt (uri .substring (portIndex + 1 , slashIndex ));
486- }
487- return port ;
488- }
489-
490- private String getHostName (String uri ) {
491- int portIndex = uri .indexOf (':' );
492- if (portIndex == -1 ) {
493- portIndex = uri .indexOf ('/' );
494- }
495- if (portIndex == -1 ) {
496- portIndex = uri .length ();
497- }
498- return uri .substring (0 , portIndex );
499- }
500-
501483 /* (non-Javadoc)
502484 * @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#connect(java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)
503485 */
0 commit comments