Changes to the VirtualFirealarm Agent + the API to enable token refresh upon expiration

revert-dabc3590
Shabirmean 9 years ago
parent 344232fc32
commit 75914aee4e

@ -23,12 +23,12 @@ import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.http.HTTPTransportHandler;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core.AgentConstants; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core.AgentConstants;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core.AgentManager; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core.AgentManager;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.http.HTTPTransportHandler;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -191,21 +191,19 @@ public class FireAlarmHTTPCommunicator extends HTTPTransportHandler {
private void executeDataPush(String pushDataPayload) { private void executeDataPush(String pushDataPayload) {
AgentManager agentManager = AgentManager.getInstance(); AgentManager agentManager = AgentManager.getInstance();
int responseCode = -1;
String pushDataEndPointURL = agentManager.getPushDataAPIEP(); String pushDataEndPointURL = agentManager.getPushDataAPIEP();
HttpURLConnection httpConnection = null; HttpURLConnection httpConnection;
int responseCode = -1;
try { try {
httpConnection = TransportUtils.getHttpConnection(agentManager.getPushDataAPIEP()); httpConnection = TransportUtils.getHttpConnection(agentManager.getPushDataAPIEP());
httpConnection.setRequestMethod(AgentConstants.HTTP_POST); httpConnection.setRequestMethod(AgentConstants.HTTP_POST);
httpConnection.setRequestProperty("Authorization", "Bearer " + httpConnection.setRequestProperty("Authorization",
agentManager.getAgentConfigs().getAuthToken()); "Bearer " + agentManager.getAgentConfigs().getAuthToken());
httpConnection.setRequestProperty("Content-Type", httpConnection.setRequestProperty("Content-Type", AgentConstants.APPLICATION_JSON);
AgentConstants.APPLICATION_JSON_TYPE);
httpConnection.setDoOutput(true); httpConnection.setDoOutput(true);
DataOutputStream dataOutPutWriter = new DataOutputStream( DataOutputStream dataOutPutWriter = new DataOutputStream(httpConnection.getOutputStream());
httpConnection.getOutputStream());
dataOutPutWriter.writeBytes(pushDataPayload); dataOutPutWriter.writeBytes(pushDataPayload);
dataOutPutWriter.flush(); dataOutPutWriter.flush();
dataOutPutWriter.close(); dataOutPutWriter.close();
@ -225,39 +223,34 @@ public class FireAlarmHTTPCommunicator extends HTTPTransportHandler {
} catch (IOException exception) { } catch (IOException exception) {
String errorMsg = String errorMsg =
"An IO error occurred whilst trying to get the response code from: " + "An IO error occurred whilst trying to get the response code from: " +
pushDataEndPointURL + " for a " + AgentConstants.HTTP_POST + pushDataEndPointURL + " for a " + AgentConstants.HTTP_POST + " method.";
" " + "method.";
log.error(AgentConstants.LOG_APPENDER + errorMsg); log.error(AgentConstants.LOG_APPENDER + errorMsg);
} catch (TransportHandlerException exception) { } catch (TransportHandlerException exception) {
log.error(AgentConstants.LOG_APPENDER + log.error(AgentConstants.LOG_APPENDER +
"Error encountered whilst trying to create HTTP-Connection " + "Error encountered whilst trying to create HTTP-Connection to IoT-Server EP at: " +
"to IoT-Server EP at: " +
pushDataEndPointURL); pushDataEndPointURL);
} }
if (responseCode == HttpStatus.CONFLICT_409 || if (responseCode == HttpStatus.CONFLICT_409 ||
responseCode == HttpStatus.PRECONDITION_FAILED_412) { responseCode == HttpStatus.PRECONDITION_FAILED_412) {
log.warn(AgentConstants.LOG_APPENDER + log.warn(AgentConstants.LOG_APPENDER +
"DeviceIP is being Re-Registered due to Push-Data failure " + "DeviceIP is being Re-Registered due to Push-Data failure with response code: " +
"with response code: " +
responseCode); responseCode);
registerThisDevice(); registerThisDevice();
} else if (responseCode != HttpStatus.NO_CONTENT_204) { } else if (responseCode != HttpStatus.NO_CONTENT_204) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.error(AgentConstants.LOG_APPENDER + "Status Code: " + responseCode + log.error(AgentConstants.LOG_APPENDER + "Status Code: " + responseCode +
" encountered whilst trying to Push-Device-Data to IoT " + " encountered whilst trying to Push-Device-Data to IoT Server at: " +
"Server at: " +
agentManager.getPushDataAPIEP()); agentManager.getPushDataAPIEP());
} }
agentManager.updateAgentStatus(AgentConstants.SERVER_NOT_RESPONDING); agentManager.updateAgentStatus(AgentConstants.SERVER_NOT_RESPONDING);
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(AgentConstants.LOG_APPENDER + "Push-Data call with payload - " + log.debug(AgentConstants.LOG_APPENDER + "Push-Data call with payload - " + pushDataPayload +
pushDataPayload + ", to IoT Server returned status " + ", to IoT Server returned status " + responseCode);
responseCode);
} }
} }
@ -272,16 +265,14 @@ public class FireAlarmHTTPCommunicator extends HTTPTransportHandler {
closeConnection(); closeConnection();
} catch (Exception e) { } catch (Exception e) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.warn(AgentConstants.LOG_APPENDER + log.warn(AgentConstants.LOG_APPENDER + "Unable to 'STOP' HTTP server at port: " + port);
"Unable to 'STOP' HTTP server at port: " + port);
} }
try { try {
Thread.sleep(timeoutInterval); Thread.sleep(timeoutInterval);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
log.error(AgentConstants.LOG_APPENDER + log.error(
"HTTP-Termination: Thread Sleep Interrupt " + AgentConstants.LOG_APPENDER + "HTTP-Termination: Thread Sleep Interrupt Exception");
"Exception");
} }
} }
} }
@ -398,8 +389,7 @@ public class FireAlarmHTTPCommunicator extends HTTPTransportHandler {
} catch (TransportHandlerException e) { } catch (TransportHandlerException e) {
String errorMsg = String errorMsg =
"Protocol specific error occurred when trying to fetch an HTTPConnection to:" + "Protocol specific error occurred when trying to fetch an HTTPConnection to:" +
" " + " " + registerEndpointURLString;
registerEndpointURLString;
log.error(AgentConstants.LOG_APPENDER + errorMsg); log.error(AgentConstants.LOG_APPENDER + errorMsg);
throw new AgentCoreOperationException(); throw new AgentCoreOperationException();
} }
@ -419,8 +409,7 @@ public class FireAlarmHTTPCommunicator extends HTTPTransportHandler {
} catch (IOException exception) { } catch (IOException exception) {
String errorMsg = "An IO error occurred whilst trying to get the response code from:" + String errorMsg = "An IO error occurred whilst trying to get the response code from:" +
" " + " " + registerEndpointURLString + " for a " + AgentConstants.HTTP_POST + " method.";
registerEndpointURLString + " for a " + AgentConstants.HTTP_POST + " method.";
log.error(AgentConstants.LOG_APPENDER + errorMsg); log.error(AgentConstants.LOG_APPENDER + errorMsg);
throw new AgentCoreOperationException(errorMsg, exception); throw new AgentCoreOperationException(errorMsg, exception);
} }

@ -35,7 +35,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
//TODO:: Lincense heade, comments and SPECIFIC class name since its not generic //TODO:: Lincence header, comments and SPECIFIC class name since its not generic
public class FireAlarmMQTTCommunicator extends MQTTTransportHandler { public class FireAlarmMQTTCommunicator extends MQTTTransportHandler {
private static final Log log = LogFactory.getLog(FireAlarmMQTTCommunicator.class); private static final Log log = LogFactory.getLog(FireAlarmMQTTCommunicator.class);

@ -35,6 +35,7 @@ public class AgentConfiguration {
private String apimGatewayEndpoint; private String apimGatewayEndpoint;
private String mqttBrokerEndpoint; private String mqttBrokerEndpoint;
private String xmppServerEndpoint; private String xmppServerEndpoint;
private String apiApplicationKey;
private String authMethod; private String authMethod;
private String authToken; private String authToken;
private String refreshToken; private String refreshToken;
@ -121,6 +122,14 @@ public class AgentConfiguration {
this.xmppServerEndpoint = xmppServerEndpoint; this.xmppServerEndpoint = xmppServerEndpoint;
} }
public String getApiApplicationKey() {
return apiApplicationKey;
}
public void setApiApplicationKey(String apiApplicationKey) {
this.apiApplicationKey = apiApplicationKey;
}
public String getAuthMethod() { public String getAuthMethod() {
return authMethod; return authMethod;
} }

@ -23,6 +23,7 @@ public class AgentConstants {
public static final String LOG_APPENDER = "AGENT_LOG:: "; public static final String LOG_APPENDER = "AGENT_LOG:: ";
public static final String PROPERTIES_FILE_PATH = ""; public static final String PROPERTIES_FILE_PATH = "";
public static final int DEFAULT_RETRY_THREAD_INTERVAL = 5000; // time in millis public static final int DEFAULT_RETRY_THREAD_INTERVAL = 5000; // time in millis
public static final String TOKEN_AUTHENTICATION_METHOD = "token";
/* --------------------------------------------------------------------------------------- /* ---------------------------------------------------------------------------------------
IoT-Server specific information IoT-Server specific information
--------------------------------------------------------------------------------------- */ --------------------------------------------------------------------------------------- */
@ -34,18 +35,23 @@ public class AgentConstants {
"{\"owner\":\"%s\",\"deviceId\":\"%s\",\"reply\":\"%s\",\"value\":\"%s\"}"; "{\"owner\":\"%s\",\"deviceId\":\"%s\",\"reply\":\"%s\",\"value\":\"%s\"}";
public static final String PUSH_SIMULATION_DATA_PAYLOAD = public static final String PUSH_SIMULATION_DATA_PAYLOAD =
"{\"owner\":\"%s\",\"deviceId\":\"%s\",\"reply\":\"%s\",\"value\":\"%s\",\"isSimulated\":\"%s\",\"duration\":\"%s\",\"frequency\":\"%s\"}"; "{\"owner\":\"%s\",\"deviceId\":\"%s\",\"reply\":\"%s\",\"value\":\"%s\",\"isSimulated\":\"%s\"," +
"\"duration\":\"%s\",\"frequency\":\"%s\"}";
public static final String AGENT_CONTROL_APP_EP = "/devicemgt/device/%s?id=%s"; public static final String AGENT_CONTROL_APP_EP = "/devicemgt/device/%s?id=%s";
public static final String DEVICE_DETAILS_PAGE_EP = "/devicemgt/device/%s?id=%s"; public static final String DEVICE_DETAILS_PAGE_EP = "/devicemgt/device/%s?id=%s";
public static final String DEVICE_ANALYTICS_PAGE_URL = "/devicemgt/device/virtual_firealarm/analytics?deviceId=%s&deviceName=%s"; public static final String DEVICE_ANALYTICS_PAGE_URL =
"/devicemgt/device/virtual_firealarm/analytics?deviceId=%s&deviceName=%s";
/* --------------------------------------------------------------------------------------- /* ---------------------------------------------------------------------------------------
HTTP Connection specific information for communicating with IoT-Server HTTP Connection specific information for communicating with IoT-Server
--------------------------------------------------------------------------------------- */ --------------------------------------------------------------------------------------- */
public static final String HTTP_POST = "POST"; public static final String HTTP_POST = "POST";
public static final String HTTP_GET = "GET"; public static final String HTTP_GET = "GET";
public static final String APPLICATION_JSON_TYPE = "application/json"; public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String CONTENT_TYPE_HEADER = "Content-Type";
public static final String APPLICATION_JSON = "application/json";
public static final String X_WWW_FORM_URLENCODED = "x-www-form-urlencoded";
public static final String REGISTERED = "Registered"; public static final String REGISTERED = "Registered";
public static final String NOT_REGISTERED = "Not-Registered"; public static final String NOT_REGISTERED = "Not-Registered";
public static final String REGISTRATION_FAILED = "Registration Failed"; public static final String REGISTRATION_FAILED = "Registration Failed";
@ -59,10 +65,7 @@ public class AgentConstants {
public static final int DEFAULT_MQTT_QUALITY_OF_SERVICE = 0; public static final int DEFAULT_MQTT_QUALITY_OF_SERVICE = 0;
public static final String MQTT_SUBSCRIBE_TOPIC = "%s/" + DEVICE_TYPE + "/%s"; public static final String MQTT_SUBSCRIBE_TOPIC = "%s/" + DEVICE_TYPE + "/%s";
public static final String MQTT_PUBLISH_TOPIC = "%s/" + DEVICE_TYPE + "/%s/publisher"; public static final String MQTT_PUBLISH_TOPIC = "%s/" + DEVICE_TYPE + "/%s/publisher";
/* ---------------------------------------------------------------------------------------
XMPP Connection specific information
--------------------------------------------------------------------------------------- */
public static final String XMPP_ADMIN_ACCOUNT_UNAME = "admin";
/* --------------------------------------------------------------------------------------- /* ---------------------------------------------------------------------------------------
Device/Agent specific properties to be read from the 'deviceConfig.properties' file Device/Agent specific properties to be read from the 'deviceConfig.properties' file
--------------------------------------------------------------------------------------- */ --------------------------------------------------------------------------------------- */
@ -79,6 +82,7 @@ public class AgentConstants {
public static final String MQTT_BROKER_EP_PROPERTY = "mqtt-ep"; public static final String MQTT_BROKER_EP_PROPERTY = "mqtt-ep";
public static final String XMPP_SERVER_EP_PROPERTY = "xmpp-ep"; public static final String XMPP_SERVER_EP_PROPERTY = "xmpp-ep";
public static final String XMPP_SERVER_NAME_PROPERTY = "xmpp-server-name"; public static final String XMPP_SERVER_NAME_PROPERTY = "xmpp-server-name";
public static final String API_APPLICATION_KEY = "application-key";
public static final String AUTH_METHOD_PROPERTY = "auth-method"; public static final String AUTH_METHOD_PROPERTY = "auth-method";
public static final String AUTH_TOKEN_PROPERTY = "auth-token"; public static final String AUTH_TOKEN_PROPERTY = "auth-token";
public static final String REFRESH_TOKEN_PROPERTY = "refresh-token"; public static final String REFRESH_TOKEN_PROPERTY = "refresh-token";

@ -20,14 +20,14 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.http.FireAlarmHTTPCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.mqtt.FireAlarmMQTTCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.xmpp.FireAlarmXMPPCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.EnrollmentManager; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.EnrollmentManager;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandler; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandler;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.http.FireAlarmHTTPCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.mqtt.FireAlarmMQTTCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.communication.xmpp.FireAlarmXMPPCommunicator;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.virtual.VirtualHardwareManager; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.virtual.VirtualHardwareManager;
import java.util.ArrayList; import java.util.ArrayList;
@ -223,6 +223,10 @@ public class AgentManager {
this.rootPath = rootPath; this.rootPath = rootPath;
} }
public String getRootPath() {
return rootPath;
}
public void setDeviceReady(boolean deviceReady) { public void setDeviceReady(boolean deviceReady) {
this.deviceReady = deviceReady; this.deviceReady = deviceReady;
} }
@ -324,6 +328,7 @@ public class AgentManager {
/** /**
* Get temperature reading from device * Get temperature reading from device
*
* @return Temperature * @return Temperature
*/ */
public int getTemperature() { public int getTemperature() {
@ -332,9 +337,10 @@ public class AgentManager {
/** /**
* Get humidity reading from device * Get humidity reading from device
*
* @return Humidity * @return Humidity
*/ */
public int getHumidity(){ public int getHumidity() {
return VirtualHardwareManager.getInstance().getHumidity(); return VirtualHardwareManager.getInstance().getHumidity();
} }

@ -26,12 +26,18 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.Enrollme
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.CommunicationUtils; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.CommunicationUtils;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
import java.io.File; import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -67,18 +73,19 @@ public class AgentUtilOperations {
Properties properties = new Properties(); Properties properties = new Properties();
InputStream propertiesInputStream = null; InputStream propertiesInputStream = null;
String propertiesFileName = AgentConstants.AGENT_PROPERTIES_FILE_NAME; String propertiesFileName = AgentConstants.AGENT_PROPERTIES_FILE_NAME;
String rootPath = "";
try { try {
ClassLoader loader = AgentUtilOperations.class.getClassLoader(); ClassLoader loader = AgentUtilOperations.class.getClassLoader();
URL path = loader.getResource(propertiesFileName); URL path = loader.getResource(propertiesFileName);
System.out.println(path); System.out.println(path);
String root = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "") rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
.replace("jar:", "").replace("file:", ""); .replace("jar:", "").replace("file:", "");
root = URLDecoder.decode(root, StandardCharsets.UTF_8.toString()); rootPath = URLDecoder.decode(rootPath, StandardCharsets.UTF_8.toString());
agentManager.setRootPath(root); agentManager.setRootPath(rootPath);
String deviceConfigFilePath = root + AgentConstants.AGENT_PROPERTIES_FILE_NAME; String deviceConfigFilePath = rootPath + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
propertiesInputStream = new FileInputStream(deviceConfigFilePath); propertiesInputStream = new FileInputStream(deviceConfigFilePath);
//load a properties file from class path, inside static method //load a properties file from class path, inside static method
@ -108,6 +115,8 @@ public class AgentUtilOperations {
AgentConstants.XMPP_SERVER_EP_PROPERTY)); AgentConstants.XMPP_SERVER_EP_PROPERTY));
iotServerConfigs.setXmppServerName(properties.getProperty( iotServerConfigs.setXmppServerName(properties.getProperty(
AgentConstants.XMPP_SERVER_NAME_PROPERTY)); AgentConstants.XMPP_SERVER_NAME_PROPERTY));
iotServerConfigs.setApiApplicationKey(properties.getProperty(
AgentConstants.API_APPLICATION_KEY));
iotServerConfigs.setAuthMethod(properties.getProperty( iotServerConfigs.setAuthMethod(properties.getProperty(
AgentConstants.AUTH_METHOD_PROPERTY)); AgentConstants.AUTH_METHOD_PROPERTY));
iotServerConfigs.setAuthToken(properties.getProperty( iotServerConfigs.setAuthToken(properties.getProperty(
@ -138,6 +147,8 @@ public class AgentUtilOperations {
iotServerConfigs.getXmppServerEndpoint()); iotServerConfigs.getXmppServerEndpoint());
log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " + log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " +
iotServerConfigs.getAuthMethod()); iotServerConfigs.getAuthMethod());
log.info(AgentConstants.LOG_APPENDER + "Base64Encoded API Application Key: " +
iotServerConfigs.getApiApplicationKey());
log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " + log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " +
iotServerConfigs.getAuthToken()); iotServerConfigs.getAuthToken());
log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " + log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " +
@ -148,7 +159,7 @@ public class AgentUtilOperations {
iotServerConfigs.getXmppServerName()); iotServerConfigs.getXmppServerName());
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
String errorMsg = "[" + propertiesFileName + "] file not found at: " + AgentConstants.PROPERTIES_FILE_PATH; String errorMsg = "[" + propertiesFileName + "] file not found at: " + rootPath;
log.error(AgentConstants.LOG_APPENDER + errorMsg); log.error(AgentConstants.LOG_APPENDER + errorMsg);
throw new AgentCoreOperationException(errorMsg); throw new AgentCoreOperationException(errorMsg);
@ -174,10 +185,6 @@ public class AgentUtilOperations {
/** /**
* This method constructs the URLs for each of the API Endpoints called by the device agent * This method constructs the URLs for each of the API Endpoints called by the device agent
* Ex: Register API, Push-Data API * Ex: Register API, Push-Data API
*
* @throws AgentCoreOperationException if any error occurs at socket level whilst trying to
* retrieve the deviceIP of the network-interface read
* from the configs file
*/ */
public static void initializeServerEndPoints() { public static void initializeServerEndPoints() {
AgentManager agentManager = AgentManager.getInstance(); AgentManager agentManager = AgentManager.getInstance();
@ -265,6 +272,155 @@ public class AgentUtilOperations {
return actualMessage; return actualMessage;
} }
public static String getAuthenticationMethod() {
String authMethod = AgentManager.getInstance().getAgentConfigs().getAuthMethod();
switch (authMethod) {
case AgentConstants.TOKEN_AUTHENTICATION_METHOD:
return AgentConstants.TOKEN_AUTHENTICATION_METHOD;
default:
return "";
}
}
public static boolean refreshOAuthToken() throws AgentCoreOperationException {
AgentManager agentManager = AgentManager.getInstance();
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint() + "/token";
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();
requestPayload = APIManagerTokenUtils.GRANT_TYPE + "=" + APIManagerTokenUtils.REFRESH_TOKEN + "&" +
APIManagerTokenUtils.REFRESH_TOKEN + "=" + refreshToken + "&" +
APIManagerTokenUtils.SCOPE + "=" + applicationScope;
DataOutputStream dataOutPutWriter = new DataOutputStream(httpConnection.getOutputStream());
dataOutPutWriter.writeBytes(requestPayload);
dataOutPutWriter.flush();
dataOutPutWriter.close();
log.info(AgentConstants.LOG_APPENDER + "Request to refresh OAuth token was sent to [" +
httpConnection.getURL() + "] with payload [" + requestPayload + "].");
log.info(AgentConstants.LOG_APPENDER + "Response [" + httpConnection.getResponseCode() + ":" +
httpConnection.getResponseMessage() + "] was received for token refresh attempt.");
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
responseMessage.append(dataFromBuffer);
}
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) {
String errorMsg = "Protocol specific error occurred when trying to set method to " +
AgentConstants.HTTP_POST + " for endpoint at: " + tokenEndpoint;
log.error(AgentConstants.LOG_APPENDER + errorMsg);
throw new AgentCoreOperationException(errorMsg, e);
} catch (IOException e) {
String errorMsg = "An IO error occurred whilst trying to get the response code from: " + tokenEndpoint +
" for a HTTP " + AgentConstants.HTTP_POST + " call.";
log.error(AgentConstants.LOG_APPENDER + errorMsg);
throw new AgentCoreOperationException(errorMsg, e);
} finally {
if (connectionBuffer != null) {
try {
connectionBuffer.close();
} catch (IOException e) {
log.error(AgentConstants.LOG_APPENDER +
"Error encounter whilst attempting to close buffer to connection at: " +
tokenEndpoint);
}
}
if (httpConnection != null) {
httpConnection.disconnect();
}
}
return refreshStatus;
}
private static boolean updateExistingTokens(String responseFromTokenEP) {
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;
}
AgentManager.getInstance().getAgentConfigs().setAuthToken(newAccessToken);
AgentManager.getInstance().getAgentConfigs().setRefreshToken(newRefreshToken);
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");
}
}
}
return true;
}
private class APIManagerTokenUtils {
public static final String GRANT_TYPE = "grant_type";
public static final String ACCESS_TOKEN = "access_token";
public static final String REFRESH_TOKEN = "refresh_token";
public static final String SCOPE = "scope";
}
} }

