You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/02/06 13:04:22 UTC

[camel-spring-boot] branch master updated: CAMEL-14502: Make jsr356 websocket component work with Spring Boot embedded servers

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/master by this push:
     new 71ab8c6  CAMEL-14502: Make jsr356 websocket component work with Spring Boot embedded servers
71ab8c6 is described below

commit 71ab8c6d4bb25f46b0568dbdad64da2d56e9b842
Author: James Netherton <ja...@gmail.com>
AuthorDate: Thu Feb 6 12:33:49 2020 +0000

    CAMEL-14502: Make jsr356 websocket component work with Spring Boot embedded servers
---
 .../camel-websocket-jsr356-starter/pom.xml         | 10 ++---
 ...6WebSocketContextListenerAutoConfiguration.java | 47 ++++++++++++++++++++++
 .../src/main/resources/META-INF/spring.factories   |  3 +-
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/components-starter/camel-websocket-jsr356-starter/pom.xml b/components-starter/camel-websocket-jsr356-starter/pom.xml
index 065e127..7c6b444 100644
--- a/components-starter/camel-websocket-jsr356-starter/pom.xml
+++ b/components-starter/camel-websocket-jsr356-starter/pom.xml
@@ -35,15 +35,15 @@
       <version>${spring-boot-version}</version>
     </dependency>
     <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-web</artifactId>
+      <version>${spring-boot-version}</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-websocket-jsr356</artifactId>
       <version>${camel-version}</version>
     </dependency>
-    <dependency>
-      <groupId>org.apache.tomcat</groupId>
-      <artifactId>tomcat-websocket-api</artifactId>
-      <version>${tomcat-version}</version>
-    </dependency>
     <!--START OF GENERATED CODE-->
     <dependency>
       <groupId>org.apache.camel.springboot</groupId>
diff --git a/components-starter/camel-websocket-jsr356-starter/src/main/java/org/apache/camel/websocket/jsr356/springboot/JSR356WebSocketContextListenerAutoConfiguration.java b/components-starter/camel-websocket-jsr356-starter/src/main/java/org/apache/camel/websocket/jsr356/springboot/JSR356WebSocketContextListenerAutoConfiguration.java
new file mode 100644
index 0000000..152e841
--- /dev/null
+++ b/components-starter/camel-websocket-jsr356-starter/src/main/java/org/apache/camel/websocket/jsr356/springboot/JSR356WebSocketContextListenerAutoConfiguration.java
@@ -0,0 +1,47 @@
+package org.apache.camel.websocket.jsr356.springboot;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.websocket.server.ServerContainer;
+
+import org.apache.camel.websocket.jsr356.JSR356WebSocketComponent;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.web.servlet.ServletContextInitializer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import static java.util.Optional.ofNullable;
+
+/**
+ * Auto configuration class which sets up a ServletContextInitializer to register the websocket
+ * {@link ServerContainer} with the JSR356WebSocketComponent.
+ *
+ * This is needed for embedded server mode, which ignores the ServletContainerInitializer provided by the camel component.
+ */
+@Configuration(proxyBeanMethods = false)
+@AutoConfigureBefore(JSR356WebSocketComponentAutoConfiguration.class)
+public class JSR356WebSocketContextListenerAutoConfiguration {
+
+    @Bean
+    public ServletContextInitializer jsr356ServletContextInitializer() {
+        return new ServletContextInitializer() {
+            @Override
+            public void onStartup(ServletContext servletContext) throws ServletException {
+                servletContext.addListener(new ServletContextListener() {
+                    @Override
+                    public void contextInitialized(ServletContextEvent sce) {
+                        final String contextPath = sce.getServletContext().getContextPath();
+                        ofNullable(sce.getServletContext().getAttribute(ServerContainer.class.getName())).map(ServerContainer.class::cast)
+                                .ifPresent(container -> JSR356WebSocketComponent.registerServer(contextPath, container));
+                    }
+
+                    @Override
+                    public void contextDestroyed(ServletContextEvent sce) {
+                        JSR356WebSocketComponent.unregisterServer(sce.getServletContext().getContextPath());
+                    }
+                });
+            }
+        };
+    }
+}
diff --git a/components-starter/camel-websocket-jsr356-starter/src/main/resources/META-INF/spring.factories b/components-starter/camel-websocket-jsr356-starter/src/main/resources/META-INF/spring.factories
index 58d720b..4bd816a 100644
--- a/components-starter/camel-websocket-jsr356-starter/src/main/resources/META-INF/spring.factories
+++ b/components-starter/camel-websocket-jsr356-starter/src/main/resources/META-INF/spring.factories
@@ -16,4 +16,5 @@
 ## ---------------------------------------------------------------------------
 
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-org.apache.camel.websocket.jsr356.springboot.JSR356WebSocketComponentAutoConfiguration
+org.apache.camel.websocket.jsr356.springboot.JSR356WebSocketComponentAutoConfiguration, \
+org.apache.camel.websocket.jsr356.springboot.JSR356WebSocketContextListenerAutoConfiguration