List of usage examples for javax.websocket Session close
void close(CloseReason closeReason) throws IOException;
From source file:hydrograph.server.execution.tracking.client.main.HydrographMain.java
/** * /*w w w .j a v a 2s . co m*/ * @param latch * @param session * @param jobId * @param timer * @param execution * @param socket * @throws IOException */ private void sendExecutionTrackingStatus(final CountDownLatch latch, Session session, final String jobId, final Timer timer, final HydrographService execution, final HydrographEngineCommunicatorSocket socket) throws IOException { try { TimerTask task = new TimerTask() { ExecutionStatus previousExecutionStatus = null; @Override public void run() { List<ComponentInfo> componentInfos = execution.getStatus(); if (!componentInfos.isEmpty()) { List<ComponentStatus> componentStatusList = new ArrayList<ComponentStatus>(); for (ComponentInfo componentInfo : componentInfos) { ComponentStatus componentStatus = new ComponentStatus(componentInfo.getComponentId(), componentInfo.getComponentName(), componentInfo.getCurrentStatus(), componentInfo.getBatch(), componentInfo.getProcessedRecords()); componentStatusList.add(componentStatus); } ExecutionStatus executionStatus = new ExecutionStatus(componentStatusList); executionStatus.setJobId(jobId); executionStatus.setClientId(Constants.ENGINE_CLIENT + jobId); executionStatus.setType(Constants.POST); Gson gson = new Gson(); try { if (previousExecutionStatus == null || !executionStatus.equals(previousExecutionStatus)) { socket.sendMessage(gson.toJson(executionStatus)); previousExecutionStatus = executionStatus; } } catch (IOException e) { logger.error("Fail to send status for job - " + jobId, e); timer.cancel(); } if (StringUtils.isNotBlank(jobId)) { //moved this after sendMessage in order to log even if the service is not running ExecutionTrackingFileLogger.INSTANCE.log(jobId, executionStatus); } } if (!execution.getJobRunningStatus()) { timer.cancel(); latch.countDown(); } } }; timer.schedule(task, 0l, ExecutionTrackingUtils.INSTANCE.getStatusFrequency()); latch.await(); } catch (Throwable t) { logger.error("Failure in job - " + jobId, t); timer.cancel(); throw new RuntimeException(t); } finally { if (session != null && session.isOpen()) { logger.debug("Closing Websocket engine client"); CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Session Closed"); session.close(closeReason); } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.ClientSessionSubscriptionEndpoint.java
/** * Web socket onOpen use when client connect to web socket url * * @param session - Registered session. * @param deviceType - DeviceType//w w w .j a v a 2s . co m * @param deviceId - Device Identifier */ @OnOpen public void onOpen(Session session, @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId) { try { ServiceHolder.getInstance().getRemoteSessionManagementService().initializeSession(session, deviceType, deviceId); } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while initializing session ", e); } try { session.close(e.getCloseReason()); } catch (IOException ex) { log.error("Failed to disconnect the client.", ex); } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.DeviceSessionSubscriptionEndpoint.java
/** * Web socket onOpen use when device connect to web socket url * * @param session - Web socket Session * @param deviceType - DeviceType/*from w w w . j a v a 2 s . co m*/ * @param deviceId - Device Identifier * @param operationId - Operations Id */ @OnOpen public void onOpen(Session session, @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId, @PathParam("operationId") String operationId) { try { ServiceHolder.getInstance().getRemoteSessionManagementService().initializeSession(session, deviceType, deviceId, operationId); } catch (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while initializing session ", e); } try { session.close(e.getCloseReason()); } catch (IOException ex) { log.error("Failed to disconnect the client.", ex); } } }
From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.SubscriptionEndpoint.java
/** * Web socket onMessage - When client sends a message * * @param session - Registered session. * @param deviceType - DeviceType//from ww w . ja v a 2s . co m * @param message - String Message which needs to send to peer */ public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam("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 (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while send message to peer session ", e); } try { session.close(e.getCloseReason()); } 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.endpoint.SubscriptionEndpoint.java
/** * Web socket onMessage use When client sends a message * * @param session - Registered session. * @param deviceType - DeviceType// w w w . j a va 2 s . co m * @param deviceId - Device Identifier * @param message - Byte Message which needs to send to peer */ public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam("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 (RemoteSessionManagementException e) { if (log.isDebugEnabled()) { log.error("Error occurred while send message to peer session ", e); } try { session.close(e.getCloseReason()); } 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.endpoint.SubscriptionEndpoint.java
/** * Web socket onError use to handle socket connection error * * @param session - Registered session. * @param throwable - Web socket exception * @param deviceType - DeviceType//from ww w . j a v a2 s.c o 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
/** * Closing the session and cleanup the resources * * @param session Web socket Remote Session *//*from ww w. j a va2 s . c om*/ @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.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. */// w w w .j av a2 s.c o m @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); } } }
From source file:org.wso2.carbon.device.mgt.output.adapter.websocket.endpoint.TenantSubscriptionEndpoint.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. * @param tdomain - Tenant domain extracted from ws url. *///from ww w . ja v a 2 s . c o m @OnOpen public void onOpen(Session session, EndpointConfig config, @PathParam("streamname") String streamName, @PathParam("version") String version, @PathParam("tdomain") String tdomain) { 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() .setTenantDomain(authenticationInfo.getTenantDomain(), true); ServiceHolder.getInstance().getWebsocketOutputCallbackControllerService() .subscribeWebsocket(streamName, version, session); } finally { PrivilegedCarbonContext.endTenantFlow(); } } else { try { session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Unauthorized Access")); } catch (IOException e) { log.error("Failed to disconnect the unauthorized client.", e); } } } else { try { session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Unauthorized Access")); } catch (IOException e) { log.error("Failed to disconnect the unauthorized client.", e); } } }