3636
3737import java .io .Closeable ;
3838import java .time .Duration ;
39+ import java .util .Arrays ;
3940import java .util .HashSet ;
4041import java .util .List ;
4142import java .util .Map ;
@@ -185,8 +186,9 @@ public MqttClient(DeviceConfiguration deviceConfiguration, ScheduledExecutorServ
185186 } catch (MqttConnectionProviderException e ) {
186187 throw new MqttException (e .getMessage ());
187188 }
189+ String endpoint = Coerce .toString (deviceConfiguration .getIotDataEndpoint ());
188190 builder .withCertificateAuthorityFromPath (null , Coerce .toString (deviceConfiguration .getRootCAFilePath ()))
189- .withEndpoint (Coerce . toString ( deviceConfiguration . getIotDataEndpoint ()) )
191+ .withEndpoint (endpoint )
190192 .withPort ((short ) Coerce .toInt (mqttTopics .findOrDefault (DEFAULT_MQTT_PORT , MQTT_PORT_KEY )))
191193 .withCleanSession (false ).withBootstrap (clientBootstrap ).withKeepAliveMs (Coerce .toInt (
192194 mqttTopics .findOrDefault (DEFAULT_MQTT_KEEP_ALIVE_TIMEOUT , MQTT_KEEP_ALIVE_TIMEOUT_KEY )))
@@ -195,9 +197,19 @@ public MqttClient(DeviceConfiguration deviceConfiguration, ScheduledExecutorServ
195197 Coerce .toInt (mqttTopics .findOrDefault (DEFAULT_MQTT_PING_TIMEOUT , MQTT_PING_TIMEOUT_KEY )))
196198 .withSocketOptions (new SocketOptions ()).withTimeoutMs (Coerce .toInt (
197199 mqttTopics .findOrDefault (DEFAULT_MQTT_SOCKET_TIMEOUT , MQTT_SOCKET_TIMEOUT_KEY )));
198- HttpProxyOptions httpProxyOptions = ProxyUtils .getHttpProxyOptions (deviceConfiguration , proxyTlsContext );
200+
201+ HttpProxyOptions httpProxyOptions =
202+ ProxyUtils .getHttpProxyOptions (deviceConfiguration , proxyTlsContext );
199203 if (httpProxyOptions != null ) {
200- builder .withHttpProxyOptions (httpProxyOptions );
204+ String noProxy = Coerce .toString (deviceConfiguration .getNoProxyAddresses ());
205+ boolean useProxy = true ;
206+ // Only use the proxy when the endpoint we're connecting to is not in the NoProxyAddress list
207+ if (Utils .isNotEmpty (noProxy ) && Utils .isNotEmpty (endpoint )) {
208+ useProxy = Arrays .stream (noProxy .split ("," )).noneMatch (endpoint ::matches );
209+ }
210+ if (useProxy ) {
211+ builder .withHttpProxyOptions (httpProxyOptions );
212+ }
201213 }
202214 return builder ;
203215 };
0 commit comments