forked from community/device-mgt-core
parent
4848e271af
commit
82778f06c6
@ -0,0 +1,82 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) 2018, 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/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>device-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<version>3.1.34-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.wsproxy</artifactId>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 - Webapp for Web Socket Proxy</name>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</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>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>secured-websocket-proxy</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jacoco</groupId>
|
||||||
|
<artifactId>jacoco-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>jacoco-initialize</id>
|
||||||
|
<goals>
|
||||||
|
<goal>prepare-agent</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>jacoco-site</id>
|
||||||
|
<phase>test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>report</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
|
||||||
|
<outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.wsproxy.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates an error with deployment tinkerer
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public class WSProxyException extends Exception {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new exception with the message provided and the cause.
|
||||||
|
*
|
||||||
|
* @param message the detailed message of the exception
|
||||||
|
* @param cause the cause of the exception
|
||||||
|
*/
|
||||||
|
public WSProxyException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.wsproxy.inbound;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.DataPublisherUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.wsproxy.exception.WSProxyException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.wsproxy.outbound.AnalyticsClient;
|
||||||
|
|
||||||
|
import javax.websocket.CloseReason;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for subscription and un-subscription for web socket
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SubscriptionEndpoint {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(SubscriptionEndpoint.class);
|
||||||
|
private Map<String, List<AnalyticsClient>> analyticsClientsMap = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web socket onOpen - When client sends a message
|
||||||
|
*
|
||||||
|
* @param session - Users registered session.
|
||||||
|
*/
|
||||||
|
public void onOpen(Session session) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("WebSocket opened, for Session id: " + session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance();
|
||||||
|
ArrayList<String> publisherGroups =
|
||||||
|
DataPublisherUtil.getEndpointGroups(analyticsConfig.getAnalyticsPublisherUrl());
|
||||||
|
List<AnalyticsClient> analyticsClients = new ArrayList<>();
|
||||||
|
for (String publisherURLGroup : publisherGroups) {
|
||||||
|
try {
|
||||||
|
String[] endpoints = DataPublisherUtil.getEndpoints(publisherURLGroup);
|
||||||
|
for (String endpoint : endpoints) {
|
||||||
|
try {
|
||||||
|
endpoint = endpoint.trim();
|
||||||
|
if (!endpoint.endsWith("/")) {
|
||||||
|
endpoint += "/";
|
||||||
|
}
|
||||||
|
endpoint += session.getRequestURI().getSchemeSpecificPart().replace("secured-websocket-proxy","");
|
||||||
|
AnalyticsClient analyticsClient = new AnalyticsClient(session);
|
||||||
|
analyticsClient.connectClient(new URI(endpoint));
|
||||||
|
analyticsClients.add(analyticsClient);
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
log.error("Unable to create URL from: " + endpoint, e);
|
||||||
|
} catch (WSProxyException e) {
|
||||||
|
log.error("Unable to create WS client for: " + endpoint, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (DataEndpointConfigurationException e) {
|
||||||
|
log.error("Unable to obtain endpoints from receiverURLGroup: " + publisherURLGroup, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Configured " + analyticsClients.size() + " analytics clients for Session id: " +
|
||||||
|
session.getId());
|
||||||
|
}
|
||||||
|
analyticsClientsMap.put(session.getId(), analyticsClients);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* @param tenantDomain - Domain of the tenant.
|
||||||
|
*/
|
||||||
|
public void onClose(Session session, CloseReason reason, String streamName, String version, String tenantDomain) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Closing a WebSocket due to " + reason.getReasonPhrase() + ", for session ID:" +
|
||||||
|
session.getId() + ", for request URI - " + session.getRequestURI());
|
||||||
|
}
|
||||||
|
for (AnalyticsClient analyticsClient : analyticsClientsMap.get(session.getId())) {
|
||||||
|
if (analyticsClient != null) {
|
||||||
|
try {
|
||||||
|
analyticsClient.closeConnection(reason);
|
||||||
|
} catch (WSProxyException e) {
|
||||||
|
log.error("Error occurred while closing ws connection due to " + reason.getReasonPhrase() +
|
||||||
|
", for session ID:" + session.getId() + ", for request URI - " + session.getRequestURI(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
analyticsClientsMap.remove(session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web socket onMessage - When client sens a message
|
||||||
|
*
|
||||||
|
* @param session - Users registered session.
|
||||||
|
* @param message - Status code for web-socket close.
|
||||||
|
*/
|
||||||
|
public void onMessage(Session session, String message) {
|
||||||
|
for (AnalyticsClient analyticsClient : analyticsClientsMap.get(session.getId())) {
|
||||||
|
if (analyticsClient != null) {
|
||||||
|
analyticsClient.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web socket onError
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
* @param tenantDomain - Domain of the tenant.
|
||||||
|
*/
|
||||||
|
public void onError(Session session, Throwable throwable, String streamName, String version, String tenantDomain) {
|
||||||
|
log.error("Error occurred in session ID: " + session.getId() + ", for request URI - " +
|
||||||
|
session.getRequestURI() + ", " + throwable.getMessage(), throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.wsproxy.inbound;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
|
|
||||||
|
import javax.websocket.CloseReason;
|
||||||
|
import javax.websocket.EndpointConfig;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to web socket with Super tenant
|
||||||
|
*/
|
||||||
|
|
||||||
|
@ServerEndpoint(value = "/{destination}/{streamname}/{version}")
|
||||||
|
public class SuperTenantSubscriptionEndpoint extends SubscriptionEndpoint {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(SuperTenantSubscriptionEndpoint.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
super.onOpen(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web socket onMessage - When client sens a message
|
||||||
|
*
|
||||||
|
* @param session - Users registered session.
|
||||||
|
* @param message - Status code for web-socket close.
|
||||||
|
* @param streamName - StreamName extracted from the ws url.
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(Session session, String message, @PathParam("streamname") String streamName) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Received message from client. Message: " + message + ", " +
|
||||||
|
"for Session id: " + session.getId() + ", for the Stream:" + streamName);
|
||||||
|
}
|
||||||
|
super.onMessage(session, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session session, CloseReason reason, @PathParam("streamname") String streamName,
|
||||||
|
@PathParam("version") String version) {
|
||||||
|
super.onClose(session, reason, streamName, version, MultitenantConstants.SUPER_TENANT_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@OnError
|
||||||
|
public void onError(Session session, Throwable throwable, @PathParam("streamname") String streamName,
|
||||||
|
@PathParam("version") String version) {
|
||||||
|
super.onError(session, throwable, streamName, version, MultitenantConstants.SUPER_TENANT_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.wsproxy.inbound;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import javax.websocket.CloseReason;
|
||||||
|
import javax.websocket.EndpointConfig;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to web socket with a tenant
|
||||||
|
*/
|
||||||
|
|
||||||
|
@ServerEndpoint(value = "/{destination}/t/{tdomain}/{streamname}/{version}")
|
||||||
|
public class TenantSubscriptionEndpoint extends SubscriptionEndpoint {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(TenantSubscriptionEndpoint.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
super.onOpen(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web socket onMessage - When client sens a message
|
||||||
|
*
|
||||||
|
* @param session - Users registered session.
|
||||||
|
* @param message - Status code for web-socket close.
|
||||||
|
* @param streamName - StreamName extracted from the ws url.
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(Session session, String message, @PathParam("streamname") String streamName, @PathParam("tdomain") String tdomain) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Received message from client. Message: " + message + ", for Session id: " +
|
||||||
|
session.getId() + ", for tenant domain" + tdomain + ", for the Adaptor:" + streamName);
|
||||||
|
}
|
||||||
|
super.onMessage(session, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session session, CloseReason reason, @PathParam("streamname") String streamName,
|
||||||
|
@PathParam("version") String version, @PathParam("tdomain") String tdomain) {
|
||||||
|
super.onClose(session, reason, streamName, version, tdomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@OnError
|
||||||
|
public void onError(Session session, Throwable throwable, @PathParam("streamname") String streamName,
|
||||||
|
@PathParam("version") String version, @PathParam("tdomain") String tdomain) {
|
||||||
|
super.onError(session, throwable, streamName, version, tdomain);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.wsproxy.outbound;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.wsproxy.exception.WSProxyException;
|
||||||
|
|
||||||
|
import javax.websocket.CloseReason;
|
||||||
|
import javax.websocket.ContainerProvider;
|
||||||
|
import javax.websocket.DeploymentException;
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.WebSocketContainer;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds web socket client implementation
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
@javax.websocket.ClientEndpoint
|
||||||
|
public class AnalyticsClient {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AnalyticsClient.class);
|
||||||
|
|
||||||
|
private WebSocketContainer container;
|
||||||
|
private Session analyticsSession = null;
|
||||||
|
private Session clientSession;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create {@link AnalyticsClient} instance.
|
||||||
|
*/
|
||||||
|
public AnalyticsClient(Session clientSession) {
|
||||||
|
container = ContainerProvider.getWebSocketContainer();
|
||||||
|
this.clientSession = clientSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create web socket client connection using {@link WebSocketContainer}.
|
||||||
|
*/
|
||||||
|
public void connectClient(URI endpointURI) throws WSProxyException {
|
||||||
|
try {
|
||||||
|
analyticsSession = container.connectToServer(this, endpointURI);
|
||||||
|
} catch (DeploymentException | IOException e) {
|
||||||
|
String msg = "Error occurred while connecting to remote endpoint " + endpointURI.toString();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new WSProxyException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback hook for Connection close events.
|
||||||
|
*
|
||||||
|
* @param userSession the analyticsSession which is getting closed.
|
||||||
|
* @param reason the reason for connection close
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose(Session userSession, CloseReason reason) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Closing web socket session: '" + userSession.getId() + "'. Code: " +
|
||||||
|
reason.getCloseCode().toString() + " Reason: " + reason.getReasonPhrase());
|
||||||
|
}
|
||||||
|
this.analyticsSession = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback hook for Message Events.
|
||||||
|
*
|
||||||
|
* <p>This method will be invoked when a client send a message.
|
||||||
|
*
|
||||||
|
* @param message The text message.
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message) {
|
||||||
|
this.clientSession.getAsyncRemote().sendText(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message.
|
||||||
|
*
|
||||||
|
* @param message the message which is going to send.
|
||||||
|
*/
|
||||||
|
public void sendMessage(String message) {
|
||||||
|
this.analyticsSession.getAsyncRemote().sendText(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close current connection.
|
||||||
|
*/
|
||||||
|
public void closeConnection(CloseReason closeReason) throws WSProxyException {
|
||||||
|
if (this.analyticsSession != null) {
|
||||||
|
try {
|
||||||
|
this.analyticsSession.close(closeReason);
|
||||||
|
} catch (IOException e) {
|
||||||
|
String msg = "Error on closing WS connection.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new WSProxyException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
<!--
|
||||||
|
~ Copyright (c) 2018, 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.
|
||||||
|
-->
|
||||||
|
<!DOCTYPE web-app PUBLIC
|
||||||
|
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||||
|
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||||
|
|
||||||
|
<web-app>
|
||||||
|
<display-name>Output WebSocket Proxy</display-name>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<filter-name>ContentTypeBasedCachePreventionFilter</filter-name>
|
||||||
|
<filter-class>org.wso2.carbon.ui.filters.cache.ContentTypeBasedCachePreventionFilter</filter-class>
|
||||||
|
<init-param>
|
||||||
|
<param-name>patterns</param-name>
|
||||||
|
<param-value>text/html" ,application/json" ,text/plain</param-value>
|
||||||
|
</init-param>
|
||||||
|
<init-param>
|
||||||
|
<param-name>filterAction</param-name>
|
||||||
|
<param-value>enforce</param-value>
|
||||||
|
</init-param>
|
||||||
|
<init-param>
|
||||||
|
<param-name>httpHeaders</param-name>
|
||||||
|
<param-value>Cache-Control: no-store, no-cache, must-revalidate, private</param-value>
|
||||||
|
</init-param>
|
||||||
|
</filter>
|
||||||
|
|
||||||
|
<filter-mapping>
|
||||||
|
<filter-name>ContentTypeBasedCachePreventionFilter</filter-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</filter-mapping>
|
||||||
|
</web-app>
|
@ -1,2 +0,0 @@
|
|||||||
instructions.configure = \
|
|
||||||
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.analytics.data.publisher_${feature.version}/conf/device-analytics-config.xml,target:${installFolder}/../../conf/etc/device-analytics-config.xml,overwrite:true);\
|
|
@ -0,0 +1,4 @@
|
|||||||
|
instructions.configure = \
|
||||||
|
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.analytics_${feature.version}/conf/device-analytics-config.xml,target:${installFolder}/../../conf/etc/device-analytics-config.xml,overwrite:true);\
|
||||||
|
org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../deployment/server/webapps/);\
|
||||||
|
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.analytics_${feature.version}/webapps/secured-websocket-proxy.war,target:${installFolder}/../../deployment/server/webapps/secured-websocket-proxy.war,overwrite:true);\
|
Loading…
Reference in new issue