@ -1,4 +1,4 @@
# ad#
# Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. # Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");

@ -54,13 +54,27 @@ import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.*; import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.util.*; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
@ -136,7 +150,8 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE)); deviceIdentifiers.add(new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE));
APIUtil.getDeviceManagementService().addOperation(VirtualFireAlarmConstants.DEVICE_TYPE, commandOp, deviceIdentifiers); APIUtil.getDeviceManagementService().addOperation(VirtualFireAlarmConstants.DEVICE_TYPE, commandOp,
deviceIdentifiers);
break; break;
} }
return Response.ok().build(); return Response.ok().build();
@ -319,14 +334,16 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
scopes); scopes);
String accessToken = accessTokenInfo.getAccessToken(); String accessToken = accessTokenInfo.getAccessToken();
String refreshToken = accessTokenInfo.getRefreshToken(); String refreshToken = accessTokenInfo.getRefreshToken();
//adding registering data
boolean status;
if (XmppConfig.getInstance().isEnabled()) {
XmppAccount newXmppAccount = new XmppAccount(); XmppAccount newXmppAccount = new XmppAccount();
newXmppAccount.setAccountName(deviceId); newXmppAccount.setAccountName(deviceId);
newXmppAccount.setUsername(deviceId); newXmppAccount.setUsername(deviceId);
newXmppAccount.setPassword(accessToken); newXmppAccount.setPassword(accessToken);
newXmppAccount.setEmail(deviceId + "@" + APIUtil.getTenantDomainOftheUser()); newXmppAccount.setEmail(deviceId + "@" + APIUtil.getTenantDomainOftheUser());
boolean status;
if (XmppConfig.getInstance().isEnabled()) {
status = XmppServerClient.createAccount(newXmppAccount); status = XmppServerClient.createAccount(newXmppAccount);
if (!status) { if (!status) {
String msg = "XMPP Account was not created for device - " + deviceId + " of owner - " + owner + String msg = "XMPP Account was not created for device - " + deviceId + " of owner - " + owner +
@ -335,14 +352,16 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
throw new DeviceManagementException(msg); throw new DeviceManagementException(msg);
} }
} }
status = register(deviceId, deviceName); status = register(deviceId, deviceName);
if (!status) { if (!status) {
String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner; String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner;
throw new DeviceManagementException(msg); throw new DeviceManagementException(msg);
} }
ZipUtil ziputil = new ZipUtil(); ZipUtil ziputil = new ZipUtil();
return ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId, return ziputil.createZipFile(owner, sketchType, deviceId, deviceName, apiApplicationKey.toString(),
deviceName, accessToken, refreshToken); accessToken, refreshToken);
} }
private static String shortUUID() { private static String shortUUID() {
@ -350,5 +369,4 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong(); long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
return Long.toString(l, Character.MAX_RADIX); return Long.toString(l, Character.MAX_RADIX);
} }
} }

