/******************************************************************************* * Copyright (c) 2009, 2017 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * https://www.eclipse.org/legal/epl-2.0 * and the Eclipse Distribution License is available at * https://www.eclipse.org/org/documents/edl-v10.php * * Contributors: * Dave Locke - initial API and implementation and/or initial documentation * Ian Craggs - MQTT 3.1.1 support * James Sutton - Automatic Reconnect & Offline Buffering * James Sutton - MQTT v5 */ package org.eclipse.paho.mqttv5.client; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; import javax.net.SocketFactory; import javax.net.ssl.HostnameVerifier; import org.eclipse.paho.mqttv5.client.internal.NetworkModuleService; import org.eclipse.paho.mqttv5.client.util.Debug; import org.eclipse.paho.mqttv5.common.MqttMessage; import org.eclipse.paho.mqttv5.common.packet.MqttProperties; import org.eclipse.paho.mqttv5.common.packet.UserProperty; import org.eclipse.paho.mqttv5.common.util.MqttTopicValidator; /** * Holds the set of options that control how the client connects to a server. * * Constructs a new {@link MqttConnectionOptions} object using the default * values. * * The defaults are: *
null
if LWT is not set.
* @see #setWill(String, MqttMessage)
*/
public String getWillDestination() {
return willDestination;
}
/**
* Returns the message to be sent as last will and testament (LWT). The returned
* object is "read only". Calling any "setter" methods on the returned object
* will result in an IllegalStateException
being thrown.
*
* @return the message to use, or null
if LWT is not set.
*/
public MqttMessage getWillMessage() {
return willMessage;
}
/**
* Sets the "Last Will and Testament" (LWT) for the connection. In the event
* that this client unexpectedly looses it's connection to the server, the
* server will publish a message to itself using the supplied details.
*
* @param topic
* the topic to publish to.
* @param message
* the {@link MqttMessage} to send
*/
public void setWill(String topic, MqttMessage message) {
if (topic == null || message == null || message.getPayload() == null) {
throw new IllegalArgumentException();
}
MqttTopicValidator.validate(topic, false, true); // Wildcards are not allowed
this.willDestination = topic;
this.willMessage = message;
// Prevent any more changes to the will message
this.willMessage.setMutable(false);
}
/**
* Returns whether the client and server should remember state for the client
* across reconnects.
*
* @return the clean session flag
*/
public boolean isCleanStart() {
return this.cleanStart;
}
/**
* Sets whether the client and server should remember state across restarts and
* reconnects. If set to false, the server will retain the session state until
* either:
* * The default value is 60 seconds *
* * @param keepAliveInterval * the interval, measured in seconds, must be >= 0. * @throws IllegalArgumentException * if the keepAliveInterval was invalid */ public void setKeepAliveInterval(int keepAliveInterval) { if (keepAliveInterval < 0) { throw new IllegalArgumentException(); } this.keepAliveInterval = keepAliveInterval; } /** * Returns the connection timeout value. * * @see #setConnectionTimeout(int) * @return the connection timeout value. */ public int getConnectionTimeout() { return connectionTimeout; } /** * Sets the connection timeout value. This value, measured in seconds, defines * the maximum time interval the client will wait for the network connection to * the MQTT server to be established. The default timeout is 30 seconds. A value * of 0 disables timeout processing meaning the client will wait until the * network connection is made successfully or fails. * * @param connectionTimeout * the timeout value, measured in seconds. It must be >0; */ public void setConnectionTimeout(int connectionTimeout) { if (connectionTimeout < 0) { throw new IllegalArgumentException(); } this.connectionTimeout = connectionTimeout; } /** * Get the maximum time (in millis) to wait between reconnects * * @return Get the maximum time (in millis) to wait between reconnects */ public int getMaxReconnectDelay() { return maxReconnectDelay; } /** * Set the maximum time to wait between reconnects * * @param maxReconnectDelay * the duration (in millis) */ public void setMaxReconnectDelay(int maxReconnectDelay) { this.maxReconnectDelay = maxReconnectDelay; } /** * Return a list of serverURIs the client may connect to * * @return the serverURIs or null if not set */ public String[] getServerURIs() { return serverURIs; } /** * Set a list of one or more serverURIs the client may connect to. *
* Each serverURI
specifies the address of a server that the client
* may connect to. Two types of connection are supported tcp://
for
* a TCP connection and ssl://
for a TCP connection secured by
* SSL/TLS. For example:
*
tcp://localhost:1883
ssl://localhost:8883
tcp://
" URIs, and 8883 for ssl://
URIs.
* * If serverURIs is set then it overrides the serverURI parameter passed in on * the constructor of the MQTT client. *
* When an attempt to connect is initiated the client will start with the first * serverURI in the list and work through the list until a connection is * established with a server. If a connection cannot be made to any of the * servers then the connect attempt fails. *
* Specifying a list of servers that a client may connect to has several uses: *
* Some MQTT servers support a high availability feature where two or more * "equal" MQTT servers share state. An MQTT client can connect to any of the * "equal" servers and be assured that messages are reliably delivered and * durable subscriptions are maintained no matter which server the client * connects to. *
** The cleanStart flag must be set to false if durable subscriptions and/or * reliable message delivery is required. *
** A set of servers may be specified that are not "equal" (as in the high * availability option). As no state is shared across the servers reliable * message delivery and durable subscriptions are not valid. The cleanStart flag * must be set to true if the hunt list mode is used *
*null
, this means the
* session will not expire.
*
* @return the Session Expiry Interval in seconds.
*/
public Long getSessionExpiryInterval() {
return sessionExpiryInterval;
}
/**
* Sets the Session Expiry Interval. This value, measured in seconds, defines
* the maximum time that the broker will maintain the session for once the
* client disconnects. Clients should only connect with a long Session Expiry
* interval if they intend to connect to the server at some later point in time.
*
* null
, it will default to
* 65,535.
*
* @return the Receive Maximum
*/
public Integer getReceiveMaximum() {
return receiveMaximum;
}
/**
* Sets the Receive Maximum. This value represents the limit of QoS 1 and QoS 2
* publications that the client is willing to process concurrently. There is no
* mechanism to limit the number of QoS 0 publications that the Server might try
* to send.
* null
then this value defaults to 65,535.null
, no limit is imposed.
*
* @return the Maximum Packet Size in bytes.
*/
public Long getMaximumPacketSize() {
return maximumPacketSize;
}
/**
* Sets the Maximum Packet Size. This value represents the Maximum Packet Size
* the client is willing to accept.
*
* null
then no limit is imposed beyond the
* limitations of the protocol.null
, the default value is
* 0.
*
* @return the Topic Alias Maximum.
*/
public Integer getTopicAliasMaximum() {
return topicAliasMaximum;
}
/**
* Sets the Topic Alias Maximum. This value if present represents the highest
* value that the Client will accept as a Topic Alias sent by the Server.
*
* null
, then it will default to to 0.null
, the default
* value is false.
*
* @return The Request Response Info Flag.
*/
public Boolean getRequestResponseInfo() {
return requestResponseInfo;
}
/**
* Sets the Request Response Info Flag.
* null
, then it will default to false.null
, the default
* value is true.
*
* @return the Request Problem Info flag.
*/
public Boolean getRequestProblemInfo() {
return requestProblemInfo;
}
/**
* Sets the Request Problem Info flag.
* null
, then it will default to true.null
, extended
* authentication is not performed.
*
* @return the Authentication Method.
*/
public String getAuthMethod() {
return authMethod;
}
/**
* Sets the Authentication Method. If set, this value contains the name of the
* authentication method to be used for extended authentication.
*
* If null
, extended authentication is not performed.
*
* @param authMethod
* The Authentication Method.
*/
public void setAuthMethod(String authMethod) {
this.authMethod = authMethod;
}
/**
* Returns the Authentication Data.
*
* @return the Authentication Data.
*/
public byte[] getAuthData() {
return authData;
}
/**
* Sets the Authentication Data. If set, this byte array contains the extended
* authentication data, defined by the Authenticated Method. It is a protocol
* error to include Authentication Data if there is no Authentication Method.
*
* @param authData
* The Authentication Data
*/
public void setAuthData(byte[] authData) {
this.authData = authData;
}
/**
* Returns the socket factory that will be used when connecting, or
* null
if one has not been set.
*
* @return The Socket Factory
*/
public SocketFactory getSocketFactory() {
return socketFactory;
}
/**
* Sets the SocketFactory
to use. This allows an application to
* apply its own policies around the creation of network sockets. If using an
* SSL connection, an SSLSocketFactory
can be used to supply
* application-specific security settings.
*
* @param socketFactory
* the factory to use.
*/
public void setSocketFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
/**
* Returns the SSL properties for the connection.
*
* @return the properties for the SSL connection
*/
public Properties getSSLProperties() {
return sslClientProps;
}
/**
* Sets the SSL properties for the connection.
* * Note that these properties are only valid if an implementation of the Java * Secure Socket Extensions (JSSE) is available. These properties are * not used if a SocketFactory has been set using * {@link #setSocketFactory(SocketFactory)}. The following properties can be * used: *
*com.ibm.micro.security.Password.obfuscate(char[] password)
. This
* obfuscates the password using a simple and insecure XOR and Base64 encoding
* mechanism. Note that this is only a simple scrambler to obfuscate clear-text
* passwords.com.ibm.micro.security.Password.obfuscate(char[] password)
. This
* obfuscates the password using a simple and insecure XOR and Base64 encoding
* mechanism. Note that this is only a simple scrambler to obfuscate clear-text
* passwords.* There is no default HostnameVerifier *
* * @param hostnameVerifier * the {@link HostnameVerifier} */ public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) { this.sslHostnameVerifier = hostnameVerifier; } /** * Returns whether to automatically assign subscription identifiers when * subscribing to a topic. * * @return if automatic assignment of subscription identifiers is enabled. */ public boolean useSubscriptionIdentifiers() { return useSubscriptionIdentifiers; } /** * Sets whether to automatically assign subscription identifiers when * subscribing to a topic. This will mean that if a subscription has a callback * associated with it then it is guaranteed to be called when an incoming * message has the correct subscription ID embedded. If disabled, then the * client will do best effort topic matching with all callbacks, however this * might result in an incorrect callback being called if there are multiple * subscriptions to topics using a combination of wildcards. * * @param useSubscriptionIdentifiers * Whether to enable automatic assignment of subscription * identifiers. */ public void setUseSubscriptionIdentifiers(boolean useSubscriptionIdentifiers) { this.useSubscriptionIdentifiers = useSubscriptionIdentifiers; } public boolean isHttpsHostnameVerificationEnabled() { return httpsHostnameVerificationEnabled; } public void setHttpsHostnameVerificationEnabled(boolean httpsHostnameVerificationEnabled) { this.httpsHostnameVerificationEnabled = httpsHostnameVerificationEnabled; } /** * @return The Debug Properties */ public Properties getDebug() { final String strNull = "null"; Properties p = new Properties(); p.put("MqttVersion", getMqttVersion()); p.put("CleanStart", Boolean.valueOf(isCleanStart())); p.put("ConTimeout", getConnectionTimeout()); p.put("KeepAliveInterval", getKeepAliveInterval()); p.put("UserName", (getUserName() == null) ? strNull : getUserName()); p.put("WillDestination", (getWillDestination() == null) ? strNull : getWillDestination()); if (getSocketFactory() == null) { p.put("SocketFactory", strNull); } else { p.put("SocketFactory", getSocketFactory()); } if (getSSLProperties() == null) { p.put("SSLProperties", strNull); } else { p.put("SSLProperties", getSSLProperties()); } return p; } /** * Sets the Custom WebSocket Headers for the WebSocket Connection. * * @param headers * The custom websocket headers {@link Properties} */ public void setCustomWebSocketHeaders(Map