PageRenderTime 35ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/eclipse/paho.mqtt.java
Java | 971 lines | 283 code | 72 blank | 616 comment | 25 complexity | d666804a8f38287d46b0347e5e4df73c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2017 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0
  10. * and the Eclipse Distribution License is available at
  11. * https://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * Contributors:
  14. * Dave Locke - initial API and implementation and/or initial documentation
  15. * Ian Craggs - MQTT 3.1.1 support
  16. * James Sutton - Automatic Reconnect & Offline Buffering
  17. * James Sutton - MQTT v5
  18. */
  19. package org.eclipse.paho.mqttv5.client;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import javax.net.SocketFactory;
  25. import javax.net.ssl.HostnameVerifier;
  26. import org.eclipse.paho.mqttv5.client.internal.NetworkModuleService;
  27. import org.eclipse.paho.mqttv5.client.util.Debug;
  28. import org.eclipse.paho.mqttv5.common.MqttMessage;
  29. import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
  30. import org.eclipse.paho.mqttv5.common.packet.UserProperty;
  31. import org.eclipse.paho.mqttv5.common.util.MqttTopicValidator;
  32. /**
  33. * Holds the set of options that control how the client connects to a server.
  34. *
  35. * Constructs a new {@link MqttConnectionOptions} object using the default
  36. * values.
  37. *
  38. * The defaults are:
  39. * <ul>
  40. * <li>The keep alive interval is 60 seconds</li>
  41. * <li>Clean Session is true</li>
  42. * <li>The message delivery retry interval is 15 seconds</li>
  43. * <li>The connection timeout period is 30 seconds</li>
  44. * <li>No Will message is set</li>
  45. * <li>Automatic Reconnect is enabled, starting at 1 second and capped at two
  46. * minutes</li>
  47. * <li>A standard SocketFactory is used</li>
  48. * </ul>
  49. * More information about these values can be found in the setter methods.
  50. */
  51. public class MqttConnectionOptions {
  52. private static final String CLIENT_ID_PREFIX = "paho";
  53. // Connection Behaviour Properties
  54. private String[] serverURIs = null; // List of Servers to connect to in order
  55. private boolean automaticReconnect = false; // Automatic Reconnect
  56. private int automaticReconnectMinDelay = 1; // Time to wait before first automatic reconnection attempt in seconds.
  57. private int automaticReconnectMaxDelay = 120; // Max time to wait for automatic reconnection attempts in seconds.
  58. private boolean useSubscriptionIdentifiers = true; // Whether to automatically assign subscription identifiers.
  59. private int keepAliveInterval = 60; // Keep Alive Interval
  60. private int connectionTimeout = 30; // Connection timeout in seconds
  61. private boolean httpsHostnameVerificationEnabled = true;
  62. private int maxReconnectDelay = 128000;
  63. private boolean sendReasonMessages = false;
  64. public MqttProperties getConnectionProperties() {
  65. MqttProperties connectionProperties = new MqttProperties();
  66. connectionProperties.setSessionExpiryInterval(sessionExpiryInterval);
  67. connectionProperties.setReceiveMaximum(receiveMaximum);
  68. connectionProperties.setMaximumPacketSize(maximumPacketSize);
  69. connectionProperties.setTopicAliasMaximum(topicAliasMaximum);
  70. connectionProperties.setRequestResponseInfo(requestResponseInfo);
  71. connectionProperties.setRequestProblemInfo(requestProblemInfo);
  72. connectionProperties.setUserProperties(userProperties);
  73. connectionProperties.setAuthenticationMethod(authMethod);
  74. connectionProperties.setAuthenticationData(authData);
  75. return connectionProperties;
  76. }
  77. public MqttProperties getWillMessageProperties() {
  78. return willMessageProperties;
  79. }
  80. public void setWillMessageProperties(MqttProperties willMessageProperties) {
  81. this.willMessageProperties = willMessageProperties;
  82. }
  83. MqttProperties willMessageProperties = new MqttProperties();
  84. // Connection packet properties
  85. private int mqttVersion = 5; // MQTT Version 5
  86. private boolean cleanStart = true; // Clean Session
  87. private String willDestination = null; // Will Topic
  88. private MqttMessage willMessage = null; // Will Message
  89. private String userName; // Username
  90. private byte[] password; // Password
  91. private Long sessionExpiryInterval = null; // The Session expiry Interval in seconds, null is the default of
  92. // never.
  93. private Integer receiveMaximum = null; // The Receive Maximum, null defaults to 65,535, cannot be 0.
  94. private Long maximumPacketSize = null; // The Maximum packet size, null defaults to no limit.
  95. private Integer topicAliasMaximum = null; // The Topic Alias Maximum, null defaults to 0.
  96. private Boolean requestResponseInfo = null; // Request Response Information, null defaults to false.
  97. private Boolean requestProblemInfo = null; // Request Problem Information, null defaults to true.
  98. private List<UserProperty> userProperties = null; // User Defined Properties.
  99. private String authMethod = null; // Authentication Method, If null, Extended Authentication is not performed.
  100. private byte[] authData = null; // Authentication Data.
  101. // TLS Properties
  102. private SocketFactory socketFactory; // SocketFactory to be used to connect
  103. private Properties sslClientProps = null; // SSL Client Properties
  104. private HostnameVerifier sslHostnameVerifier = null; // SSL Hostname Verifier
  105. private Map<String, String> customWebSocketHeaders;
  106. // Client Operation Parameters
  107. private int executorServiceTimeout = 1; // How long to wait in seconds when terminating the executor service.
  108. /**
  109. * Returns the MQTT version.
  110. *
  111. * @return the MQTT version.
  112. */
  113. public int getMqttVersion() {
  114. return mqttVersion;
  115. }
  116. /**
  117. * Returns the user name to use for the connection.
  118. *
  119. * @return the user name to use for the connection.
  120. */
  121. public String getUserName() {
  122. return userName;
  123. }
  124. /**
  125. * Sets the user name to use for the connection.
  126. *
  127. * @param userName
  128. * The Username as a String
  129. */
  130. public void setUserName(String userName) {
  131. this.userName = userName;
  132. }
  133. /**
  134. * Returns the password to use for the connection.
  135. *
  136. * @return the password to use for the connection.
  137. */
  138. public byte[] getPassword() {
  139. return password;
  140. }
  141. /**
  142. * Sets the password to use for the connection.
  143. *
  144. * @param password
  145. * A Char Array of the password
  146. */
  147. public void setPassword(byte[] password) {
  148. this.password = password;
  149. }
  150. /**
  151. * Returns the topic to be used for last will and testament (LWT).
  152. *
  153. * @return the MqttTopic to use, or <code>null</code> if LWT is not set.
  154. * @see #setWill(String, MqttMessage)
  155. */
  156. public String getWillDestination() {
  157. return willDestination;
  158. }
  159. /**
  160. * Returns the message to be sent as last will and testament (LWT). The returned
  161. * object is "read only". Calling any "setter" methods on the returned object
  162. * will result in an <code>IllegalStateException</code> being thrown.
  163. *
  164. * @return the message to use, or <code>null</code> if LWT is not set.
  165. */
  166. public MqttMessage getWillMessage() {
  167. return willMessage;
  168. }
  169. /**
  170. * Sets the "Last Will and Testament" (LWT) for the connection. In the event
  171. * that this client unexpectedly looses it's connection to the server, the
  172. * server will publish a message to itself using the supplied details.
  173. *
  174. * @param topic
  175. * the topic to publish to.
  176. * @param message
  177. * the {@link MqttMessage} to send
  178. */
  179. public void setWill(String topic, MqttMessage message) {
  180. if (topic == null || message == null || message.getPayload() == null) {
  181. throw new IllegalArgumentException();
  182. }
  183. MqttTopicValidator.validate(topic, false, true); // Wildcards are not allowed
  184. this.willDestination = topic;
  185. this.willMessage = message;
  186. // Prevent any more changes to the will message
  187. this.willMessage.setMutable(false);
  188. }
  189. /**
  190. * Returns whether the client and server should remember state for the client
  191. * across reconnects.
  192. *
  193. * @return the clean session flag
  194. */
  195. public boolean isCleanStart() {
  196. return this.cleanStart;
  197. }
  198. /**
  199. * Sets whether the client and server should remember state across restarts and
  200. * reconnects. If set to false, the server will retain the session state until
  201. * either:
  202. * <ul>
  203. * <li>A new connection is made with the client and with the cleanStart flag set
  204. * to true.</li>
  205. * <li>The Session expiry interval is exceeded after the network connection is
  206. * closed, see {@link MqttConnectionOptions#setSessionExpiryInterval}</li>
  207. * </ul>
  208. *
  209. * If set to true, the server will immediately drop any existing session state
  210. * for the given client and will initiate a new session.
  211. *
  212. * In order to implement QoS 1 and QoS 2 protocol flows the Client and Server
  213. * need to associate state with the Client Identifier, this is referred to as
  214. * the Session State. The Server also stores the subscriptions as part of the
  215. * Session State.
  216. *
  217. * The session can continue across a sequence of Network Connections. It lasts
  218. * as long as the latest Network Connection plus the Session Expiry Interval.
  219. *
  220. * The Session State in the Client consists of:
  221. *
  222. * <ul>
  223. * <li>QoS 1 and QoS 2 messages which have been sent to the Server, but have not
  224. * been completely acknowledged.</li>
  225. * <li>QoS 2 messages which have been received from the Server, but have not
  226. * been completely acknowledged.</li>
  227. * </ul>
  228. *
  229. * The Session State in the Server consists of:
  230. * <ul>
  231. * <li>The existence of a Session, even if the rest of the Session State is
  232. * empty.</li>
  233. * <li>The Clients subscriptions, including any Subscription Identifiers.</li>
  234. * <li>QoS 1 and QoS 2 messages which have been sent to the Client, but have not
  235. * been completely acknowledged.</li>
  236. * <li>QoS 1 and QoS 2 messages pending transmission to the Client and
  237. * OPTIONALLY QoS 0 messages pending transmission to the Client.</li>
  238. * <li>QoS 2 messages which have been received from the Client, but have not
  239. * been completely acknowledged.The Will Message and the Will Delay
  240. * Interval</li>
  241. * <li>If the Session is currently not connected, the time at which the Session
  242. * will end and Session State will be discarded.</li>
  243. * </ul>
  244. *
  245. * Retained messages do not form part of the Session State in the Server, they
  246. * are not deleted as a result of a Session ending.
  247. *
  248. *
  249. *
  250. * @param cleanStart
  251. * Set to True to enable cleanSession
  252. */
  253. public void setCleanStart(boolean cleanStart) {
  254. this.cleanStart = cleanStart;
  255. }
  256. /**
  257. * Returns the "keep alive" interval.
  258. *
  259. * @see #setKeepAliveInterval(int)
  260. * @return the keep alive interval.
  261. */
  262. public int getKeepAliveInterval() {
  263. return keepAliveInterval;
  264. }
  265. /**
  266. * Sets the "keep alive" interval. This value, measured in seconds, defines the
  267. * maximum time interval between messages sent or received. It enables the
  268. * client to detect if the server is no longer available, without having to wait
  269. * for the TCP/IP timeout. The client will ensure that at least one message
  270. * travels across the network within each keep alive period. In the absence of a
  271. * data-related message during the time period, the client sends a very small
  272. * "ping" message, which the server will acknowledge. A value of 0 disables
  273. * keepalive processing in the client.
  274. * <p>
  275. * The default value is 60 seconds
  276. * </p>
  277. *
  278. * @param keepAliveInterval
  279. * the interval, measured in seconds, must be &gt;= 0.
  280. * @throws IllegalArgumentException
  281. * if the keepAliveInterval was invalid
  282. */
  283. public void setKeepAliveInterval(int keepAliveInterval) {
  284. if (keepAliveInterval < 0) {
  285. throw new IllegalArgumentException();
  286. }
  287. this.keepAliveInterval = keepAliveInterval;
  288. }
  289. /**
  290. * Returns the connection timeout value.
  291. *
  292. * @see #setConnectionTimeout(int)
  293. * @return the connection timeout value.
  294. */
  295. public int getConnectionTimeout() {
  296. return connectionTimeout;
  297. }
  298. /**
  299. * Sets the connection timeout value. This value, measured in seconds, defines
  300. * the maximum time interval the client will wait for the network connection to
  301. * the MQTT server to be established. The default timeout is 30 seconds. A value
  302. * of 0 disables timeout processing meaning the client will wait until the
  303. * network connection is made successfully or fails.
  304. *
  305. * @param connectionTimeout
  306. * the timeout value, measured in seconds. It must be &gt;0;
  307. */
  308. public void setConnectionTimeout(int connectionTimeout) {
  309. if (connectionTimeout < 0) {
  310. throw new IllegalArgumentException();
  311. }
  312. this.connectionTimeout = connectionTimeout;
  313. }
  314. /**
  315. * Get the maximum time (in millis) to wait between reconnects
  316. *
  317. * @return Get the maximum time (in millis) to wait between reconnects
  318. */
  319. public int getMaxReconnectDelay() {
  320. return maxReconnectDelay;
  321. }
  322. /**
  323. * Set the maximum time to wait between reconnects
  324. *
  325. * @param maxReconnectDelay
  326. * the duration (in millis)
  327. */
  328. public void setMaxReconnectDelay(int maxReconnectDelay) {
  329. this.maxReconnectDelay = maxReconnectDelay;
  330. }
  331. /**
  332. * Return a list of serverURIs the client may connect to
  333. *
  334. * @return the serverURIs or null if not set
  335. */
  336. public String[] getServerURIs() {
  337. return serverURIs;
  338. }
  339. /**
  340. * Set a list of one or more serverURIs the client may connect to.
  341. * <p>
  342. * Each <code>serverURI</code> specifies the address of a server that the client
  343. * may connect to. Two types of connection are supported <code>tcp://</code> for
  344. * a TCP connection and <code>ssl://</code> for a TCP connection secured by
  345. * SSL/TLS. For example:
  346. * <ul>
  347. * <li><code>tcp://localhost:1883</code></li>
  348. * <li><code>ssl://localhost:8883</code></li>
  349. * </ul>
  350. * If the port is not specified, it will default to 1883 for
  351. * <code>tcp://</code>" URIs, and 8883 for <code>ssl://</code> URIs.
  352. * <p>
  353. * If serverURIs is set then it overrides the serverURI parameter passed in on
  354. * the constructor of the MQTT client.
  355. * <p>
  356. * When an attempt to connect is initiated the client will start with the first
  357. * serverURI in the list and work through the list until a connection is
  358. * established with a server. If a connection cannot be made to any of the
  359. * servers then the connect attempt fails.
  360. * <p>
  361. * Specifying a list of servers that a client may connect to has several uses:
  362. * <ol>
  363. * <li>High Availability and reliable message delivery
  364. * <p>
  365. * Some MQTT servers support a high availability feature where two or more
  366. * "equal" MQTT servers share state. An MQTT client can connect to any of the
  367. * "equal" servers and be assured that messages are reliably delivered and
  368. * durable subscriptions are maintained no matter which server the client
  369. * connects to.
  370. * </p>
  371. * <p>
  372. * The cleanStart flag must be set to false if durable subscriptions and/or
  373. * reliable message delivery is required.
  374. * </p>
  375. * </li>
  376. * <li>Hunt List
  377. * <p>
  378. * A set of servers may be specified that are not "equal" (as in the high
  379. * availability option). As no state is shared across the servers reliable
  380. * message delivery and durable subscriptions are not valid. The cleanStart flag
  381. * must be set to true if the hunt list mode is used
  382. * </p>
  383. * </li>
  384. * </ol>
  385. *
  386. * @param serverURIs
  387. * to be used by the client
  388. */
  389. public void setServerURIs(String[] serverURIs) {
  390. for (String serverURI : serverURIs) {
  391. NetworkModuleService.validateURI(serverURI);
  392. }
  393. this.serverURIs = serverURIs.clone();
  394. }
  395. /**
  396. * Returns whether the client will automatically attempt to reconnect to the
  397. * server if the connection is lost
  398. *
  399. * @return the automatic reconnection flag.
  400. */
  401. public boolean isAutomaticReconnect() {
  402. return automaticReconnect;
  403. }
  404. /**
  405. * Sets whether the client will automatically attempt to reconnect to the server
  406. * if the connection is lost.
  407. * <ul>
  408. * <li>If set to false, the client will not attempt to automatically reconnect
  409. * to the server in the event that the connection is lost.</li>
  410. * <li>If set to true, in the event that the connection is lost, the client will
  411. * attempt to reconnect to the server. It will initially wait 1 second before it
  412. * attempts to reconnect, for every failed reconnect attempt, the delay will
  413. * double until it is at 2 minutes at which point the delay will stay at 2
  414. * minutes.</li>
  415. * </ul>
  416. *
  417. * You can change the Minimum and Maximum delays by using
  418. * {@link #setAutomaticReconnectDelay(int, int)}
  419. *
  420. * This Defaults to true
  421. *
  422. * @param automaticReconnect
  423. * If set to True, Automatic Reconnect will be enabled
  424. */
  425. public void setAutomaticReconnect(boolean automaticReconnect) {
  426. this.automaticReconnect = automaticReconnect;
  427. }
  428. /**
  429. * Sets the Minimum and Maximum delays used when attempting to automatically
  430. * reconnect.
  431. *
  432. * @param minDelay
  433. * the minimum delay to wait before attempting to reconnect in
  434. * seconds, defaults to 1 second.
  435. * @param maxDelay
  436. * the maximum delay to wait before attempting to reconnect in
  437. * seconds, defaults to 120 seconds.
  438. */
  439. public void setAutomaticReconnectDelay(int minDelay, int maxDelay) {
  440. this.automaticReconnectMinDelay = minDelay;
  441. this.automaticReconnectMaxDelay = maxDelay;
  442. }
  443. /**
  444. * Returns the minimum number of seconds to wait before attempting to
  445. * automatically reconnect.
  446. *
  447. * @return the automatic reconnect minimum delay in seconds.
  448. */
  449. public int getAutomaticReconnectMinDelay() {
  450. return automaticReconnectMinDelay;
  451. }
  452. /**
  453. * Returns the maximum number of seconds to wait before attempting to
  454. * automatically reconnect.
  455. *
  456. * @return the automatic reconnect maximum delay in seconds.
  457. */
  458. public int getAutomaticReconnectMaxDelay() {
  459. return automaticReconnectMaxDelay;
  460. }
  461. /**
  462. * Returns the Session Expiry Interval. If <code>null</code>, this means the
  463. * session will not expire.
  464. *
  465. * @return the Session Expiry Interval in seconds.
  466. */
  467. public Long getSessionExpiryInterval() {
  468. return sessionExpiryInterval;
  469. }
  470. /**
  471. * Sets the Session Expiry Interval. This value, measured in seconds, defines
  472. * the maximum time that the broker will maintain the session for once the
  473. * client disconnects. Clients should only connect with a long Session Expiry
  474. * interval if they intend to connect to the server at some later point in time.
  475. *
  476. * <ul>
  477. * <li>By default this value is null and so will not be sent, in this case, the
  478. * session will not expire.</li>
  479. * <li>If a 0 is sent, the session will end immediately once the Network
  480. * Connection is closed.</li>
  481. * </ul>
  482. *
  483. * When the client has determined that it has no longer any use for the session,
  484. * it should disconnect with a Session Expiry Interval set to 0.
  485. *
  486. * @param sessionExpiryInterval
  487. * The Session Expiry Interval in seconds.
  488. */
  489. public void setSessionExpiryInterval(Long sessionExpiryInterval) {
  490. this.sessionExpiryInterval = sessionExpiryInterval;
  491. }
  492. /**
  493. * Returns the Receive Maximum value. If <code>null</code>, it will default to
  494. * 65,535.
  495. *
  496. * @return the Receive Maximum
  497. */
  498. public Integer getReceiveMaximum() {
  499. return receiveMaximum;
  500. }
  501. /**
  502. * Sets the Receive Maximum. This value represents the limit of QoS 1 and QoS 2
  503. * publications that the client is willing to process concurrently. There is no
  504. * mechanism to limit the number of QoS 0 publications that the Server might try
  505. * to send.
  506. * <ul>
  507. * <li>If set to <code>null</code> then this value defaults to 65,535.</li>
  508. * <li>If set, the minimum value for this property is 1.</li>
  509. * <li>The maximum value for this property is 65,535.</li>
  510. * </ul>
  511. *
  512. * @param receiveMaximum
  513. * the Receive Maximum.
  514. */
  515. public void setReceiveMaximum(Integer receiveMaximum) {
  516. if (receiveMaximum != null && (receiveMaximum == 0 || receiveMaximum > 65535)) {
  517. throw new IllegalArgumentException();
  518. }
  519. this.receiveMaximum = receiveMaximum;
  520. }
  521. /**
  522. * Returns the Maximum Packet Size. If <code>null</code>, no limit is imposed.
  523. *
  524. * @return the Maximum Packet Size in bytes.
  525. */
  526. public Long getMaximumPacketSize() {
  527. return maximumPacketSize;
  528. }
  529. /**
  530. * Sets the Maximum Packet Size. This value represents the Maximum Packet Size
  531. * the client is willing to accept.
  532. *
  533. * <ul>
  534. * <li>If set to <code>null</code> then no limit is imposed beyond the
  535. * limitations of the protocol.</li>
  536. * <li>If set, the minimum value for this property is 1.</li>
  537. * <li>The maximum value for this property is 2,684,354,656.</li>
  538. * </ul>
  539. *
  540. * @param maximumPacketSize
  541. * The Maximum packet size.
  542. */
  543. public void setMaximumPacketSize(Long maximumPacketSize) {
  544. this.maximumPacketSize = maximumPacketSize;
  545. }
  546. /**
  547. * Returns the Topic Alias Maximum. If <code>null</code>, the default value is
  548. * 0.
  549. *
  550. * @return the Topic Alias Maximum.
  551. */
  552. public Integer getTopicAliasMaximum() {
  553. return topicAliasMaximum;
  554. }
  555. /**
  556. * Sets the Topic Alias Maximum. This value if present represents the highest
  557. * value that the Client will accept as a Topic Alias sent by the Server.
  558. *
  559. * <ul>
  560. * <li>If set to <code>null</code>, then it will default to to 0.</li>
  561. * <li>If set to 0, the Client will not accept any Topic Aliases</li>
  562. * <li>The Maximum value for this property is 65535.</li>
  563. * </ul>
  564. *
  565. * @param topicAliasMaximum
  566. * the Topic Alias Maximum
  567. */
  568. public void setTopicAliasMaximum(Integer topicAliasMaximum) {
  569. if (topicAliasMaximum != null && topicAliasMaximum > 65535) {
  570. throw new IllegalArgumentException();
  571. }
  572. this.topicAliasMaximum = topicAliasMaximum;
  573. }
  574. /**
  575. * Returns the Request Response Info flag. If <code>null</code>, the default
  576. * value is false.
  577. *
  578. * @return The Request Response Info Flag.
  579. */
  580. public Boolean getRequestResponseInfo() {
  581. return requestResponseInfo;
  582. }
  583. /**
  584. * Sets the Request Response Info Flag.
  585. * <ul>
  586. * <li>If set to <code>null</code>, then it will default to false.</li>
  587. * <li>If set to false, the server will not return any response information in
  588. * the CONNACK.</li>
  589. * <li>If set to true, the server MAY return response information in the
  590. * CONNACK.</li>
  591. * </ul>
  592. *
  593. * @param requestResponseInfo
  594. * The Request Response Info Flag.
  595. */
  596. public void setRequestResponseInfo(boolean requestResponseInfo) {
  597. this.requestResponseInfo = requestResponseInfo;
  598. }
  599. /**
  600. * Returns the Request Problem Info flag. If <code>null</code>, the default
  601. * value is true.
  602. *
  603. * @return the Request Problem Info flag.
  604. */
  605. public Boolean getRequestProblemInfo() {
  606. return requestProblemInfo;
  607. }
  608. /**
  609. * Sets the Request Problem Info flag.
  610. * <ul>
  611. * <li>If set to <code>null</code>, then it will default to true.</li>
  612. * <li>If set to false, the server MAY return a Reason String or User Properties
  613. * on a CONNACK or DISCONNECT, but must not send a Reason String or User
  614. * Properties on any packet other than PUBLISH, CONNACK or DISCONNECT.</li>
  615. * <li>If set to true, the server MAY return a Reason String or User Properties
  616. * on any packet where it is allowed.</li>
  617. * </ul>
  618. *
  619. * @param requestProblemInfo
  620. * The Flag to request problem information.
  621. */
  622. public void setRequestProblemInfo(boolean requestProblemInfo) {
  623. this.requestProblemInfo = requestProblemInfo;
  624. }
  625. /**
  626. * Returns the User Properties.
  627. *
  628. * @return the User Properties.
  629. */
  630. public List<UserProperty> getUserProperties() {
  631. return userProperties;
  632. }
  633. /**
  634. * Sets the User Properties. A User Property is a UTF-8 String Pair, the same
  635. * name is allowed to appear more than once.
  636. *
  637. * @param userProperties
  638. * User Properties
  639. */
  640. public void setUserProperties(List<UserProperty> userProperties) {
  641. this.userProperties = userProperties;
  642. }
  643. /**
  644. * Returns the Authentication Method. If <code>null</code>, extended
  645. * authentication is not performed.
  646. *
  647. * @return the Authentication Method.
  648. */
  649. public String getAuthMethod() {
  650. return authMethod;
  651. }
  652. /**
  653. * Sets the Authentication Method. If set, this value contains the name of the
  654. * authentication method to be used for extended authentication.
  655. *
  656. * If <code>null</code>, extended authentication is not performed.
  657. *
  658. * @param authMethod
  659. * The Authentication Method.
  660. */
  661. public void setAuthMethod(String authMethod) {
  662. this.authMethod = authMethod;
  663. }
  664. /**
  665. * Returns the Authentication Data.
  666. *
  667. * @return the Authentication Data.
  668. */
  669. public byte[] getAuthData() {
  670. return authData;
  671. }
  672. /**
  673. * Sets the Authentication Data. If set, this byte array contains the extended
  674. * authentication data, defined by the Authenticated Method. It is a protocol
  675. * error to include Authentication Data if there is no Authentication Method.
  676. *
  677. * @param authData
  678. * The Authentication Data
  679. */
  680. public void setAuthData(byte[] authData) {
  681. this.authData = authData;
  682. }
  683. /**
  684. * Returns the socket factory that will be used when connecting, or
  685. * <code>null</code> if one has not been set.
  686. *
  687. * @return The Socket Factory
  688. */
  689. public SocketFactory getSocketFactory() {
  690. return socketFactory;
  691. }
  692. /**
  693. * Sets the <code>SocketFactory</code> to use. This allows an application to
  694. * apply its own policies around the creation of network sockets. If using an
  695. * SSL connection, an <code>SSLSocketFactory</code> can be used to supply
  696. * application-specific security settings.
  697. *
  698. * @param socketFactory
  699. * the factory to use.
  700. */
  701. public void setSocketFactory(SocketFactory socketFactory) {
  702. this.socketFactory = socketFactory;
  703. }
  704. /**
  705. * Returns the SSL properties for the connection.
  706. *
  707. * @return the properties for the SSL connection
  708. */
  709. public Properties getSSLProperties() {
  710. return sslClientProps;
  711. }
  712. /**
  713. * Sets the SSL properties for the connection.
  714. * <p>
  715. * Note that these properties are only valid if an implementation of the Java
  716. * Secure Socket Extensions (JSSE) is available. These properties are
  717. * <em>not</em> used if a SocketFactory has been set using
  718. * {@link #setSocketFactory(SocketFactory)}. The following properties can be
  719. * used:
  720. * </p>
  721. * <dl>
  722. * <dt>com.ibm.ssl.protocol</dt>
  723. * <dd>One of: SSL, SSLv3, TLS, TLSv1, SSL_TLS.</dd>
  724. * <dt>com.ibm.ssl.contextProvider
  725. * <dd>Underlying JSSE provider. For example "IBMJSSE2" or "SunJSSE"</dd>
  726. *
  727. * <dt>com.ibm.ssl.keyStore</dt>
  728. * <dd>The name of the file that contains the KeyStore object that you want the
  729. * KeyManager to use. For example /mydir/etc/key.p12</dd>
  730. *
  731. * <dt>com.ibm.ssl.keyStorePassword</dt>
  732. * <dd>The password for the KeyStore object that you want the KeyManager to use.
  733. * The password can either be in plain-text, or may be obfuscated using the
  734. * static method:
  735. * <code>com.ibm.micro.security.Password.obfuscate(char[] password)</code>. This
  736. * obfuscates the password using a simple and insecure XOR and Base64 encoding
  737. * mechanism. Note that this is only a simple scrambler to obfuscate clear-text
  738. * passwords.</dd>
  739. *
  740. * <dt>com.ibm.ssl.keyStoreType</dt>
  741. * <dd>Type of key store, for example "PKCS12", "JKS", or "JCEKS".</dd>
  742. *
  743. * <dt>com.ibm.ssl.keyStoreProvider</dt>
  744. * <dd>Key store provider, for example "IBMJCE" or "IBMJCEFIPS".</dd>
  745. *
  746. * <dt>com.ibm.ssl.trustStore</dt>
  747. * <dd>The name of the file that contains the KeyStore object that you want the
  748. * TrustManager to use.</dd>
  749. *
  750. * <dt>com.ibm.ssl.trustStorePassword</dt>
  751. * <dd>The password for the TrustStore object that you want the TrustManager to
  752. * use. The password can either be in plain-text, or may be obfuscated using the
  753. * static method:
  754. * <code>com.ibm.micro.security.Password.obfuscate(char[] password)</code>. This
  755. * obfuscates the password using a simple and insecure XOR and Base64 encoding
  756. * mechanism. Note that this is only a simple scrambler to obfuscate clear-text
  757. * passwords.</dd>
  758. *
  759. * <dt>com.ibm.ssl.trustStoreType</dt>
  760. * <dd>The type of KeyStore object that you want the default TrustManager to
  761. * use. Same possible values as "keyStoreType".</dd>
  762. *
  763. * <dt>com.ibm.ssl.trustStoreProvider</dt>
  764. * <dd>Trust store provider, for example "IBMJCE" or "IBMJCEFIPS".</dd>
  765. *
  766. * <dt>com.ibm.ssl.enabledCipherSuites</dt>
  767. * <dd>A list of which ciphers are enabled. Values are dependent on the
  768. * provider, for example:
  769. * SSL_RSA_WITH_AES_128_CBC_SHA;SSL_RSA_WITH_3DES_EDE_CBC_SHA.</dd>
  770. *
  771. * <dt>com.ibm.ssl.keyManager</dt>
  772. * <dd>Sets the algorithm that will be used to instantiate a KeyManagerFactory
  773. * object instead of using the default algorithm available in the platform.
  774. * Example values: "IbmX509" or "IBMJ9X509".</dd>
  775. *
  776. * <dt>com.ibm.ssl.trustManager</dt>
  777. * <dd>Sets the algorithm that will be used to instantiate a TrustManagerFactory
  778. * object instead of using the default algorithm available in the platform.
  779. * Example values: "PKIX" or "IBMJ9X509".</dd>
  780. * </dl>
  781. *
  782. * @param props
  783. * The SSL {@link Properties}
  784. */
  785. public void setSSLProperties(Properties props) {
  786. this.sslClientProps = props;
  787. }
  788. /**
  789. * Returns the HostnameVerifier for the SSL connection.
  790. *
  791. * @return the HostnameVerifier for the SSL connection
  792. */
  793. public HostnameVerifier getSSLHostnameVerifier() {
  794. return sslHostnameVerifier;
  795. }
  796. /**
  797. * Sets the HostnameVerifier for the SSL connection. Note that it will be used
  798. * after handshake on a connection and you should do actions by yourserlf when
  799. * hostname is verified error.
  800. * <p>
  801. * There is no default HostnameVerifier
  802. * </p>
  803. *
  804. * @param hostnameVerifier
  805. * the {@link HostnameVerifier}
  806. */
  807. public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) {
  808. this.sslHostnameVerifier = hostnameVerifier;
  809. }
  810. /**
  811. * Returns whether to automatically assign subscription identifiers when
  812. * subscribing to a topic.
  813. *
  814. * @return if automatic assignment of subscription identifiers is enabled.
  815. */
  816. public boolean useSubscriptionIdentifiers() {
  817. return useSubscriptionIdentifiers;
  818. }
  819. /**
  820. * Sets whether to automatically assign subscription identifiers when
  821. * subscribing to a topic. This will mean that if a subscription has a callback
  822. * associated with it then it is guaranteed to be called when an incoming
  823. * message has the correct subscription ID embedded. If disabled, then the
  824. * client will do best effort topic matching with all callbacks, however this
  825. * might result in an incorrect callback being called if there are multiple
  826. * subscriptions to topics using a combination of wildcards.
  827. *
  828. * @param useSubscriptionIdentifiers
  829. * Whether to enable automatic assignment of subscription
  830. * identifiers.
  831. */
  832. public void setUseSubscriptionIdentifiers(boolean useSubscriptionIdentifiers) {
  833. this.useSubscriptionIdentifiers = useSubscriptionIdentifiers;
  834. }
  835. public boolean isHttpsHostnameVerificationEnabled() {
  836. return httpsHostnameVerificationEnabled;
  837. }
  838. public void setHttpsHostnameVerificationEnabled(boolean httpsHostnameVerificationEnabled) {
  839. this.httpsHostnameVerificationEnabled = httpsHostnameVerificationEnabled;
  840. }
  841. /**
  842. * @return The Debug Properties
  843. */
  844. public Properties getDebug() {
  845. final String strNull = "null";
  846. Properties p = new Properties();
  847. p.put("MqttVersion", getMqttVersion());
  848. p.put("CleanStart", Boolean.valueOf(isCleanStart()));
  849. p.put("ConTimeout", getConnectionTimeout());
  850. p.put("KeepAliveInterval", getKeepAliveInterval());
  851. p.put("UserName", (getUserName() == null) ? strNull : getUserName());
  852. p.put("WillDestination", (getWillDestination() == null) ? strNull : getWillDestination());
  853. if (getSocketFactory() == null) {
  854. p.put("SocketFactory", strNull);
  855. } else {
  856. p.put("SocketFactory", getSocketFactory());
  857. }
  858. if (getSSLProperties() == null) {
  859. p.put("SSLProperties", strNull);
  860. } else {
  861. p.put("SSLProperties", getSSLProperties());
  862. }
  863. return p;
  864. }
  865. /**
  866. * Sets the Custom WebSocket Headers for the WebSocket Connection.
  867. *
  868. * @param headers
  869. * The custom websocket headers {@link Properties}
  870. */
  871. public void setCustomWebSocketHeaders(Map<String, String> headers) {
  872. this.customWebSocketHeaders = Collections.unmodifiableMap(headers);
  873. }
  874. public Map<String, String> getCustomWebSocketHeaders() {
  875. return customWebSocketHeaders;
  876. }
  877. public String toString() {
  878. return Debug.dumpProperties(getDebug(), "Connection options");
  879. }
  880. public boolean isSendReasonMessages() {
  881. return sendReasonMessages;
  882. }
  883. public void setSendReasonMessages(boolean sendReasonMessages) {
  884. this.sendReasonMessages = sendReasonMessages;
  885. }
  886. public int getExecutorServiceTimeout() {
  887. return executorServiceTimeout;
  888. }
  889. /**
  890. * Set the time in seconds that the executor service should wait when
  891. * terminating before forcefully terminating. It is not recommended to change
  892. * this value unless you are absolutely sure that you need to.
  893. *
  894. * @param executorServiceTimeout the time in seconds to wait when shutting down.Ï
  895. */
  896. public void setExecutorServiceTimeout(int executorServiceTimeout) {
  897. this.executorServiceTimeout = executorServiceTimeout;
  898. }
  899. }