use of org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence in project openhab1-addons by openhab.
the class MqttBrokerConnection method openConnection.
/**
* Open an MQTT client connection.
*
* @throws Exception
*/
private void openConnection() throws Exception {
if (client != null && client.isConnected()) {
return;
}
if (StringUtils.isBlank(url)) {
throw new Exception("Missing url");
}
if (client == null) {
if (StringUtils.isBlank(clientId) || clientId.length() > 23) {
clientId = MqttClient.generateClientId();
}
String tmpDir = System.getProperty("java.io.tmpdir") + "/" + name;
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
logger.debug("Creating new client for '{}' using id '{}' and file store '{}'", url, clientId, tmpDir);
client = new MqttClient(url, clientId, dataStore);
client.setCallback(this);
}
MqttConnectOptions options = new MqttConnectOptions();
if (!StringUtils.isBlank(user)) {
options.setUserName(user);
}
if (!StringUtils.isBlank(password)) {
options.setPassword(password.toCharArray());
}
if (url.toLowerCase().contains("ssl")) {
if (StringUtils.isNotBlank(System.getProperty("com.ibm.ssl.protocol"))) {
// get all com.ibm.ssl properties from the system properties
// and set them as the SSL properties to use.
Properties sslProps = new Properties();
addSystemProperty("com.ibm.ssl.protocol", sslProps);
addSystemProperty("com.ibm.ssl.contextProvider", sslProps);
addSystemProperty("com.ibm.ssl.keyStore", sslProps);
addSystemProperty("com.ibm.ssl.keyStorePassword", sslProps);
addSystemProperty("com.ibm.ssl.keyStoreType", sslProps);
addSystemProperty("com.ibm.ssl.keyStoreProvider", sslProps);
addSystemProperty("com.ibm.ssl.trustStore", sslProps);
addSystemProperty("com.ibm.ssl.trustStorePassword", sslProps);
addSystemProperty("com.ibm.ssl.trustStoreType", sslProps);
addSystemProperty("com.ibm.ssl.trustStoreProvider", sslProps);
addSystemProperty("com.ibm.ssl.enabledCipherSuites", sslProps);
addSystemProperty("com.ibm.ssl.keyManager", sslProps);
addSystemProperty("com.ibm.ssl.trustManager", sslProps);
options.setSSLProperties(sslProps);
} else {
// use standard JSSE available in the runtime and
// use TLSv1.2 which is the default for a secured mosquitto
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, new TrustManager[] { getVeryTrustingTrustManager() }, new java.security.SecureRandom());
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
options.setSocketFactory(socketFactory);
}
}
if (lastWill != null) {
options.setWill(lastWill.getTopic(), lastWill.getPayload(), lastWill.getQos(), lastWill.isRetain());
}
options.setKeepAliveInterval(keepAliveInterval);
client.connect(options);
}
Aggregations