[KARAF-6002] Add websocket servlet appender
authorJean-Baptiste Onofré <jbonofre@apache.org>
Wed, 14 Nov 2018 09:36:15 +0000 (10:36 +0100)
committerJean-Baptiste Onofré <jbonofre@apache.org>
Wed, 14 Nov 2018 09:36:15 +0000 (10:36 +0100)
appender/pom.xml
appender/websocket-servlet/pom.xml [new file with mode: 0644]
appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketAppender.java [new file with mode: 0644]
appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketServlet.java [new file with mode: 0644]
assembly/src/main/feature/feature.xml
manual/src/main/asciidoc/user-guide/appenders.adoc

index 6546526..3c9605b 100644 (file)
@@ -53,6 +53,7 @@
         <module>rest</module>
         <module>socket</module>
         <module>timescaledb</module>
+        <module>websocket-servlet</module>
     </modules>
 
 </project>
\ No newline at end of file
diff --git a/appender/websocket-servlet/pom.xml b/appender/websocket-servlet/pom.xml
new file mode 100644 (file)
index 0000000..319daf8
--- /dev/null
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+
+    <!--
+
+        Licensed to the Apache Software Foundation (ASF) under one or more
+        contributor license agreements.  See the NOTICE file distributed with
+        this work for additional information regarding copyright ownership.
+        The ASF 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.
+    -->
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.karaf.decanter</groupId>
+        <artifactId>appender</artifactId>
+        <version>2.2.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <groupId>org.apache.karaf.decanter.appender</groupId>
+    <artifactId>org.apache.karaf.decanter.appender.websocket-servlet</artifactId>
+    <packaging>bundle</packaging>
+    <name>Apache Karaf :: Decanter :: Appender :: Websocket Servlet</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.eclipse.jetty.websocket</groupId>
+            <artifactId>websocket-servlet</artifactId>
+            <version>9.4.11.v20180605</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.decanter</groupId>
+            <artifactId>org.apache.karaf.decanter.api</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Import-Package>
+                            org.osgi.service.component,
+                            org.osgi.service.event,
+                            *
+                        </Import-Package>
+                        <Private-Package>
+                            org.apache.karaf.decanter.appender.websocket
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
\ No newline at end of file
diff --git a/appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketAppender.java b/appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketAppender.java
new file mode 100644 (file)
index 0000000..d2c16b6
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.karaf.decanter.appender.websocket;
+
+import org.apache.karaf.decanter.api.marshaller.Marshaller;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
+import org.eclipse.jetty.websocket.api.annotations.WebSocket;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.osgi.service.http.HttpService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+
+@Component(
+        name = "org.apache.karaf.decanter.appender.websocket.servlet",
+        immediate = true,
+        property = EventConstants.EVENT_TOPIC + "=decanter/collect/*"
+)
+@WebSocket
+public class DecanterWebSocketAppender implements EventHandler {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DecanterWebSocketAppender.class);
+
+    private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
+
+    @Reference
+    private Marshaller marshaller;
+
+    @Reference
+    private HttpService httpService;
+
+    @OnWebSocketConnect
+    public void onOpen(Session session) {
+        session.setIdleTimeout(-1);
+        sessions.add(session);
+    }
+
+    @OnWebSocketClose
+    public void onClose(Session session, int statusCode, String reason) {
+        sessions.remove(session);
+    }
+
+    @Activate
+    public void activate(ComponentContext componentContext) throws Exception {
+        httpService.registerServlet("/decanter-websocket", new DecanterWebSocketServlet(), null, null);
+    }
+
+    @Deactivate
+    public void deactivate() throws Exception {
+        httpService.unregister("/decanter-websocket");
+    }
+
+    @Override
+    public void handleEvent(Event event) {
+        String message = marshaller.marshal(event);
+        synchronized (sessions) {
+            for (Session session : sessions) {
+                try {
+                    session.getRemote().sendString(message);
+                } catch (Exception e) {
+                    LOG.warn("Can't publish to remote websocket endpoint",  e);
+                }
+            }
+        }
+    }
+
+}
diff --git a/appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketServlet.java b/appender/websocket-servlet/src/main/java/org/apache/karaf/decanter/appender/websocket/DecanterWebSocketServlet.java
new file mode 100644 (file)
index 0000000..7075684
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.karaf.decanter.appender.websocket;
+
+import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
+import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
+
+import javax.servlet.annotation.WebServlet;
+
+@WebServlet(name = "Decanter WebSocket Servlet", urlPatterns = { "/decanter-websocket" })
+public class DecanterWebSocketServlet extends WebSocketServlet {
+
+    @Override
+    public void configure(WebSocketServletFactory factory) {
+        factory.register(DecanterWebSocketAppender.class);
+    }
+
+}
index 67ad591..845b665 100644 (file)
         <configfile finalname="/etc/org.apache.karaf.decanter.appender.timescaledb.cfg">mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.timescaledb/${project.version}/cfg</configfile>
     </feature>
 
+    <feature name="decanter-appender-websocket-servlet" version="${project.version}" description="Karaf Decanter WebSocket Servlet Appender">
+        <feature>decanter-common</feature>
+        <feature>http-whiteboard</feature>
+        <bundle>mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.websocket-servlet/${project.version}</bundle>
+    </feature>
+
     <feature name="decanter-alerting-core" version="${project.version}" description="Karaf Decanter Alerting core">
         <feature>decanter-common</feature>
         <bundle>mvn:org.apache.karaf.decanter.alerting/org.apache.karaf.decanter.alerting.checker/${project.version}</bundle>
index 10829c8..1df18b7 100644 (file)
@@ -1420,3 +1420,36 @@ The table is simple and contains just two column:
 ** `timestamp` as BIGINT
 ** `content` as TEXT
 * `marshaller.target` is the marshaller used to serialize data into the table.
+
+===== WebSocket Servlet
+
+The `decanter-appender-websocket-servlet` feature expose a websocket on wich client can register. Then, Decanter will send the collected data to the connected clients.
+
+It's very easy to use. First install the feature:
+
+```
+karaf@root()> feature:install decanter-appender-websocket-servlet
+```
+
+The feature register the WebSocket endpoint on `http://localhost:8181/decanter-websocket` by default:
+
+```
+karaf@root()> http:list
+ID │ Servlet                  │ Servlet-Name   │ State       │ Alias               │ Url
+───┼──────────────────────────┼────────────────┼─────────────┼─────────────────────┼────────────────────────
+55 │ DecanterWebSocketServlet │ ServletModel-2 │ Deployed    │ /decanter-websocket │ [/decanter-websocket/*]
+```
+
+You can now register your websocket client on this URL. You can use `curl` as client to test:
+
+```
+curl --include \
+     --no-buffer \
+     --header "Connection: Upgrade" \
+     --header "Upgrade: websocket" \
+     --header "Host: localhost:8181" \
+     --header "Origin: http://localhost:8181/decanter-websocket" \
+     --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
+     --header "Sec-WebSocket-Version: 13" \
+     http://localhost:8181/decanter-websocket
+```
\ No newline at end of file