camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8861: Add a factory to support custom websocket sub-protocol. Thanks to Emilien for the patch.
Date Tue, 16 Jun 2015 08:31:41 GMT
Repository: camel
Updated Branches:
  refs/heads/master 75745c5a6 -> 69ca07896


CAMEL-8861: Add a factory to support custom websocket sub-protocol. Thanks to Emilien for
the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/69ca0789
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/69ca0789
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/69ca0789

Branch: refs/heads/master
Commit: 69ca07896868751b39d682be4f3f76d64a828552
Parents: 75745c5
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Jun 16 10:37:09 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Jun 16 10:37:09 2015 +0200

----------------------------------------------------------------------
 .../websocket/DefaultWebsocketFactory.java      | 33 ++++++++++++++++++++
 .../component/websocket/WebSocketFactory.java   | 30 ++++++++++++++++++
 .../component/websocket/WebsocketComponent.java | 20 +++++++++++-
 .../websocket/WebsocketComponentServlet.java    | 23 ++++++++++++--
 .../WebsocketComponentServletTest.java          | 13 ++++++--
 5 files changed, 112 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/69ca0789/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/DefaultWebsocketFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/DefaultWebsocketFactory.java
b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/DefaultWebsocketFactory.java
new file mode 100644
index 0000000..6fb0124
--- /dev/null
+++ b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/DefaultWebsocketFactory.java
@@ -0,0 +1,33 @@
+/**
+ * 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.camel.component.websocket;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.eclipse.jetty.websocket.WebSocket;
+
+/**
+ * Default websocket factory.
+ * Used when no custom websocket is needed.
+ */
+public class DefaultWebsocketFactory implements WebSocketFactory {
+
+    @Override
+    public WebSocket newInstance(HttpServletRequest request, String protocol, NodeSynchronization
sync, WebsocketConsumer consumer) {
+        return new DefaultWebsocket(sync, consumer);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/69ca0789/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebSocketFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebSocketFactory.java
b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebSocketFactory.java
new file mode 100644
index 0000000..6f06038
--- /dev/null
+++ b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebSocketFactory.java
@@ -0,0 +1,30 @@
+/**
+ * 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.camel.component.websocket;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.eclipse.jetty.websocket.WebSocket;
+
+/**
+ * Web socket factory interface.
+ */
+public interface WebSocketFactory {
+
+    WebSocket newInstance(HttpServletRequest request, String protocol, NodeSynchronization
sync, WebsocketConsumer consumer);
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/69ca0789/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponent.java
b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponent.java
index 379c730..a34d08a 100644
--- a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponent.java
+++ b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponent.java
@@ -80,6 +80,7 @@ public class WebsocketComponent extends UriEndpointComponent {
     protected String sslKeyPassword;
     protected String sslPassword;
     protected String sslKeystore;
+    protected Map<String, WebSocketFactory> socketFactory; 
 
     /**
      * Map for storing servlets. {@link WebsocketComponentServlet} is identified by pathSpec
{@link String}.
@@ -114,6 +115,11 @@ public class WebsocketComponent extends UriEndpointComponent {
 
     public WebsocketComponent() {
         super(WebsocketEndpoint.class);
+
+        if (this.socketFactory == null) {
+            this.socketFactory = new HashMap<String, WebSocketFactory>();
+            this.socketFactory.put("default", new DefaultWebsocketFactory());
+        }
     }
 
     /**
@@ -460,7 +466,7 @@ public class WebsocketComponent extends UriEndpointComponent {
     }
 
     protected WebsocketComponentServlet createServlet(NodeSynchronization sync, String pathSpec,
Map<String, WebsocketComponentServlet> servlets, ServletContextHandler handler) {
-        WebsocketComponentServlet servlet = new WebsocketComponentServlet(sync);
+        WebsocketComponentServlet servlet = new WebsocketComponentServlet(sync, socketFactory);
         servlets.put(pathSpec, servlet);
         handler.addServlet(new ServletHolder(servlet), pathSpec);
         return servlet;
@@ -765,6 +771,18 @@ public class WebsocketComponent extends UriEndpointComponent {
         this.sslContextParameters = sslContextParameters;
     }
 
+    public Map<String, WebSocketFactory> getSocketFactory() {
+        return socketFactory;
+    }
+
+    public void setSocketFactory(Map<String, WebSocketFactory> socketFactory) {
+        this.socketFactory = socketFactory;
+
+        if (!this.socketFactory.containsKey("default")) {
+            this.socketFactory.put("default", new DefaultWebsocketFactory());
+        }
+    }
+
     public static HashMap<String, ConnectorRef> getConnectors() {
         return CONNECTORS;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/69ca0789/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponentServlet.java
----------------------------------------------------------------------
diff --git a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponentServlet.java
b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponentServlet.java
index bf7b1bc..078c61a 100644
--- a/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponentServlet.java
+++ b/components/camel-websocket/src/main/java/org/apache/camel/component/websocket/WebsocketComponentServlet.java
@@ -16,9 +16,9 @@
  */
 package org.apache.camel.component.websocket;
 
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-
 import javax.servlet.http.HttpServletRequest;
 
 import org.eclipse.jetty.websocket.WebSocket;
@@ -34,9 +34,11 @@ public class WebsocketComponentServlet extends WebSocketServlet {
     private WebsocketConsumer consumer;
 
     private ConcurrentMap<String, WebsocketConsumer> consumers = new ConcurrentHashMap<String,
WebsocketConsumer>();
+    private Map<String, WebSocketFactory> socketFactory;
 
-    public WebsocketComponentServlet(NodeSynchronization sync) {
+    public WebsocketComponentServlet(NodeSynchronization sync, Map<String, WebSocketFactory>
socketFactory) {
         this.sync = sync;
+        this.socketFactory = socketFactory;
     }
 
     public WebsocketConsumer getConsumer() {
@@ -59,7 +61,22 @@ public class WebsocketComponentServlet extends WebSocketServlet {
 
     @Override
     public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
-        return new DefaultWebsocket(sync, consumer);
+        String protocolKey = protocol;
+
+        if (protocol == null || !socketFactory.containsKey(protocol)) {
+            log.debug("No factory found for the socket protocol: {}, returning default implementation",
protocol);
+            protocolKey = "default";
+        }
+
+        WebSocketFactory factory = socketFactory.get(protocolKey);
+        return factory.newInstance(request, protocolKey, sync, consumer);
     }
 
+    public Map<String, WebSocketFactory> getSocketFactory() {
+        return socketFactory;
+    }
+
+    public void setSocketFactory(Map<String, WebSocketFactory> socketFactory) {
+        this.socketFactory = socketFactory;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/69ca0789/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
----------------------------------------------------------------------
diff --git a/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
b/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
index 2ff0735..2b24fa5 100644
--- a/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
+++ b/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
@@ -16,14 +16,15 @@
  */
 package org.apache.camel.component.websocket;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.servlet.http.HttpServletRequest;
 
 import org.eclipse.jetty.websocket.WebSocket;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-
 import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
@@ -53,9 +54,15 @@ public class WebsocketComponentServletTest {
 
     private WebsocketComponentServlet websocketComponentServlet;
 
+    private Map<String, WebSocketFactory> socketFactory;
+    
+    
     @Before
     public void setUp() throws Exception {
-        websocketComponentServlet = new WebsocketComponentServlet(sync);
+        socketFactory = new HashMap<String, WebSocketFactory>();
+        socketFactory.put("default", new DefaultWebsocketFactory());
+        
+        websocketComponentServlet = new WebsocketComponentServlet(sync, socketFactory);
     }
 
     @Test


Mime
View raw message