|
|
|
@ -19,8 +19,11 @@
|
|
|
|
|
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
|
|
|
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.eclipse.jetty.http.HttpStatus;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.EnrollmentManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
|
|
|
|
@ -28,11 +31,13 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.Communica
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
@ -78,86 +83,91 @@ public class AgentUtilOperations {
|
|
|
|
|
try {
|
|
|
|
|
ClassLoader loader = AgentUtilOperations.class.getClassLoader();
|
|
|
|
|
URL path = loader.getResource(propertiesFileName);
|
|
|
|
|
System.out.println(path);
|
|
|
|
|
rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
|
|
|
|
|
|
rootPath = URLDecoder.decode(rootPath, StandardCharsets.UTF_8.toString());
|
|
|
|
|
agentManager.setRootPath(rootPath);
|
|
|
|
|
|
|
|
|
|
String deviceConfigFilePath = rootPath + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
propertiesInputStream = new FileInputStream(deviceConfigFilePath);
|
|
|
|
|
|
|
|
|
|
//load a properties file from class path, inside static method
|
|
|
|
|
properties.load(propertiesInputStream);
|
|
|
|
|
|
|
|
|
|
iotServerConfigs.setTenantDomain(properties.getProperty(
|
|
|
|
|
AgentConstants.TENANT_DOMAIN));
|
|
|
|
|
iotServerConfigs.setDeviceOwner(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_OWNER_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDeviceId(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_ID_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDeviceName(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_NAME_PROPERTY));
|
|
|
|
|
iotServerConfigs.setControllerContext(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_CONTROLLER_CONTEXT_PROPERTY));
|
|
|
|
|
iotServerConfigs.setScepContext(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_SCEP_CONTEXT_PROPERTY));
|
|
|
|
|
iotServerConfigs.setHTTPS_ServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.SERVER_HTTPS_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setHTTP_ServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.SERVER_HTTP_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setApimGatewayEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.APIM_GATEWAY_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setMqttBrokerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.MQTT_BROKER_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setXmppServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.XMPP_SERVER_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setXmppServerName(properties.getProperty(
|
|
|
|
|
AgentConstants.XMPP_SERVER_NAME_PROPERTY));
|
|
|
|
|
iotServerConfigs.setApiApplicationKey(properties.getProperty(
|
|
|
|
|
AgentConstants.API_APPLICATION_KEY));
|
|
|
|
|
iotServerConfigs.setAuthMethod(properties.getProperty(
|
|
|
|
|
AgentConstants.AUTH_METHOD_PROPERTY));
|
|
|
|
|
iotServerConfigs.setAuthToken(properties.getProperty(
|
|
|
|
|
AgentConstants.AUTH_TOKEN_PROPERTY));
|
|
|
|
|
iotServerConfigs.setRefreshToken(properties.getProperty(
|
|
|
|
|
AgentConstants.REFRESH_TOKEN_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDataPushInterval(Integer.parseInt(properties.getProperty(
|
|
|
|
|
AgentConstants.PUSH_INTERVAL_PROPERTY)));
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Tenant Domain: " +
|
|
|
|
|
iotServerConfigs.getTenantDomain());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Owner: " +
|
|
|
|
|
iotServerConfigs.getDeviceOwner());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device ID: " + iotServerConfigs.getDeviceId());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Name: " +
|
|
|
|
|
iotServerConfigs.getDeviceName());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Controller Context: " +
|
|
|
|
|
iotServerConfigs.getControllerContext());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTPS EndPoint: " +
|
|
|
|
|
iotServerConfigs.getHTTPS_ServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTP EndPoint: " +
|
|
|
|
|
iotServerConfigs.getHTTP_ServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "API-Manager Gateway EndPoint: " +
|
|
|
|
|
iotServerConfigs.getApimGatewayEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "MQTT Broker EndPoint: " +
|
|
|
|
|
iotServerConfigs.getMqttBrokerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server EndPoint: " +
|
|
|
|
|
iotServerConfigs.getXmppServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " +
|
|
|
|
|
iotServerConfigs.getAuthMethod());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Base64Encoded API Application Key: " +
|
|
|
|
|
iotServerConfigs.getApiApplicationKey());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " +
|
|
|
|
|
iotServerConfigs.getAuthToken());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " +
|
|
|
|
|
iotServerConfigs.getRefreshToken());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Data Push Interval: " +
|
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
|
iotServerConfigs.getXmppServerName());
|
|
|
|
|
|
|
|
|
|
if (path != null) {
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + path);
|
|
|
|
|
rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
|
|
|
|
|
|
rootPath = URLDecoder.decode(rootPath, StandardCharsets.UTF_8.toString());
|
|
|
|
|
agentManager.setRootPath(rootPath);
|
|
|
|
|
|
|
|
|
|
String deviceConfigFilePath = rootPath + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
propertiesInputStream = new FileInputStream(deviceConfigFilePath);
|
|
|
|
|
|
|
|
|
|
//load a properties file from class path, inside static method
|
|
|
|
|
properties.load(propertiesInputStream);
|
|
|
|
|
|
|
|
|
|
iotServerConfigs.setTenantDomain(properties.getProperty(
|
|
|
|
|
AgentConstants.TENANT_DOMAIN));
|
|
|
|
|
iotServerConfigs.setDeviceOwner(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_OWNER_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDeviceId(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_ID_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDeviceName(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_NAME_PROPERTY));
|
|
|
|
|
iotServerConfigs.setControllerContext(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_CONTROLLER_CONTEXT_PROPERTY));
|
|
|
|
|
iotServerConfigs.setScepContext(properties.getProperty(
|
|
|
|
|
AgentConstants.DEVICE_SCEP_CONTEXT_PROPERTY));
|
|
|
|
|
iotServerConfigs.setHTTPS_ServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.SERVER_HTTPS_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setHTTP_ServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.SERVER_HTTP_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setApimGatewayEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.APIM_GATEWAY_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setMqttBrokerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.MQTT_BROKER_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setXmppServerEndpoint(properties.getProperty(
|
|
|
|
|
AgentConstants.XMPP_SERVER_EP_PROPERTY));
|
|
|
|
|
iotServerConfigs.setXmppServerName(properties.getProperty(
|
|
|
|
|
AgentConstants.XMPP_SERVER_NAME_PROPERTY));
|
|
|
|
|
iotServerConfigs.setApiApplicationKey(properties.getProperty(
|
|
|
|
|
AgentConstants.API_APPLICATION_KEY));
|
|
|
|
|
iotServerConfigs.setAuthMethod(properties.getProperty(
|
|
|
|
|
AgentConstants.AUTH_METHOD_PROPERTY));
|
|
|
|
|
iotServerConfigs.setAuthToken(properties.getProperty(
|
|
|
|
|
AgentConstants.AUTH_TOKEN_PROPERTY));
|
|
|
|
|
iotServerConfigs.setRefreshToken(properties.getProperty(
|
|
|
|
|
AgentConstants.REFRESH_TOKEN_PROPERTY));
|
|
|
|
|
iotServerConfigs.setDataPushInterval(Integer.parseInt(properties.getProperty(
|
|
|
|
|
AgentConstants.PUSH_INTERVAL_PROPERTY)));
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Tenant Domain: " +
|
|
|
|
|
iotServerConfigs.getTenantDomain());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Owner: " +
|
|
|
|
|
iotServerConfigs.getDeviceOwner());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device ID: " + iotServerConfigs.getDeviceId());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Name: " +
|
|
|
|
|
iotServerConfigs.getDeviceName());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Controller Context: " +
|
|
|
|
|
iotServerConfigs.getControllerContext());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTPS EndPoint: " +
|
|
|
|
|
iotServerConfigs.getHTTPS_ServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTP EndPoint: " +
|
|
|
|
|
iotServerConfigs.getHTTP_ServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "API-Manager Gateway EndPoint: " +
|
|
|
|
|
iotServerConfigs.getApimGatewayEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "MQTT Broker EndPoint: " +
|
|
|
|
|
iotServerConfigs.getMqttBrokerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server EndPoint: " +
|
|
|
|
|
iotServerConfigs.getXmppServerEndpoint());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " +
|
|
|
|
|
iotServerConfigs.getAuthMethod());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Base64Encoded API Application Key: " +
|
|
|
|
|
iotServerConfigs.getApiApplicationKey());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " +
|
|
|
|
|
iotServerConfigs.getAuthToken());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " +
|
|
|
|
|
iotServerConfigs.getRefreshToken());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Data Push Interval: " +
|
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
|
iotServerConfigs.getXmppServerName());
|
|
|
|
|
} else {
|
|
|
|
|
throw new AgentCoreOperationException(
|
|
|
|
|
"Failed to load path of resource [" + propertiesFileName + "] from this classpath.");
|
|
|
|
|
}
|
|
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
|
String errorMsg = "[" + propertiesFileName + "] file not found at: " + rootPath;
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
@ -216,6 +226,26 @@ public class AgentUtilOperations {
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Push-Data API EndPoint: " + pushDataEndPointURL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setHTTPSConfigurations() {
|
|
|
|
|
String apimEndpoint = AgentManager.getInstance().getAgentConfigs().getApimGatewayEndpoint();
|
|
|
|
|
System.setProperty("javax.net.ssl.trustStore", AgentConstants.DEVICE_KEYSTORE);
|
|
|
|
|
System.setProperty("javax.net.ssl.trustStorePassword", AgentConstants.DEVICE_KEYSTORE_PASSWORD);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final String apimHost = TransportUtils.getHostAndPort(apimEndpoint).get(AgentConstants.HOST_PROPERTY);
|
|
|
|
|
|
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
|
|
|
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
|
|
|
return hostname.equals(apimHost);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
|
"Failed to set HTTPS HostNameVerifier to the APIMServer-Host using the APIM-Endpoint " +
|
|
|
|
|
"string [" + apimEndpoint + "].");
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String prepareSecurePayLoad(String message) throws AgentCoreOperationException {
|
|
|
|
|
PrivateKey devicePrivateKey = EnrollmentManager.getInstance().getPrivateKey();
|
|
|
|
@ -234,7 +264,6 @@ public class AgentUtilOperations {
|
|
|
|
|
jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload);
|
|
|
|
|
//below statements are temporary fix.
|
|
|
|
|
jsonPayload.put(JSON_SERIAL_KEY, EnrollmentManager.getInstance().getSCEPCertificate().getSerialNumber());
|
|
|
|
|
|
|
|
|
|
return jsonPayload.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -282,26 +311,19 @@ public class AgentUtilOperations {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean refreshOAuthToken() throws AgentCoreOperationException {
|
|
|
|
|
public static void refreshOAuthToken() throws AgentCoreOperationException {
|
|
|
|
|
|
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
|
|
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint() + "/token";
|
|
|
|
|
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint();
|
|
|
|
|
tokenEndpoint = tokenEndpoint + APIManagerTokenUtils.TOKEN_ENDPOINT;
|
|
|
|
|
|
|
|
|
|
HttpURLConnection httpConnection = null;
|
|
|
|
|
BufferedReader connectionBuffer = null;
|
|
|
|
|
String requestPayload;
|
|
|
|
|
String dataFromBuffer;
|
|
|
|
|
StringBuilder responseMessage = new StringBuilder();
|
|
|
|
|
boolean refreshStatus = false;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
httpConnection = TransportUtils.getHttpConnection(tokenEndpoint);
|
|
|
|
|
httpConnection.setRequestMethod(AgentConstants.HTTP_POST);
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.AUTHORIZATION_HEADER,
|
|
|
|
|
"Bearer " + agentManager.getAgentConfigs().getApiApplicationKey());
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.CONTENT_TYPE_HEADER, AgentConstants.X_WWW_FORM_URLENCODED);
|
|
|
|
|
httpConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
String refreshToken = agentManager.getAgentConfigs().getRefreshToken();
|
|
|
|
|
String applicationScope = "device_type_" + AgentConstants.DEVICE_TYPE +
|
|
|
|
|
" device_" + agentManager.getAgentConfigs().getDeviceId();
|
|
|
|
@ -310,6 +332,13 @@ public class AgentUtilOperations {
|
|
|
|
|
APIManagerTokenUtils.REFRESH_TOKEN + "=" + refreshToken + "&" +
|
|
|
|
|
APIManagerTokenUtils.SCOPE + "=" + applicationScope;
|
|
|
|
|
|
|
|
|
|
httpConnection = TransportUtils.getHttpConnection(tokenEndpoint);
|
|
|
|
|
httpConnection.setRequestMethod(AgentConstants.HTTP_POST);
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.AUTHORIZATION_HEADER,
|
|
|
|
|
"Basic " + agentManager.getAgentConfigs().getApiApplicationKey());
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.CONTENT_TYPE_HEADER, AgentConstants.X_WWW_FORM_URLENCODED);
|
|
|
|
|
httpConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
DataOutputStream dataOutPutWriter = new DataOutputStream(httpConnection.getOutputStream());
|
|
|
|
|
dataOutPutWriter.writeBytes(requestPayload);
|
|
|
|
|
dataOutPutWriter.flush();
|
|
|
|
@ -320,17 +349,19 @@ public class AgentUtilOperations {
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + httpConnection.getResponseCode() + ":" +
|
|
|
|
|
httpConnection.getResponseMessage() + "] was received for token refresh attempt.");
|
|
|
|
|
|
|
|
|
|
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
|
|
|
|
if (httpConnection.getResponseCode() == HttpStatus.OK_200) {
|
|
|
|
|
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
|
|
|
|
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
|
|
|
|
|
responseMessage.append(dataFromBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
|
|
|
|
|
responseMessage.append(dataFromBuffer);
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER +
|
|
|
|
|
"Response " + responseMessage + " was received for the token refresh call.");
|
|
|
|
|
updateExistingTokens(responseMessage.toString());
|
|
|
|
|
} else {
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "There was an issue with refreshing the Access Token.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + responseMessage +
|
|
|
|
|
"] was received for the token refresh call.");
|
|
|
|
|
|
|
|
|
|
refreshStatus = updateExistingTokens(responseMessage.toString());
|
|
|
|
|
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
throw new AgentCoreOperationException(e);
|
|
|
|
|
} catch (ProtocolException e) {
|
|
|
|
@ -359,20 +390,18 @@ public class AgentUtilOperations {
|
|
|
|
|
httpConnection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return refreshStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean updateExistingTokens(String responseFromTokenEP) {
|
|
|
|
|
private static void updateExistingTokens(String responseFromTokenEP) throws AgentCoreOperationException {
|
|
|
|
|
JSONObject jsonTokenObject = new JSONObject(responseFromTokenEP);
|
|
|
|
|
String newAccessToken = jsonTokenObject.get(APIManagerTokenUtils.ACCESS_TOKEN).toString();
|
|
|
|
|
String newRefreshToken = jsonTokenObject.get(APIManagerTokenUtils.REFRESH_TOKEN).toString();
|
|
|
|
|
|
|
|
|
|
if (newAccessToken == null || newRefreshToken == null) {
|
|
|
|
|
log.error(
|
|
|
|
|
AgentConstants.LOG_APPENDER + "Neither Access-Token nor Refresh-Token was found in the response [" +
|
|
|
|
|
responseFromTokenEP + "].");
|
|
|
|
|
return false;
|
|
|
|
|
String msg =
|
|
|
|
|
"Neither Access-Token nor Refresh-Token was found in the response [" + responseFromTokenEP + "].";
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + msg);
|
|
|
|
|
throw new AgentCoreOperationException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AgentManager.getInstance().getAgentConfigs().setAuthToken(newAccessToken);
|
|
|
|
@ -380,42 +409,21 @@ public class AgentUtilOperations {
|
|
|
|
|
String deviceConfigFilePath =
|
|
|
|
|
AgentManager.getInstance().getRootPath() + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
|
|
Properties deviceProperties = new Properties();
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream = new FileOutputStream(deviceConfigFilePath);
|
|
|
|
|
deviceProperties.setProperty(AgentConstants.AUTH_TOKEN_PROPERTY, newAccessToken);
|
|
|
|
|
deviceProperties.setProperty(AgentConstants.REFRESH_TOKEN_PROPERTY, newRefreshToken);
|
|
|
|
|
deviceProperties.store(fileOutputStream, null);
|
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
|
String errorMsg =
|
|
|
|
|
"[" + AgentConstants.AGENT_PROPERTIES_FILE_NAME + "] file not found at: " + deviceConfigFilePath;
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
String errorMsg = "Error occurred whilst trying to write to [" + AgentConstants.AGENT_PROPERTIES_FILE_NAME +
|
|
|
|
|
"] at: " + deviceConfigFilePath;
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
if (fileOutputStream != null) {
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
|
"Error occurred whilst trying to close InputStream resource used to read the '" +
|
|
|
|
|
AgentConstants.AGENT_PROPERTIES_FILE_NAME + "' file");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PropertiesConfiguration propertyFileConfiguration = new PropertiesConfiguration(deviceConfigFilePath);
|
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.AUTH_TOKEN_PROPERTY, newAccessToken);
|
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.REFRESH_TOKEN_PROPERTY, newRefreshToken);
|
|
|
|
|
propertyFileConfiguration.save();
|
|
|
|
|
} catch (ConfigurationException e) {
|
|
|
|
|
String msg = "Error occurred whilst trying to update the [" + AgentConstants.AGENT_PROPERTIES_FILE_NAME +
|
|
|
|
|
"] at: " + deviceConfigFilePath + " will the new tokens.";
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + msg);
|
|
|
|
|
throw new AgentCoreOperationException(msg);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class APIManagerTokenUtils {
|
|
|
|
|
public static final String TOKEN_ENDPOINT = "/oauth2/token";
|
|
|
|
|
public static final String GRANT_TYPE = "grant_type";
|
|
|
|
|
public static final String ACCESS_TOKEN = "access_token";
|
|
|
|
|
public static final String REFRESH_TOKEN = "refresh_token";
|
|
|
|
|