From 17b1bd70887c31e73dac40980e58cd660df2da7e Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Mon, 26 Aug 2019 20:27:37 +0200
Subject: [PATCH] Add webapi plugin skeleton

---
 .idea/encodings.xml                           |   1 +
 .idea/modules.xml                             |   1 +
 PlayWallCore/pom.xml                          |  27 +++-
 PlayWallPlugins/PlayWallPluginWebAPI/pom.xml  | 137 ++++++++++++++++++
 .../src/main/resources/plugin.yml             |   6 +
 .../playpad/plugin/api/WebApiPlugin.scala     |  22 +++
 .../api/websocket/WebSocketHandler.scala      |  31 ++++
 PlayWallPlugins/pom.xml                       |   1 +
 pom.xml                                       |   2 +
 9 files changed, 220 insertions(+), 8 deletions(-)
 create mode 100644 PlayWallPlugins/PlayWallPluginWebAPI/pom.xml
 create mode 100644 PlayWallPlugins/PlayWallPluginWebAPI/src/main/resources/plugin.yml
 create mode 100644 PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
 create mode 100644 PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala

diff --git a/.idea/encodings.xml b/.idea/encodings.xml
index 1bc9cd60..cfb629fb 100644
--- a/.idea/encodings.xml
+++ b/.idea/encodings.xml
@@ -12,6 +12,7 @@
     <file url="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginMedia" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginNativeAudio" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginPlayoutLog" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginWebAPI" charset="UTF-8" />
     <file url="PROJECT" charset="UTF-8" />
   </component>
 </project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
index 9bd97900..71a1574f 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -12,6 +12,7 @@
       <module fileurl="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginMedia/PlayWallPluginMedia.iml" filepath="$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginMedia/PlayWallPluginMedia.iml" />
       <module fileurl="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginNativeAudio/PlayWallPluginNativeAudio.iml" filepath="$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginNativeAudio/PlayWallPluginNativeAudio.iml" />
       <module fileurl="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginPlayoutLog/PlayWallPluginPlayoutLog.iml" filepath="$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginPlayoutLog/PlayWallPluginPlayoutLog.iml" />
+      <module fileurl="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginWebAPI/PlayWallPluginWebAPI.iml" filepath="$PROJECT_DIR$/PlayWallPlugins/PlayWallPluginWebAPI/PlayWallPluginWebAPI.iml" />
       <module fileurl="file://$PROJECT_DIR$/PlayWallPlugins/PlayWallPlugins.iml" filepath="$PROJECT_DIR$/PlayWallPlugins/PlayWallPlugins.iml" />
     </modules>
   </component>
diff --git a/PlayWallCore/pom.xml b/PlayWallCore/pom.xml
index b9e117a4..7bb61491 100644
--- a/PlayWallCore/pom.xml
+++ b/PlayWallCore/pom.xml
@@ -22,6 +22,10 @@
             <groupId>de.thecodelabs</groupId>
             <artifactId>libLogger</artifactId>
         </dependency>
+        <dependency>
+            <groupId>de.thecodelabs</groupId>
+            <artifactId>libLogger-slf4j</artifactId>
+        </dependency>
         <dependency>
             <groupId>de.thecodelabs</groupId>
             <artifactId>libPlugins-core</artifactId>
@@ -36,13 +40,6 @@
             <version>${versionizer-api.version}</version>
         </dependency>
 
-        <!-- PDF-->
-        <dependency>
-            <groupId>com.itextpdf</groupId>
-            <artifactId>itextpdf</artifactId>
-            <version>${itextpdf.version}</version>
-        </dependency>
-
         <!--Audio-->
         <dependency>
             <groupId>javazoom</groupId>
@@ -112,6 +109,21 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- Plugin Dependencies -->
+
+        <!-- PDF-->
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>${itextpdf.version}</version>
+        </dependency>
+
+        <!--Spark-->
+        <dependency>
+            <groupId>com.sparkjava</groupId>
+            <artifactId>spark-core</artifactId>
+            <version>${spark-core.version}</version>
+        </dependency>
     </dependencies>
 
     <distributionManagement>
@@ -149,5 +161,4 @@
             </snapshots>
         </repository>
     </repositories>
-
 </project>
