<module>rest</module>
<module>socket</module>
<module>timescaledb</module>
+ <module>websocket-servlet</module>
</modules>
</project>
\ No newline at end of file
--- /dev/null
+<?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
--- /dev/null
+/*
+ * 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);
+ }
+ }
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
<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>
** `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