@ -0,0 +1,20 @@
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig;
public class VirtualFireAlarmUtilConstants {
public static final String TENANT_DOMAIN = "TENANT_DOMAIN";
public static final String DEVICE_OWNER = "DEVICE_OWNER";
public static final String DEVICE_ID = "DEVICE_ID";
public static final String DEVICE_NAME = "DEVICE_NAME";
public static final String HTTPS_EP = "HTTPS_EP";
public static final String HTTP_EP = "HTTP_EP";
public static final String APIM_EP = "APIM_EP";
public static final String MQTT_EP = "MQTT_EP";
public static final String XMPP_EP = "XMPP_EP";
public static final String API_APPLICATION_KEY = "API_APPLICATION_KEY";
public static final String DEVICE_TOKEN = "DEVICE_TOKEN";
public static final String DEVICE_REFRESH_TOKEN = "DEVICE_REFRESH_TOKEN";
public static final String SERVER_NAME = "SERVER_NAME";
}

@ -18,6 +18,11 @@
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util; package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.util.Utils; import org.wso2.carbon.device.mgt.iot.util.Utils;
import org.wso2.carbon.device.mgt.iot.util.ZipArchive; import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
@ -35,6 +40,7 @@ import java.util.Map;
*/ */
public class ZipUtil { public class ZipUtil {
private static final Log log = LogFactory.getLog(ZipUtil.class);
private static final String HTTPS_PORT_PROPERTY = "httpsPort"; private static final String HTTPS_PORT_PROPERTY = "httpsPort";
private static final String HTTP_PORT_PROPERTY = "httpPort"; private static final String HTTP_PORT_PROPERTY = "httpPort";
@ -42,12 +48,13 @@ public class ZipUtil {
private static final String HTTPS_PROTOCOL_APPENDER = "https://"; private static final String HTTPS_PROTOCOL_APPENDER = "https://";
private static final String HTTP_PROTOCOL_APPENDER = "http://"; private static final String HTTP_PROTOCOL_APPENDER = "http://";
public ZipArchive createZipFile(String owner, String tenantDomain, String deviceType, public ZipArchive createZipFile(String owner, String deviceType, String deviceId, String deviceName,
String deviceId, String deviceName, String token, String apiApplicationKey, String token, String refreshToken)
String refreshToken) throws DeviceManagementException { throws DeviceManagementException {
String sketchFolder = "repository" + File.separator + "resources" + File.separator + "sketches"; String sketchFolder = "repository" + File.separator + "resources" + File.separator + "sketches";
String archivesPath = CarbonUtils.getCarbonHome() + File.separator + sketchFolder + File.separator + "archives" + String archivesPath =
CarbonUtils.getCarbonHome() + File.separator + sketchFolder + File.separator + "archives" +
File.separator + deviceId; File.separator + deviceId;
String templateSketchPath = sketchFolder + File.separator + deviceType; String templateSketchPath = sketchFolder + File.separator + deviceType;
String iotServerIP; String iotServerIP;
@ -63,24 +70,29 @@ public class ZipUtil {
if (mqttEndpoint.contains(LOCALHOST)) { if (mqttEndpoint.contains(LOCALHOST)) {
mqttEndpoint = mqttEndpoint.replace(LOCALHOST, iotServerIP); mqttEndpoint = mqttEndpoint.replace(LOCALHOST, iotServerIP);
} }
String xmppEndpoint = XmppConfig.getInstance().getXmppServerIP() + ":" + String xmppEndpoint = XmppConfig.getInstance().getXmppServerIP() + ":" +
XmppConfig.getInstance().getXmppServerPort(); XmppConfig.getInstance().getXmppServerPort();
if (xmppEndpoint.contains(LOCALHOST)) { if (xmppEndpoint.contains(LOCALHOST)) {
xmppEndpoint = xmppEndpoint.replace(LOCALHOST, iotServerIP); xmppEndpoint = xmppEndpoint.replace(LOCALHOST, iotServerIP);
} }
String base64EncodedApplicationKey = getBase64EncodedAPIAppKey(apiApplicationKey);
Map<String, String> contextParams = new HashMap<>(); Map<String, String> contextParams = new HashMap<>();
contextParams.put("TENANT_DOMAIN", APIUtil.getTenantDomainOftheUser()); contextParams.put(VirtualFireAlarmUtilConstants.TENANT_DOMAIN, APIUtil.getTenantDomainOftheUser());
contextParams.put("DEVICE_OWNER", owner); contextParams.put(VirtualFireAlarmUtilConstants.DEVICE_OWNER, owner);
contextParams.put("DEVICE_ID", deviceId); contextParams.put(VirtualFireAlarmUtilConstants.DEVICE_ID, deviceId);
contextParams.put("DEVICE_NAME", deviceName); contextParams.put(VirtualFireAlarmUtilConstants.DEVICE_NAME, deviceName);
contextParams.put("HTTPS_EP", httpsServerEP); contextParams.put(VirtualFireAlarmUtilConstants.HTTPS_EP, httpsServerEP);
contextParams.put("HTTP_EP", httpServerEP); contextParams.put(VirtualFireAlarmUtilConstants.HTTP_EP, httpServerEP);
contextParams.put("APIM_EP", apimEndpoint); contextParams.put(VirtualFireAlarmUtilConstants.APIM_EP, apimEndpoint);
contextParams.put("MQTT_EP", mqttEndpoint); contextParams.put(VirtualFireAlarmUtilConstants.MQTT_EP, mqttEndpoint);
contextParams.put("XMPP_EP", "XMPP:" + xmppEndpoint); contextParams.put(VirtualFireAlarmUtilConstants.XMPP_EP, "XMPP:" + xmppEndpoint);
contextParams.put("DEVICE_TOKEN", token); contextParams.put(VirtualFireAlarmUtilConstants.API_APPLICATION_KEY, base64EncodedApplicationKey);
contextParams.put("DEVICE_REFRESH_TOKEN", refreshToken); contextParams.put(VirtualFireAlarmUtilConstants.DEVICE_TOKEN, token);
contextParams.put("SERVER_NAME", XmppConfig.getInstance().getXmppServerName()); contextParams.put(VirtualFireAlarmUtilConstants.DEVICE_REFRESH_TOKEN, refreshToken);
contextParams.put(VirtualFireAlarmUtilConstants.SERVER_NAME, XmppConfig.getInstance().getXmppServerName());
ZipArchive zipFile; ZipArchive zipFile;
zipFile = Utils.getSketchArchive(archivesPath, templateSketchPath, contextParams, deviceName); zipFile = Utils.getSketchArchive(archivesPath, templateSketchPath, contextParams, deviceName);
return zipFile; return zipFile;
@ -88,4 +100,13 @@ public class ZipUtil {
throw new DeviceManagementException("Zip File Creation Failed", e); throw new DeviceManagementException("Zip File Creation Failed", e);
} }
} }
private String getBase64EncodedAPIAppKey(String apiAppCredentialsAsJSONString) {
JSONObject jsonObject = new JSONObject(apiAppCredentialsAsJSONString);
String consumerKey = jsonObject.get(ApiApplicationConstants.OAUTH_CLIENT_ID).toString();
String consumerSecret = jsonObject.get(ApiApplicationConstants.OAUTH_CLIENT_SECRET).toString();
String stringToEncode = consumerKey + ":" + consumerSecret;
return Base64.encodeBase64String(stringToEncode.getBytes());
}
} }

@ -28,6 +28,7 @@ apim-ep=${APIM_EP}
mqtt-ep=${MQTT_EP} mqtt-ep=${MQTT_EP}
xmpp-ep=${XMPP_EP} xmpp-ep=${XMPP_EP}
auth-method=token auth-method=token
application-key=${API_APPLICATION_KEY}
auth-token=${DEVICE_TOKEN} auth-token=${DEVICE_TOKEN}
refresh-token=${DEVICE_REFRESH_TOKEN} refresh-token=${DEVICE_REFRESH_TOKEN}
push-interval=15 push-interval=15

Loading…
Cancel
Save