\ No newline at end of file
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/pom.xml b/PlayWallPlugins/PlayWallPluginWebAPI/pom.xml
new file mode 100644
index 00000000..5433e8b8
--- /dev/null
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/pom.xml
@@ -0,0 +1,137 @@
+<?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">
+    <parent>
+        <artifactId>PlayWallPlugins</artifactId>
+        <groupId>de.tobias.playpad</groupId>
+        <version>6.2.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>PlayWallPluginWebAPI</artifactId>
+
+    <properties>
+        <project.outputDirectory>../../build/${project.version}</project.outputDirectory>
+        <project.artifactName>${project.artifactId}-v${project.version}</project.artifactName>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>de.tobias.playpad</groupId>
+            <artifactId>PlayWallCore</artifactId>
+            <version>${project.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sparkjava</groupId>
+            <artifactId>spark-core</artifactId>
+            <version>${spark-core.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>de.thecodelabs</groupId>
+                <artifactId>versionizer-maven-plugin</artifactId>
+                <version>1.0.1</version>
+                <configuration>
+                    <resourceFile>plugin.yml</resourceFile>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>resource-fill</id>
+                        <goals>
+                            <goal>resource-fill</goal>
+                        </goals>
+                        <phase>compile</phase>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <!--Scala Plugin-->
+            <plugin>
+                <groupId>net.alchim31.maven</groupId>
+                <artifactId>scala-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>scala-compile-first</id>
+                        <phase>process-resources</phase>
+                        <goals>
+                            <goal>add-source</goal>
+                            <goal>compile</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>scala-test-compile</id>
+                        <phase>process-test-resources</phase>
+                        <goals>
+                            <goal>testCompile</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-assembly-plugin</artifactId>
+                <version>3.1.1</version>
+                <configuration>
+                    <descriptorRefs>
+                        <descriptorRef>jar-with-dependencies</descriptorRef>
+                    </descriptorRefs>
+                    <outputDirectory>${project.outputDirectory}</outputDirectory>
+                    <finalName>${project.artifactName}</finalName>
+                    <appendAssemblyId>false</appendAssemblyId>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>assemble-all</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>single</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+    <distributionManagement>
+        <repository>
+            <id>release</id>
+            <name>TheCodeLabs-releases</name>
+            <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-release</url>
+        </repository>
+        <snapshotRepository>
+            <id>snapshots</id>
+            <name>TheCodeLabs-snapshots</name>
+            <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-snapshots</url>
+        </snapshotRepository>
+    </distributionManagement>
+
+    <repositories>
+        <repository>
+            <id>release</id>
+            <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-release</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+        <repository>
+            <id>snapshots</id>
+            <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-snapshots</url>
+            <releases>
+                <enabled>false</enabled>
+            </releases>
+            <snapshots>
+                <enabled>true</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
+
+</project>
\ No newline at end of file
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/resources/plugin.yml b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/resources/plugin.yml
new file mode 100644
index 00000000..20f98f59
--- /dev/null
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/resources/plugin.yml
@@ -0,0 +1,6 @@
+main: "de.tobias.playpad.plugin.api.WebApiPlugin"
+name: "WebApiPlugin"
+artifactId: "${pom.artifactId}"
+groupId: "${pom.groupId}"
+version: "${pom.version}"
+build: 1
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
new file mode 100644
index 00000000..a971e036
--- /dev/null
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
@@ -0,0 +1,22 @@
+package de.tobias.playpad.plugin.api
+
+import de.thecodelabs.logger.Logger
+import de.thecodelabs.plugins.{Plugin, PluginDescriptor}
+import de.tobias.playpad.plugin.api.websocket.WebSocketHandler
+import spark.Spark
+
+class WebApiPlugin extends Plugin {
+	override def startup(pluginDescriptor: PluginDescriptor): Unit = {
+		Logger.debug("Enable Web API Plugin")
+
+		Spark.port(9876)
+		Spark.webSocket("/api", classOf[WebSocketHandler])
+		Spark.get("/", (_, _) => "PlayWall API")
+	}
+
+	override def shutdown(): Unit = {
+		Logger.debug("Disable Web API Plugin")
+
+		Spark.stop()
+	}
+}
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala
new file mode 100644
index 00000000..aa71195b
--- /dev/null
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala
@@ -0,0 +1,31 @@
+package de.tobias.playpad.plugin.api.websocket
+
+import java.util.concurrent.ConcurrentLinkedQueue
+
+import de.thecodelabs.logger.Logger
+import org.eclipse.jetty.websocket.api.Session
+import org.eclipse.jetty.websocket.api.annotations._
+
+@WebSocket
+class WebSocketHandler {
+
+	private val sessions = new ConcurrentLinkedQueue[Session]
+
+	@OnWebSocketConnect def connected(session: Session): Unit = {
+		Logger.debug("WebSocket Client connected")
+		sessions.add(session)
+	}
+
+	@OnWebSocketClose def closed(session: Session, statusCode: Int, reason: String): Unit = {
+		Logger.debug("WebSocket Client disconnected")
+		sessions.remove(session)
+	}
+
+	@OnWebSocketMessage def onRead(session: Session, message: String): Unit = {
+		Logger.debug("Read from WebSocket: {0}", message)
+	}
+
+	@OnWebSocketError def onError(session: Session, error: Throwable): Unit = {
+		Logger.warning(error.getMessage)
+	}
+}
diff --git a/PlayWallPlugins/pom.xml b/PlayWallPlugins/pom.xml
index 2a798907..5ecdd6e1 100644
--- a/PlayWallPlugins/pom.xml
+++ b/PlayWallPlugins/pom.xml
@@ -19,6 +19,7 @@
         <module>PlayWallPluginMedia</module>
         <module>PlayWallPluginNativeAudio</module>
         <module>PlayWallPluginPlayoutLog</module>
+        <module>PlayWallPluginWebAPI</module>
     </modules>
 
 </project>
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index c6962394..96417509 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,8 @@
 
         <itextpdf.version>5.5.13</itextpdf.version>
 
+        <spark-core.version>[2.9.0,)</spark-core.version>
+
         <jna.version>5.2.0</jna.version>
 
         <scala-library.version>2.13.0</scala-library.version>
-- 
GitLab