From 82f6736bce1413cabdb06e248e86f164a9fbf7b4 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Sun, 1 Oct 2017 10:57:26 +0530 Subject: [PATCH] fix concurrent accesss issue with remote session --- .../pom.xml | 2 +- .../ClientSessionSubscriptionEndpoint.java | 33 +-- .../DeviceSessionSubscriptionEndpoint.java | 30 +-- .../endpoint/SubscriptionEndpoint.java | 44 ++-- .../session/endpoint/constants/Constants.java | 3 + .../utils/HttpSessionConfigurator.java | 2 +- .../pom.xml | 4 +- .../RemoteSessionManagementService.java | 25 +- .../RemoteSessionManagementServiceImpl.java | 226 ++++++++++-------- .../authentication/AuthenticationInfo.java | 2 +- .../authentication/OAuthAuthenticator.java | 2 +- .../oauth/OAuthTokenValidator.java | 2 +- .../oauth/OAuthTokenValidatorStubFactory.java | 3 +- .../OAuthTokenValidationException.java | 2 +- .../constants/RemoteSessionConstants.java | 12 +- .../remote.session/dto/RemoteSession.java | 66 ++--- .../RemoteSessionManagementException.java | 21 +- .../RemoteSessionManagementDataHolder.java | 31 +-- ...moteSessionManagementServiceComponent.java | 23 +- .../RemoteSessionManagerStartupListener.java | 8 +- .../remote.session/util/PropertyUtils.java | 12 +- .../remote-session-extension/pom.xml | 2 +- 22 files changed, 268 insertions(+), 287 deletions(-) diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/pom.xml b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/pom.xml index a2b67a2933..88edfc0996 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/pom.xml +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/pom.xml @@ -23,7 +23,7 @@ org.wso2.carbon.devicemgt-plugins remote-session-extension - 4.0.54-SNAPSHOT + 4.0.66-SNAPSHOT ../pom.xml diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/ClientSessionSubscriptionEndpoint.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/ClientSessionSubscriptionEndpoint.java index 66ab3e89f1..cadb860886 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/ClientSessionSubscriptionEndpoint.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/ClientSessionSubscriptionEndpoint.java @@ -20,11 +20,9 @@ package org.wso2.carbon.device.mgt.extensions.remote.session.endpoint; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.extensions.remote.session.constants.RemoteSessionConstants; -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.endpoint.utils.HttpSessionConfigurator; import org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.utils.ServiceHolder; +import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException; import javax.websocket.CloseReason; import javax.websocket.OnClose; @@ -43,6 +41,7 @@ import java.io.IOException; public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint { private static final Log log = LogFactory.getLog(ClientSessionSubscriptionEndpoint.class); + /** * Web socket onOpen - When client sends a message * @@ -51,15 +50,10 @@ public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint { @OnOpen public void onOpen(Session session, @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId) { - System.out.print("**************Open***************"); - if (log.isDebugEnabled()) { - log.debug("WebSocket opened, for RemoteSession id: " + session.getId()); - } try { - ServiceHolder.getInstance().getRemoteSessionManagementService().initializeSession(session, deviceType, deviceId); - System.out.print("**************Opened***************"); - } catch (RemoteSessionInvalidException e) { - System.out.print(e.getMessage()); + ServiceHolder.getInstance().getRemoteSessionManagementService().initializeSession(session, deviceType, + deviceId); + } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while initializing session ", e); } @@ -68,20 +62,7 @@ public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint { } catch (IOException ex) { log.error("Failed to disconnect the client.", ex); } - } catch (RemoteSessionManagementException e) { - System.out.print(e.getMessage()); - if (log.isDebugEnabled()) { - log.error("Error occurred while initializing session ", e); - } - try { - session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Error occurred while adding operation")); - } catch (IOException ex) { - if (log.isDebugEnabled()) { - log.error("Failed to disconnect the client.", ex); - } - } } - } /** @@ -92,7 +73,7 @@ public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint { */ @OnMessage public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { + ("deviceId") String deviceId) { super.onMessage(session, message, deviceType, deviceId); } @@ -104,7 +85,7 @@ public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint { */ @OnMessage public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { + ("deviceId") String deviceId) { super.onMessage(session, message, deviceType, deviceId); } diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/DeviceSessionSubscriptionEndpoint.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/DeviceSessionSubscriptionEndpoint.java index 9c771f74d1..9933f1f616 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/DeviceSessionSubscriptionEndpoint.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/DeviceSessionSubscriptionEndpoint.java @@ -20,11 +20,9 @@ package org.wso2.carbon.device.mgt.extensions.remote.session.endpoint; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.extensions.remote.session.constants.RemoteSessionConstants; -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.endpoint.utils.HttpSessionConfigurator; import org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.utils.ServiceHolder; +import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException; import javax.websocket.CloseReason; import javax.websocket.OnClose; @@ -43,6 +41,7 @@ import java.io.IOException; public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint { private static final Log log = LogFactory.getLog(DeviceSessionSubscriptionEndpoint.class); + /** * Web socket onOpen - When client sends a message * @@ -51,16 +50,10 @@ public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint { @OnOpen public void onOpen(Session session, @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId, @PathParam("operationId") String operationId) { - System.out.print("**************Open***************"+operationId); - if (log.isDebugEnabled()) { - log.debug("WebSocket opened, for RemoteSession id: " + session.getId()); - } try { ServiceHolder.getInstance().getRemoteSessionManagementService().initializeSession(session, deviceType, deviceId, operationId); - System.out.print("**************Opened***************"); - } catch (RemoteSessionInvalidException e) { - System.out.print(e.getMessage()); + } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while initializing session ", e); } @@ -69,20 +62,7 @@ public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint { } catch (IOException ex) { log.error("Failed to disconnect the client.", ex); } - } catch (RemoteSessionManagementException e) { - System.out.print(e.getMessage()); - if (log.isDebugEnabled()) { - log.error("Error occurred while initializing session ", e); - } - try { - session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Error occurred while adding operation")); - } catch (IOException ex) { - if (log.isDebugEnabled()) { - log.error("Failed to disconnect the client.", ex); - } - } } - } /** @@ -93,7 +73,7 @@ public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint { */ @OnMessage public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { + ("deviceId") String deviceId) { super.onMessage(session, message, deviceType, deviceId); } @@ -105,7 +85,7 @@ public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint { */ @OnMessage public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { + ("deviceId") String deviceId) { super.onMessage(session, message, deviceType, deviceId); } diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/SubscriptionEndpoint.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/SubscriptionEndpoint.java index 53cd4def73..2431a674ef 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/SubscriptionEndpoint.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/SubscriptionEndpoint.java @@ -20,9 +20,8 @@ package org.wso2.carbon.device.mgt.extensions.remote.session.endpoint; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -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.endpoint.utils.ServiceHolder; +import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException; import javax.websocket.CloseReason; import javax.websocket.Session; @@ -43,15 +42,14 @@ public class SubscriptionEndpoint { * @param message - Status code for web-socket close. */ public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { - System.out.print("______________" + session.getId()); + ("deviceId") String deviceId) { if (log.isDebugEnabled()) { log.debug("Received message from client for RemoteSession id: " + session.getId() + " device type: " + deviceType + " device id: " + deviceId); } try { ServiceHolder.getInstance().getRemoteSessionManagementService().sendMessageToPeer(session, message); - } catch (RemoteSessionInvalidException e) { + } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while send message to peer session ", e); } @@ -72,15 +70,14 @@ public class SubscriptionEndpoint { * @param message - Message which needs to send to peer */ public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam - ("deviceId") String deviceId) throws RemoteSessionManagementException { - System.out.print("______________" + session.getId()); + ("deviceId") String deviceId) { if (log.isDebugEnabled()) { log.debug("Received message from client for RemoteSession id: " + session.getId() + " device type: " + deviceType + " device id: " + deviceId); } try { ServiceHolder.getInstance().getRemoteSessionManagementService().sendMessageToPeer(session, message); - } catch (RemoteSessionInvalidException e) { + } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while send message to peer session ", e); } @@ -102,17 +99,14 @@ public class SubscriptionEndpoint { */ public void onClose(Session session, CloseReason reason, @PathParam("deviceType") String deviceType, @PathParam ("deviceId") String deviceId) { + + ServiceHolder.getInstance().getRemoteSessionManagementService().endSession(session, "Remote session closed"); if (log.isDebugEnabled()) { - log.debug("Closing a WebSocket due to " + reason.getReasonPhrase() + ", for session ID:" + session.getId - () + ", for request URI - " + session.getRequestURI() + " device type: " + deviceType + " device id: " + deviceId); - } - try { - ServiceHolder.getInstance().getRemoteSessionManagementService().endSession(session); - } catch (IOException ex) { - if (log.isDebugEnabled()) { - log.error("Failed to disconnect the client.", ex); - } + log.debug("websocket closed due to " + reason.getReasonPhrase() + ", for session ID:" + session.getId + () + ", for request URI - " + session.getRequestURI() + " device type: " + deviceType + " device " + + "id: " + deviceId); } + } /** @@ -123,12 +117,20 @@ public class SubscriptionEndpoint { */ public void onError(Session session, Throwable throwable, @PathParam("deviceType") String deviceType, @PathParam ("deviceId") String deviceId) { - log.error( - "Error occurred in session ID: " + session.getId() + " device type: " + deviceType + " device id: " + - deviceId + ", for request URI - " + session.getRequestURI() + + + if (throwable instanceof IOException) { + if (log.isDebugEnabled()) { + log.error("Error occurred in session ID: " + session.getId() + " device type: " + deviceType + + "device id: " + deviceId + ", for request URI - " + session.getRequestURI() + ", " + throwable.getMessage(), throwable); + } + } else { + log.error("Error occurred in session ID: " + session.getId() + " device type: " + deviceType + " device " + + "id: " + deviceId + ", for request URI - " + session.getRequestURI() + ", " + throwable.getMessage + (), throwable); + } try { - ServiceHolder.getInstance().getRemoteSessionManagementService().endSession(session); + ServiceHolder.getInstance().getRemoteSessionManagementService().endSession(session, "Remote session closed"); if (session.isOpen()) { session.close(new CloseReason(CloseReason.CloseCodes.PROTOCOL_ERROR, "Unexpected Error Occurred")); } diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/constants/Constants.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/constants/Constants.java index 9bf277389b..fc3dcdfbcc 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/constants/Constants.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/constants/Constants.java @@ -20,6 +20,9 @@ package org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.constants; +/** + * This holds the constants related to remote session web socket endpoint + */ public class Constants { public static final String HTTP_HEADERS = "HttpHeaders"; } diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/utils/HttpSessionConfigurator.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/utils/HttpSessionConfigurator.java index a0088689b4..94b6bf3fd3 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/utils/HttpSessionConfigurator.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session.endpoint/src/main/java/org/wso2/carbon/device/mgt/extensions/remote/session/endpoint/utils/HttpSessionConfigurator.java @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/pom.xml b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/pom.xml index a8033555f7..659aac91f6 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/pom.xml +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/pom.xml @@ -1,6 +1,6 @@