forked from community/device-mgt-plugins
Merge branch 'master' of https://github.com/wso2/carbon-device-mgt-plugins
commit
37dd3f5d88
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!--
|
||||
~ 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
|
||||
~ in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing,
|
||||
~ software distributed under the License is distributed on an
|
||||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
~ KIND, either express or implied. See the License for the
|
||||
~ specific language governing permissions and limitations
|
||||
~ under the License.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<artifactId>remote-session-extension</artifactId>
|
||||
<version>4.0.100-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.remote.session.endpoint</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<name>WSO2 - Webapp for UI Remote Session</name>
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-websocket-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.ws.rs</groupId>
|
||||
<artifactId>javax.ws.rs-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json.wso2</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.remote.session</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>remote#session</finalName>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* deviceId 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
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.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;
|
||||
import javax.websocket.OnError;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class represents web socket endpoint to manage Remote Sessions
|
||||
*/
|
||||
@ServerEndpoint(value = "/clients/{deviceType}/{deviceId}", configurator = HttpSessionConfigurator.class)
|
||||
public class ClientSessionSubscriptionEndpoint extends SubscriptionEndpoint {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ClientSessionSubscriptionEndpoint.class);
|
||||
|
||||
/**
|
||||
* Web socket onOpen use when client connect to web socket url
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onMessage use when client sends a string message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onMessage(session, message, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onMessage use when client sends a byte message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
* @param message - Byte message which needs to send to peer
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onMessage(session, message, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onClose use to handle socket connection close
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
* @param reason - Status code for web-socket close.
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session, CloseReason reason, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onClose(session, reason, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onError use to handle socket connection error
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param throwable - Web socket exception
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable throwable, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onError(session, throwable, deviceType, deviceId);
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* deviceId 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
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.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;
|
||||
import javax.websocket.OnError;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class represents web socket endpoint to manage Remote Sessions
|
||||
*/
|
||||
@ServerEndpoint(value = "/devices/{deviceType}/{deviceId}/{operationId}", configurator = HttpSessionConfigurator.class)
|
||||
public class DeviceSessionSubscriptionEndpoint extends SubscriptionEndpoint {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceSessionSubscriptionEndpoint.class);
|
||||
|
||||
/**
|
||||
* Web socket onOpen use when device connect to web socket url
|
||||
*
|
||||
* @param session - Web socket Session
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onMessage use when device sends a string message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param message - String message which needs to send to peer
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onMessage(session, message, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onMessage use when device sends a byte message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param message - Byte message which needs to send to peer
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, byte[] message, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onMessage(session, message, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onClose use to handle socket connection close
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
* @param reason - Status code for web-socket close.
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session, CloseReason reason, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onClose(session, reason, deviceType, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onError use to handle socket connection error
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param throwable - Web socket exception
|
||||
* @param deviceType - DeviceType
|
||||
* @param deviceId - Device Identifier
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable throwable, @PathParam("deviceType") String deviceType, @PathParam
|
||||
("deviceId") String deviceId) {
|
||||
super.onError(session, throwable, deviceType, deviceId);
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* deviceId 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
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.endpoint.utils.ServiceHolder;
|
||||
import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException;
|
||||
|
||||
import javax.websocket.CloseReason;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.PathParam;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class represents common web socket endpoint to manage Remote Sessions
|
||||
*/
|
||||
public class SubscriptionEndpoint {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SubscriptionEndpoint.class);
|
||||
|
||||
/**
|
||||
* Web socket onMessage - When client sends a message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onMessage use When client sends a message
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onClose use to handle socket connection close
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Web socket onError use to handle socket connection error
|
||||
*
|
||||
* @param session - Registered session.
|
||||
* @param throwable - Web socket exception
|
||||
* @param deviceType - DeviceType
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue