|
|
@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
|
|
|
import org.apache.http.conn.HttpHostConnectException;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
@ -131,7 +132,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void startListener() throws MqttException {
|
|
|
|
public boolean startListener() throws MqttException {
|
|
|
|
if (this.mqttBrokerConnectionConfiguration.getUsername() != null &&
|
|
|
|
if (this.mqttBrokerConnectionConfiguration.getUsername() != null &&
|
|
|
|
this.mqttBrokerConnectionConfiguration.getDcrUrl() != null) {
|
|
|
|
this.mqttBrokerConnectionConfiguration.getDcrUrl() != null) {
|
|
|
|
String username = this.mqttBrokerConnectionConfiguration.getUsername();
|
|
|
|
String username = this.mqttBrokerConnectionConfiguration.getUsername();
|
|
|
@ -151,12 +152,12 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
|
|
|
registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE);
|
|
|
|
registrationProfile.setTokenScope(MQTTEventAdapterConstants.TOKEN_SCOPE);
|
|
|
|
if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
|
|
|
if (!mqttBrokerConnectionConfiguration.isGlobalCredentailSet()) {
|
|
|
|
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
|
|
|
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
|
|
|
+ mqttBrokerConnectionConfiguration.getAdapterName() +
|
|
|
|
+ mqttBrokerConnectionConfiguration.getAdapterName() +
|
|
|
|
"_" + tenantDomain);
|
|
|
|
"_" + tenantDomain);
|
|
|
|
registrationProfile.setIsSaasApp(false);
|
|
|
|
registrationProfile.setIsSaasApp(false);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
|
|
|
registrationProfile.setClientName(MQTTEventAdapterConstants.APPLICATION_NAME_PREFIX
|
|
|
|
+ mqttBrokerConnectionConfiguration.getAdapterName());
|
|
|
|
+ mqttBrokerConnectionConfiguration.getAdapterName());
|
|
|
|
registrationProfile.setIsSaasApp(true);
|
|
|
|
registrationProfile.setIsSaasApp(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String jsonString = registrationProfile.toJSON();
|
|
|
|
String jsonString = registrationProfile.toJSON();
|
|
|
@ -164,8 +165,8 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
|
|
|
postMethod.setEntity(requestEntity);
|
|
|
|
postMethod.setEntity(requestEntity);
|
|
|
|
String basicAuth = getBase64Encode(username, password);
|
|
|
|
String basicAuth = getBase64Encode(username, password);
|
|
|
|
postMethod.setHeader(new BasicHeader(MQTTEventAdapterConstants.AUTHORIZATION_HEADER_NAME,
|
|
|
|
postMethod.setHeader(new BasicHeader(MQTTEventAdapterConstants.AUTHORIZATION_HEADER_NAME,
|
|
|
|
MQTTEventAdapterConstants.AUTHORIZATION_HEADER_VALUE_PREFIX +
|
|
|
|
MQTTEventAdapterConstants.AUTHORIZATION_HEADER_VALUE_PREFIX +
|
|
|
|
basicAuth));
|
|
|
|
basicAuth));
|
|
|
|
HttpResponse httpResponse = httpClient.execute(postMethod);
|
|
|
|
HttpResponse httpResponse = httpClient.execute(postMethod);
|
|
|
|
if (httpResponse != null) {
|
|
|
|
if (httpResponse != null) {
|
|
|
|
String response = MQTTUtil.getResponseString(httpResponse);
|
|
|
|
String response = MQTTUtil.getResponseString(httpResponse);
|
|
|
@ -182,21 +183,29 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (HttpHostConnectException e) {
|
|
|
|
|
|
|
|
log.error("Keymanager is unreachable, Waiting....");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
log.error("Invalid dcrUrl : " + dcrUrlString);
|
|
|
|
log.error("Invalid dcrUrl : " + dcrUrlString);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (JWTClientException | UserStoreException e) {
|
|
|
|
} catch (JWTClientException | UserStoreException e) {
|
|
|
|
log.error("Failed to create an oauth token with jwt grant type.", e);
|
|
|
|
log.error("Failed to create an oauth token with jwt grant type.", e);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | IOException e) {
|
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | IOException e) {
|
|
|
|
log.error("Failed to create a http connection.", e);
|
|
|
|
log.error("Failed to create a http connection.", e);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
mqttClient.connect(connectionOptions);
|
|
|
|
mqttClient.connect(connectionOptions);
|
|
|
|
} catch (MqttException e) {
|
|
|
|
} catch (MqttException e) {
|
|
|
|
log.error("Broker is unreachable, Waiting.....");
|
|
|
|
log.warn("Broker is unreachable, Waiting.....");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mqttClient.subscribe(topic);
|
|
|
|
mqttClient.subscribe(topic);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -271,10 +280,14 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
|
|
|
|
while (!connectionSucceeded) {
|
|
|
|
while (!connectionSucceeded) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
connectionDuration = connectionDuration * MQTTEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR;
|
|
|
|
connectionDuration = connectionDuration * MQTTEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR;
|
|
|
|
|
|
|
|
if (connectionDuration > MQTTEventAdapterConstants.MAXIMUM_RECONNECTION_DURATION) {
|
|
|
|
|
|
|
|
connectionDuration = MQTTEventAdapterConstants.MAXIMUM_RECONNECTION_DURATION;
|
|
|
|
|
|
|
|
}
|
|
|
|
Thread.sleep(connectionDuration);
|
|
|
|
Thread.sleep(connectionDuration);
|
|
|
|
startListener();
|
|
|
|
if (startListener()) {
|
|
|
|
connectionSucceeded = true;
|
|
|
|
connectionSucceeded = true;
|
|
|
|
log.info("MQTT Connection successful");
|
|
|
|
log.info("MQTT Connection successful");
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
log.error("Interruption occurred while waiting for reconnection", e);
|
|
|
|
log.error("Interruption occurred while waiting for reconnection", e);
|
|
|
|
} catch (MqttException e) {
|
|
|
|
} catch (MqttException e) {
|
|
|
|