|
|
@ -33,10 +33,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.authentication.AuthenticationInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.authentication.AuthenticationInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.authentication.OAuthAuthenticator;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.authentication.OAuthAuthenticator;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.constants.RemoteSessionConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.constants.RemoteSessionConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.dto.ClientSession;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.dto.RemoteSession;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.dto.DeviceSession;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.dto.common.RemoteSession;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionInvalidException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.internal.RemoteSessionManagementDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.extensions.remote.session.internal.RemoteSessionManagementDataHolder;
|
|
|
|
|
|
|
|
|
|
|
@ -53,21 +50,19 @@ import java.util.Map;
|
|
|
|
public class RemoteSessionManagementServiceImpl implements RemoteSessionManagementService {
|
|
|
|
public class RemoteSessionManagementServiceImpl implements RemoteSessionManagementService {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(RemoteSessionManagementServiceImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(RemoteSessionManagementServiceImpl.class);
|
|
|
|
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
|
|
|
private static final int MAX_BUFFER_SIZE = 640 * 1024;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void initializeSession(Session session, String deviceType, String deviceId, String operationId) throws
|
|
|
|
public void initializeSession(Session session, String deviceType, String deviceId, String operationId) throws
|
|
|
|
RemoteSessionInvalidException, RemoteSessionManagementException {
|
|
|
|
RemoteSessionManagementException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check whether required configurations are enabled
|
|
|
|
if (!RemoteSessionManagementDataHolder.getInstance().isEnabled()) {
|
|
|
|
if (!RemoteSessionManagementDataHolder.getInstance().isEnabled()) {
|
|
|
|
throw new RemoteSessionManagementException("Remote session feature is disabled.");
|
|
|
|
throw new RemoteSessionManagementException("Remote session feature is disabled.");
|
|
|
|
} else if (RemoteSessionManagementDataHolder.getInstance().getServerUrl() == null) {
|
|
|
|
} else if (RemoteSessionManagementDataHolder.getInstance().getServerUrl() == null) {
|
|
|
|
throw new RemoteSessionManagementException("Server url haven't been configured.");
|
|
|
|
throw new RemoteSessionManagementException("Server url has not been configured.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read Query Parameters for obtain the token
|
|
|
|
Map<String, List<String>> sessionQueryParam = new HashedMap();
|
|
|
|
Map<String, List<String>> sessionQueryParam = new HashedMap();
|
|
|
|
List<String> sessionQueryParamList = new LinkedList<>();
|
|
|
|
List<String> sessionQueryParamList = new LinkedList<>();
|
|
|
|
sessionQueryParamList.add(session.getQueryString());
|
|
|
|
sessionQueryParamList.add(session.getQueryString());
|
|
|
@ -78,64 +73,61 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
|
|
|
|
AuthenticationInfo authenticationInfo = oAuthAuthenticator.isAuthenticated(sessionQueryParam);
|
|
|
|
AuthenticationInfo authenticationInfo = oAuthAuthenticator.isAuthenticated(sessionQueryParam);
|
|
|
|
|
|
|
|
|
|
|
|
if (authenticationInfo != null && authenticationInfo.isAuthenticated()) {
|
|
|
|
if (authenticationInfo != null && authenticationInfo.isAuthenticated()) {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(authenticationInfo.getTenantDomain()
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(authenticationInfo
|
|
|
|
|
|
|
|
.getTenantDomain()
|
|
|
|
, true);
|
|
|
|
, true);
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(authenticationInfo.getUsername());
|
|
|
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(authenticationInfo.getUsername());
|
|
|
|
if (deviceId != null && !deviceId.isEmpty() && deviceType != null && !deviceType.isEmpty()) {
|
|
|
|
if (deviceId != null && !deviceId.isEmpty() && deviceType != null && !deviceType.isEmpty()) {
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
deviceIdentifier.setType(deviceType);
|
|
|
|
deviceIdentifier.setType(deviceType);
|
|
|
|
// Check authorization for user
|
|
|
|
|
|
|
|
|
|
|
|
// Check authorization of user for given device
|
|
|
|
boolean userAuthorized = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
boolean userAuthorized = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
.getDeviceAccessAuthorizationService()
|
|
|
|
.getDeviceAccessAuthorizationService()
|
|
|
|
.isUserAuthorized(deviceIdentifier, authenticationInfo.getUsername());
|
|
|
|
.isUserAuthorized(deviceIdentifier, authenticationInfo.getUsername());
|
|
|
|
if (userAuthorized) {
|
|
|
|
if (userAuthorized) {
|
|
|
|
log.info("Operation ID: " + operationId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set common settings for session
|
|
|
|
// set common settings for session
|
|
|
|
session.setMaxBinaryMessageBufferSize(MAX_BUFFER_SIZE);
|
|
|
|
session.setMaxBinaryMessageBufferSize(RemoteSessionConstants.MAX_BUFFER_SIZE);
|
|
|
|
session.setMaxTextMessageBufferSize(MAX_BUFFER_SIZE);
|
|
|
|
session.setMaxTextMessageBufferSize(RemoteSessionConstants.MAX_BUFFER_SIZE);
|
|
|
|
session.setMaxIdleTimeout(RemoteSessionManagementDataHolder.getInstance().getMaxIdleTimeout());
|
|
|
|
session.setMaxIdleTimeout(RemoteSessionManagementDataHolder.getInstance().getMaxIdleTimeout());
|
|
|
|
|
|
|
|
|
|
|
|
// if session initiated using operatiod id means request came from device
|
|
|
|
// if session initiated using operation id means request came from device
|
|
|
|
if (operationId != null) {
|
|
|
|
if (operationId != null) {
|
|
|
|
Session pendingSession = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
RemoteSession activeSession = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
.getDeviceRequestMap().get((authenticationInfo.getTenantDomain() + "/" + deviceType
|
|
|
|
.getActiveDeviceClientSessionMap().get((authenticationInfo.getTenantDomain() + "/" +
|
|
|
|
+ "/" + deviceId));
|
|
|
|
deviceType + "/" + deviceId));
|
|
|
|
|
|
|
|
if (activeSession != null) {
|
|
|
|
if (pendingSession != null) {
|
|
|
|
|
|
|
|
RemoteSession clientRemote = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
RemoteSession clientRemote = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
.getSessionMap().get(pendingSession.getId());
|
|
|
|
.getSessionMap().get(activeSession.getMySession().getId());
|
|
|
|
if (clientRemote != null) {
|
|
|
|
if (clientRemote != null) {
|
|
|
|
|
|
|
|
|
|
|
|
if (clientRemote.getOperationId().equals(operationId)) {
|
|
|
|
if (clientRemote.getOperationId().equals(operationId)) {
|
|
|
|
RemoteSession deviceRemote = new DeviceSession(session, authenticationInfo
|
|
|
|
RemoteSession deviceRemote = new RemoteSession(session, authenticationInfo
|
|
|
|
.getTenantDomain(), deviceType, deviceId, operationId);
|
|
|
|
.getTenantDomain(), deviceType, deviceId);
|
|
|
|
|
|
|
|
deviceRemote.setOperationId(operationId);
|
|
|
|
deviceRemote.setPeerSession(clientRemote);
|
|
|
|
deviceRemote.setPeerSession(clientRemote);
|
|
|
|
clientRemote.setPeerSession(deviceRemote);
|
|
|
|
clientRemote.setPeerSession(deviceRemote);
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().put(session
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().put(session
|
|
|
|
.getId(), deviceRemote);
|
|
|
|
.getId(), deviceRemote);
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getDeviceRequestMap().remove(
|
|
|
|
|
|
|
|
(authenticationInfo.getTenantDomain() + "/" + deviceType + "/" + deviceId));
|
|
|
|
|
|
|
|
// Send Remote connect response
|
|
|
|
// Send Remote connect response
|
|
|
|
JSONObject message = new JSONObject();
|
|
|
|
JSONObject message = new JSONObject();
|
|
|
|
message.put("code", RemoteSessionConstants.REMOTE_CONNECT);
|
|
|
|
message.put(RemoteSessionConstants.REMOTE_CONNECT_CODE, RemoteSessionConstants
|
|
|
|
message.put("operation_response", "connected");
|
|
|
|
.REMOTE_CONNECT);
|
|
|
|
deviceRemote.sendMessageToPeer(message.toString());
|
|
|
|
deviceRemote.sendMessageToPeer(message.toString());
|
|
|
|
|
|
|
|
log.info("Device session opened for session id: " + session.getId() +
|
|
|
|
|
|
|
|
" device Type : " + deviceType + " , " + "deviceId : " + deviceId);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionManagementException("Device and Operation information does" +
|
|
|
|
throw new RemoteSessionManagementException("Device and Operation information " +
|
|
|
|
" not matched with client information for operation id: " + operationId +
|
|
|
|
"does not matched with client information for operation id: " +
|
|
|
|
" device Type : " + deviceType + " , " + "deviceId : " +
|
|
|
|
operationId + " device Type : " + deviceType + " , " + "deviceId : " +
|
|
|
|
deviceId);
|
|
|
|
deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionManagementException("Device session is inactive for operation " +
|
|
|
|
throw new RemoteSessionManagementException("Device session is inactive for " +
|
|
|
|
"id: " + operationId + " device Type : " + deviceType + " , " + "deviceId : " +
|
|
|
|
"operation id: " + operationId + " device Type : " + deviceType + " , " +
|
|
|
|
deviceId);
|
|
|
|
"deviceId : " + deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -144,19 +136,34 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
|
|
|
|
"id: " + operationId + " device Type : " + deviceType + " , " + "deviceId : " +
|
|
|
|
"id: " + operationId + " device Type : " + deviceType + " , " + "deviceId : " +
|
|
|
|
deviceId);
|
|
|
|
deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
RemoteSession clientRemote = new RemoteSession(session, authenticationInfo
|
|
|
|
|
|
|
|
.getTenantDomain(), deviceType, deviceId);
|
|
|
|
// Create new remote control operation to start the session
|
|
|
|
// Create new remote control operation to start the session
|
|
|
|
Session pendingSession = RemoteSessionManagementDataHolder.getInstance().getDeviceRequestMap().get(
|
|
|
|
RemoteSession activeSession = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
(authenticationInfo.getTenantDomain() + "/" + deviceType + "/" + deviceId));
|
|
|
|
.getActiveDeviceClientSessionMap().putIfAbsent((authenticationInfo
|
|
|
|
if (pendingSession != null && pendingSession.isOpen()) {
|
|
|
|
.getTenantDomain() + "/" + deviceType + "/" + deviceId),
|
|
|
|
throw new RemoteSessionManagementException("Another client session waiting on device to connect.");
|
|
|
|
clientRemote);
|
|
|
|
|
|
|
|
if (activeSession != null && activeSession.getMySession().isOpen() && activeSession
|
|
|
|
|
|
|
|
.getPeerSession() == null) {
|
|
|
|
|
|
|
|
throw new RemoteSessionManagementException("Another client session waiting on device " +
|
|
|
|
|
|
|
|
"to connect.");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Session lastSession = RemoteSessionManagementDataHolder.getInstance().getDeviceRequestMap().putIfAbsent(
|
|
|
|
// if there is pending session exists but already closed, then we need to remove it.
|
|
|
|
(authenticationInfo.getTenantDomain() + "/" + deviceType + "/" + deviceId),
|
|
|
|
if (activeSession != null) {
|
|
|
|
session);
|
|
|
|
endSession(activeSession.getMySession(), "Remote session closed due to new session" +
|
|
|
|
|
|
|
|
" request");
|
|
|
|
|
|
|
|
// Use put if absent for adding session to waiting list since we need to overcome
|
|
|
|
|
|
|
|
// multithreaded session requests.
|
|
|
|
|
|
|
|
activeSession = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
|
|
|
|
.getActiveDeviceClientSessionMap().putIfAbsent((authenticationInfo
|
|
|
|
|
|
|
|
.getTenantDomain() + "/" + deviceType + "/" +
|
|
|
|
|
|
|
|
deviceId), clientRemote);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (lastSession == null) {
|
|
|
|
// If another client tried to start session same time then active session will be
|
|
|
|
|
|
|
|
// exist. So we are adding session request only no parallel sessions added to map
|
|
|
|
|
|
|
|
if (activeSession == null) {
|
|
|
|
|
|
|
|
|
|
|
|
// Create operation if session initiated by client
|
|
|
|
// Create operation if session initiated by client
|
|
|
|
Operation operation = new ConfigOperation();
|
|
|
|
Operation operation = new ConfigOperation();
|
|
|
@ -164,55 +171,58 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
|
|
|
|
operation.setEnabled(true);
|
|
|
|
operation.setEnabled(true);
|
|
|
|
operation.setControl(Operation.Control.NO_REPEAT);
|
|
|
|
operation.setControl(Operation.Control.NO_REPEAT);
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
payload.put("serverUrl", RemoteSessionManagementDataHolder.getInstance().getServerUrl());
|
|
|
|
payload.put("serverUrl", RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
|
|
|
|
.getServerUrl());
|
|
|
|
operation.setPayLoad(payload.toString());
|
|
|
|
operation.setPayLoad(payload.toString());
|
|
|
|
String date = new SimpleDateFormat(DATE_FORMAT_NOW).format(new Date());
|
|
|
|
String date = new SimpleDateFormat(RemoteSessionConstants.DATE_FORMAT_NOW).format
|
|
|
|
|
|
|
|
(new Date());
|
|
|
|
operation.setCreatedTimeStamp(date);
|
|
|
|
operation.setCreatedTimeStamp(date);
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
deviceIdentifiers.add(new DeviceIdentifier(deviceId, deviceType));
|
|
|
|
deviceIdentifiers.add(new DeviceIdentifier(deviceId, deviceType));
|
|
|
|
Activity activity = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
Activity activity = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
.getDeviceManagementProviderService().addOperation(deviceType, operation,
|
|
|
|
.getDeviceManagementProviderService().addOperation(deviceType, operation,
|
|
|
|
deviceIdentifiers);
|
|
|
|
deviceIdentifiers);
|
|
|
|
log.info("Activity id: " + activity.getActivityId());
|
|
|
|
clientRemote.setOperationId(activity.getActivityId()
|
|
|
|
|
|
|
|
.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, ""));
|
|
|
|
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().put(session.getId
|
|
|
|
|
|
|
|
(), clientRemote);
|
|
|
|
|
|
|
|
log.info("Client remote session opened for session id: " + session.getId() +
|
|
|
|
|
|
|
|
" device Type : " + deviceType + " , " + "deviceId : " + deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
RemoteSession clientRemote = new ClientSession(session, authenticationInfo
|
|
|
|
} else {
|
|
|
|
.getTenantDomain(), deviceType, deviceId, activity.getActivityId().replace(DeviceManagementConstants
|
|
|
|
throw new RemoteSessionManagementException("Another client session waiting on " +
|
|
|
|
.OperationAttributes.ACTIVITY, ""));
|
|
|
|
"device to connect.");
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().put(session.getId(), clientRemote);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Current session count: " + RemoteSessionManagementDataHolder
|
|
|
|
log.info("Current remote sessions count: " + RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
.getInstance().getSessionMap().size());
|
|
|
|
.getSessionMap().size());
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionInvalidException("Missing device Id or type ", new CloseReason
|
|
|
|
throw new RemoteSessionManagementException("Missing device Id or type ");
|
|
|
|
(CloseReason.CloseCodes.CANNOT_ACCEPT, "Missing device Id or device type "));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionInvalidException("Unauthorized Access for the device Type : " + deviceType
|
|
|
|
throw new RemoteSessionManagementException("Unauthorized Access for the device Type : " + deviceType
|
|
|
|
+ " , deviceId : " + deviceId, new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT,
|
|
|
|
+ " , deviceId : " + deviceId);
|
|
|
|
"Unauthorized Access"));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (OperationManagementException | InvalidDeviceException e) {
|
|
|
|
} catch (OperationManagementException | InvalidDeviceException e) {
|
|
|
|
throw new RemoteSessionManagementException("Error occurred while adding initial operation for the device Type : " +
|
|
|
|
throw new RemoteSessionManagementException("Error occurred while adding initial operation for the " +
|
|
|
|
deviceType + " , deviceId : " + deviceId, e);
|
|
|
|
"device Type : " + deviceType + " , deviceId : " + deviceId);
|
|
|
|
} catch (DeviceAccessAuthorizationException e) {
|
|
|
|
} catch (DeviceAccessAuthorizationException e) {
|
|
|
|
throw new RemoteSessionManagementException("Error occurred while device access authorization for the device Type : " +
|
|
|
|
throw new RemoteSessionManagementException("Error occurred while device access authorization for the " +
|
|
|
|
deviceType + " , " + "deviceId : " + deviceId, e);
|
|
|
|
"device Type : " + deviceType + " , " + "deviceId : " + deviceId);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionInvalidException("Invalid token", new CloseReason(CloseReason.CloseCodes
|
|
|
|
throw new RemoteSessionManagementException("Invalid token");
|
|
|
|
.CANNOT_ACCEPT, "Invalid token"));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void initializeSession(Session session, String deviceType, String deviceId) throws RemoteSessionInvalidException, RemoteSessionManagementException {
|
|
|
|
public void initializeSession(Session session, String deviceType, String deviceId) throws
|
|
|
|
|
|
|
|
RemoteSessionManagementException {
|
|
|
|
initializeSession(session, deviceType, deviceId, null);
|
|
|
|
initializeSession(session, deviceType, deviceId, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -221,22 +231,18 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param session Web socket RemoteSession
|
|
|
|
* @param session Web socket RemoteSession
|
|
|
|
* @param message String message needs to send to peer connection
|
|
|
|
* @param message String message needs to send to peer connection
|
|
|
|
* @throws RemoteSessionInvalidException throws when session cannot be made due to invalid data
|
|
|
|
* @throws RemoteSessionManagementException throws when session cannot be made due to invalid data
|
|
|
|
* @throws RemoteSessionManagementException throws when session has error with accessing device resources
|
|
|
|
* @throws RemoteSessionManagementException throws when session has error with accessing device resources
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void sendMessageToPeer(Session session, String message) throws RemoteSessionManagementException,
|
|
|
|
public void sendMessageToPeer(Session session, String message) throws RemoteSessionManagementException {
|
|
|
|
RemoteSessionInvalidException {
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(message);
|
|
|
|
JSONObject jsonObject = new JSONObject(message);
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().get(session.getId());
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().get(session
|
|
|
|
|
|
|
|
.getId());
|
|
|
|
if (remoteSession != null) {
|
|
|
|
if (remoteSession != null) {
|
|
|
|
if (remoteSession instanceof ClientSession) {
|
|
|
|
|
|
|
|
jsonObject.put("id", remoteSession.getOperationId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteSession.sendMessageToPeer(jsonObject.toString());
|
|
|
|
remoteSession.sendMessageToPeer(jsonObject.toString());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionInvalidException("Remote Session cannot be found ", new CloseReason(CloseReason
|
|
|
|
throw new RemoteSessionManagementException("Remote Session cannot be found ");
|
|
|
|
.CloseCodes.CANNOT_ACCEPT, "Invalid RemoteSession"));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -246,48 +252,68 @@ public class RemoteSessionManagementServiceImpl implements RemoteSessionManageme
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param session Web socket RemoteSession
|
|
|
|
* @param session Web socket RemoteSession
|
|
|
|
* @param message Byte message needs to send to peer connection
|
|
|
|
* @param message Byte message needs to send to peer connection
|
|
|
|
* @throws RemoteSessionInvalidException throws when session cannot be made due to invalid data
|
|
|
|
* @throws RemoteSessionManagementException throws when session cannot be made due to invalid data
|
|
|
|
* @throws RemoteSessionManagementException throws when session has error with accessing device resources
|
|
|
|
* @throws RemoteSessionManagementException throws when session has error with accessing device resources
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void sendMessageToPeer(Session session, byte[] message) throws RemoteSessionInvalidException,
|
|
|
|
public void sendMessageToPeer(Session session, byte[] message) throws RemoteSessionManagementException {
|
|
|
|
RemoteSessionManagementException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().get(session.getId());
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().get(session
|
|
|
|
|
|
|
|
.getId());
|
|
|
|
if (remoteSession != null) {
|
|
|
|
if (remoteSession != null) {
|
|
|
|
remoteSession.sendMessageToPeer(message);
|
|
|
|
remoteSession.sendMessageToPeer(message);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RemoteSessionInvalidException("Remote Session cannot be found ", new CloseReason(CloseReason
|
|
|
|
throw new RemoteSessionManagementException("Remote Session cannot be found ");
|
|
|
|
.CloseCodes.CANNOT_ACCEPT, "Invalid RemoteSession"));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Closing the session and cleanup the resources
|
|
|
|
* Closing the session and cleanup the resources
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param session Web socket RemoteSession
|
|
|
|
* @param session Web socket Remote Session
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void endSession(Session session) throws IOException {
|
|
|
|
public void endSession(Session session, String closeReason) {
|
|
|
|
|
|
|
|
log.info("Closing session: "+session.getId()+" due to:"+ closeReason);
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(session.getId());
|
|
|
|
RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(session
|
|
|
|
|
|
|
|
.getId());
|
|
|
|
if (remoteSession != null) {
|
|
|
|
if (remoteSession != null) {
|
|
|
|
String operationId = remoteSession.getOperationId();
|
|
|
|
String operationId = remoteSession.getOperationId();
|
|
|
|
Session peerSession = remoteSession.getPeerSession().getMySession();
|
|
|
|
if (remoteSession.getPeerSession() != null) {
|
|
|
|
if (peerSession != null) {
|
|
|
|
Session peerSession = remoteSession.getPeerSession().getMySession();
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(peerSession.getId());
|
|
|
|
if (peerSession != null) {
|
|
|
|
if (peerSession.isOpen()) {
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(peerSession.getId());
|
|
|
|
peerSession.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "Remote session closed"));
|
|
|
|
if (peerSession.isOpen()) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
peerSession.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, closeReason));
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.error("Failed to disconnect the client.", ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remoteSession.getMySession() != null) {
|
|
|
|
|
|
|
|
Session mySession = remoteSession.getMySession();
|
|
|
|
|
|
|
|
if (mySession.isOpen()) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
mySession.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, closeReason));
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.error("Failed to disconnect the client.", ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operationId != null) {
|
|
|
|
if (operationId != null) {
|
|
|
|
Session lastSession = RemoteSessionManagementDataHolder.getInstance().getDeviceRequestMap().get(
|
|
|
|
String deviceIdentifier = remoteSession.getTenantDomain() + "/" + remoteSession
|
|
|
|
(remoteSession.getTenantDomain() + "/" + remoteSession.getDeviceType() + "/" + remoteSession
|
|
|
|
.getDeviceType() + "/" + remoteSession.getDeviceId();
|
|
|
|
.getDeviceId()));
|
|
|
|
RemoteSession lastSession = RemoteSessionManagementDataHolder.getInstance()
|
|
|
|
if (lastSession != null && lastSession.getId().equals(session.getId())) {
|
|
|
|
.getActiveDeviceClientSessionMap().get(deviceIdentifier);
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getDeviceRequestMap().remove(
|
|
|
|
if (lastSession != null && lastSession.getMySession().getId().equals(session.getId())) {
|
|
|
|
(remoteSession.getTenantDomain() + "/" + remoteSession.getDeviceType() + "/" + remoteSession
|
|
|
|
RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap().remove
|
|
|
|
.getDeviceId()));
|
|
|
|
(deviceIdentifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|