List of usage examples for javax.websocket Session getId
String getId();
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.SubscriptionEndpoint.java
/** * Web socket onClose use to handle socket connection close * * @param session - Registered session. * @param deviceType - DeviceType/* w w w .j av a 2 s. co m*/ * @param deviceId - Device Identifier * @param reason - Status code for web-socket close. */ 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("websocket closed due to " + reason.getReasonPhrase() + ", for session ID:" + session.getId() + ", for request URI - " + session.getRequestURI() + " device type: " + deviceType + " device " + "id: " + deviceId); } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.SubscriptionEndpoint.java
/** * Web socket onError use to handle socket connection error * * @param session - Registered session. * @param throwable - Web socket exception * @param deviceType - DeviceType//w w w. j av a 2 s. co m * @param deviceId - Device Identifier */ public void onError(Session session, Throwable throwable, @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId) { 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, "Remote session closed"); if (session.isOpen()) { session.close(new CloseReason(CloseReason.CloseCodes.PROTOCOL_ERROR, "Unexpected Error Occurred")); } } catch (IOException ex) { if (log.isDebugEnabled()) { log.error("Failed to disconnect the client.", ex); } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java
/** * Implements the behaviour of sending message to peer connection * * @param session Web socket RemoteSession * @param message String message needs to send to peer connection * @throws RemoteSessionManagementException throws when session cannot be made due to invalid data * @throws RemoteSessionManagementException throws when session has error with accessing device resources */// w w w .j a v a 2 s. co m @Override public void sendMessageToPeer(Session session, String message) throws RemoteSessionManagementException { JSONObject jsonObject = new JSONObject(message); RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap() .get(session.getId()); if (remoteSession != null) { remoteSession.sendMessageToPeer(jsonObject.toString()); } else { throw new RemoteSessionManagementException("Remote Session cannot be found "); } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java
/** * Implements the behaviour of sending message to peer connection * * @param session Web socket RemoteSession * @param message Byte message needs to send to peer connection * @throws RemoteSessionManagementException throws when session cannot be made due to invalid data * @throws RemoteSessionManagementException throws when session has error with accessing device resources *//*from ww w . jav a 2 s.co m*/ @Override public void sendMessageToPeer(Session session, byte[] message) throws RemoteSessionManagementException { RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap() .get(session.getId()); if (remoteSession != null) { remoteSession.sendMessageToPeer(message); } else { throw new RemoteSessionManagementException("Remote Session cannot be found "); } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java
/** * Closing the session and cleanup the resources * * @param session Web socket Remote Session *//* ww w . j av a2 s .c o m*/ @Override public void endSession(Session session, String closeReason) { RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap() .remove(session.getId()); if (remoteSession != null) { //String operationId = remoteSession.getOperationId(); String deviceKey = remoteSession.getTenantDomain() + "/" + remoteSession.getDeviceType() + "/" + remoteSession.getDeviceId(); RemoteSession lastSession = RemoteSessionManagementDataHolder.getInstance() .getActiveDeviceClientSessionMap().get(deviceKey); if (lastSession != null && lastSession.getMySession().getId().equals(session.getId())) { RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap().remove(deviceKey); } if (remoteSession.getPeerSession() != null) { Session peerSession = remoteSession.getPeerSession().getMySession(); if (peerSession != null) { RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(peerSession.getId()); if (lastSession != null && lastSession.getMySession().getId().equals(peerSession.getId())) { RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap() .remove(deviceKey); } 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); } } } } } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java
/** * Starting new client session// www . j ava 2s . c o m * * @param session Web socket Session * @param tenantDomain Tenant domain * @param deviceType Device Type * @param deviceId Device Id * @throws RemoteSessionManagementException throws when session has errors with accessing device resources * @throws OperationManagementException throws when error occured during new operation * @throws InvalidDeviceException throws when incorrect device identifier */ private void initializeClientSession(Session session, String tenantDomain, String deviceType, String deviceId) throws RemoteSessionManagementException, OperationManagementException, InvalidDeviceException { RemoteSession clientRemote = new RemoteSession(session, tenantDomain, deviceType, deviceId, RemoteSessionConstants.CONNECTION_TYPE.CLIENT); String deviceKey = tenantDomain + "/" + deviceType + "/" + deviceId; // Create new remote control operation to start the session RemoteSession activeSession = RemoteSessionManagementDataHolder.getInstance() .getActiveDeviceClientSessionMap().putIfAbsent(deviceKey, clientRemote); if (activeSession != null && activeSession.getMySession().isOpen() && activeSession.getPeerSession() == null) { throw new RemoteSessionManagementException("Another client session waiting on device to connect."); } else { // if there is pending session exists but already closed, then we need to remove it. if (activeSession != null) { RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap().remove(deviceKey); try { activeSession.getMySession().close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "Remote " + "session closed due to new session request")); } catch (IOException ex) { if (log.isDebugEnabled()) { log.error("Failed to disconnect the client.", ex); } } // Use put if absent for adding session to waiting list since we need to overcome // multithreaded session requests. activeSession = RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap() .putIfAbsent(deviceKey, clientRemote); } // 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 Operation operation = new ConfigOperation(); operation.setCode(RemoteSessionConstants.REMOTE_CONNECT); operation.setEnabled(true); operation.setControl(Operation.Control.NO_REPEAT); JSONObject payload = new JSONObject(); payload.put("serverUrl", RemoteSessionManagementDataHolder.getInstance().getServerUrl()); operation.setPayLoad(payload.toString()); String date = new SimpleDateFormat(RemoteSessionConstants.DATE_FORMAT_NOW).format(new Date()); operation.setCreatedTimeStamp(date); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(new DeviceIdentifier(deviceId, deviceType)); Activity activity = RemoteSessionManagementDataHolder.getInstance() .getDeviceManagementProviderService() .addOperation(deviceType, operation, deviceIdentifiers); 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); } else { throw new RemoteSessionManagementException( "Another client session waiting on " + "device to connect."); } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java
/** * Starting new device session/* w w w .ja v a 2 s .c o m*/ * * @param session Web socket Session * @param tenantDomain Tenant domain * @param deviceType Device Type * @param deviceId Device Id * @param operationId Operation id * @throws RemoteSessionManagementException throws when session has errors with accessing device resources */ private void initializeDeviceSession(Session session, String tenantDomain, String deviceType, String deviceId, String operationId) throws RemoteSessionManagementException { String deviceKey = tenantDomain + "/" + deviceType + "/" + deviceId; RemoteSession activeSession = RemoteSessionManagementDataHolder.getInstance() .getActiveDeviceClientSessionMap().get(deviceKey); if (activeSession != null) { RemoteSession clientRemote = RemoteSessionManagementDataHolder.getInstance().getSessionMap() .get(activeSession.getMySession().getId()); if (clientRemote != null) { if (clientRemote.getOperationId().equals(operationId)) { RemoteSession deviceRemote = new RemoteSession(session, tenantDomain, deviceType, deviceId, RemoteSessionConstants.CONNECTION_TYPE.DEVICE); deviceRemote.setOperationId(operationId); deviceRemote.setPeerSession(clientRemote); clientRemote.setPeerSession(deviceRemote); RemoteSessionManagementDataHolder.getInstance().getSessionMap().put(session.getId(), deviceRemote); // Send Remote connect response JSONObject message = new JSONObject(); message.put(RemoteSessionConstants.REMOTE_CONNECT_CODE, RemoteSessionConstants.REMOTE_CONNECT); deviceRemote.sendMessageToPeer(message.toString()); log.info("Device session opened for session id: " + session.getId() + " device Type : " + deviceType + " , " + "deviceId : " + deviceId); } else { throw new RemoteSessionManagementException("Device and Operation information " + "does not matched with client information for operation id: " + operationId + " device " + "Type : " + deviceType + " , " + "deviceId : " + deviceId); } } else { throw new RemoteSessionManagementException("Device session is inactive for " + "operation id: " + operationId + " device Type : " + deviceType + " , " + "deviceId : " + deviceId); } } else { throw new RemoteSessionManagementException("Device session is inactive for operation " + "id: " + operationId + " device Type : " + deviceType + " , " + "deviceId : " + deviceId); } }
From source file:org.wso2.carbon.device.mgt.output.adapter.websocket.endpoint.SubscriptionEndpoint.java
/** * Web socket onClose - Remove the registered sessions * * @param session - Users registered session. * @param reason - Status code for web-socket close. * @param streamName - StreamName extracted from the ws url. * @param version - Version extracted from the ws url. *///from w w w . ja v a 2 s . c o m public void onClose(Session session, CloseReason reason, String streamName, String version) { if (log.isDebugEnabled()) { log.debug("Closing a WebSocket due to " + reason.getReasonPhrase() + ", for session ID:" + session.getId() + ", for request URI - " + session.getRequestURI()); } ServiceHolder.getInstance().getWebsocketOutputCallbackControllerService().unsubscribeWebsocket(streamName, version, session); }
From source file:org.wso2.carbon.device.mgt.output.adapter.websocket.endpoint.SubscriptionEndpoint.java
/** * Web socket onError - Remove the registered sessions * * @param session - Users registered session. * @param throwable - Status code for web-socket close. * @param streamName - StreamName extracted from the ws url. * @param version - Version extracted from the ws url. *//*from w ww . ja v a2 s . c om*/ public void onError(Session session, Throwable throwable, String streamName, String version) { log.error("Error occurred in session ID: " + session.getId() + ", for request URI - " + session.getRequestURI() + ", " + throwable.getMessage(), throwable); ServiceHolder.getInstance().getWebsocketOutputCallbackControllerService().unsubscribeWebsocket(streamName, version, session); }
From source file:org.wso2.carbon.device.mgt.output.adapter.websocket.endpoint.SuperTenantSubscriptionEndpoint.java
/** * Web socket onOpen - When client sends a message * * @param session - Users registered session. * @param streamName - StreamName extracted from the ws url. * @param version - Version extracted from the ws url. *//*from www .j a va 2 s. c om*/ @OnOpen public void onOpen(Session session, EndpointConfig config, @PathParam("streamname") String streamName, @PathParam("version") String version) { if (log.isDebugEnabled()) { log.debug("WebSocket opened, for Session id: " + session.getId() + ", for the Stream:" + streamName); } Map<String, List<String>> sessionQueryParam = new HashedMap(); List<String> sessionQueryParamList = new LinkedList<>(); sessionQueryParamList.add(session.getQueryString()); sessionQueryParam.put(Constants.QUERY_STRING, sessionQueryParamList); Authenticator authenticator = ServiceHolder.getWebsocketValidationService().getAuthenticator(); AuthenticationInfo authenticationInfo = authenticator.isAuthenticated(sessionQueryParam); if (authenticationInfo != null && authenticationInfo.isAuthenticated()) { Authorizer authorizer = ServiceHolder.getWebsocketValidationService().getAuthorizer(); boolean isAuthorized = authorizer.isAuthorized(authenticationInfo, session, streamName); if (isAuthorized) { try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext() .setTenantId(MultitenantConstants.SUPER_TENANT_ID); ServiceHolder.getInstance().getWebsocketOutputCallbackControllerService() .subscribeWebsocket(streamName, version, session); } finally { PrivilegedCarbonContext.endTenantFlow(); } } else { log.info("Failed to authorize the connection for the stream : " + streamName + " , version : " + version); } } else { try { session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Unauthorized Access")); } catch (IOException e) { log.error("Failed to disconnect the unauthorized client.", e); } } }