camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/3] camel git commit: [CAMEL-9392] Add ability to pass parameters within websocket url and handle them in onOpen event
Date Sat, 05 Dec 2015 13:33:44 GMT
[CAMEL-9392] Add ability to pass parameters within websocket url and handle them in onOpen
event


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

Branch: refs/heads/master
Commit: 1d762e5114b0411a64f1b27352f72f57029fc018
Parents: 2d77c3a
Author: Pavlo Kletsko <pkletsko@gmail.com>
Authored: Sat Dec 5 13:32:51 2015 +0100
Committer: Pavlo Kletsko <pkletsko@gmail.com>
Committed: Sat Dec 5 13:32:51 2015 +0100

----------------------------------------------------------------------
 .../atmosphere/websocket/WebsocketConsumer.java | 20 +++++++++++++
 .../WebsocketRouteWithInitParamTest.java        | 31 ++++++++++++++++++++
 2 files changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1d762e51/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketConsumer.java
b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketConsumer.java
index b8267ac..86bd016 100644
--- a/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketConsumer.java
+++ b/components/camel-atmosphere-websocket/src/main/java/org/apache/camel/component/atmosphere/websocket/WebsocketConsumer.java
@@ -17,6 +17,8 @@
 package org.apache.camel.component.atmosphere.websocket;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -37,6 +39,7 @@ import org.atmosphere.websocket.WebSocketProtocol;
 public class WebsocketConsumer extends ServletConsumer {
     private AtmosphereFramework framework;
     private boolean enableEventsResending;
+    private Map<String, String> queryMap = new HashMap<>();
 
     public WebsocketConsumer(WebsocketEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -63,6 +66,7 @@ public class WebsocketConsumer extends ServletConsumer {
     
     void service(HttpServletRequest request, HttpServletResponse response, boolean enableEventsResending)
throws IOException, ServletException {
         this.enableEventsResending = enableEventsResending;
+        this.queryMap = getQueryMap(request.getQueryString());
         framework.doCometSupport(AtmosphereRequestImpl.wrap(request), AtmosphereResponseImpl.wrap(response));
     }
 
@@ -90,6 +94,10 @@ public class WebsocketConsumer extends ServletConsumer {
         exchange.getIn().setHeader(WebsocketConstants.CONNECTION_KEY, connectionKey);
         exchange.getIn().setHeader(WebsocketConstants.EVENT_TYPE, eventType);
 
+        for (Map.Entry<String, String> param : queryMap.entrySet()) {
+            exchange.getIn().setHeader(param.getKey(), param.getValue());
+        }
+
         // send exchange using the async routing engine
         getAsyncProcessor().process(exchange, new AsyncCallback() {
             public void done(boolean doneSync) {
@@ -103,4 +111,16 @@ public class WebsocketConsumer extends ServletConsumer {
     public boolean isEnableEventsResending() {
         return enableEventsResending;
     }
+
+    private Map<String, String> getQueryMap(String query) {
+        Map<String, String> map = new HashMap<>();
+        if (query != null) {
+            String[] params = query.split("&");
+            for (String param : params) {
+                String[] nameval = param.split("=");
+                map.put(nameval[0], nameval[1]);
+            }
+        }
+        return map;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1d762e51/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketRouteWithInitParamTest.java
----------------------------------------------------------------------
diff --git a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketRouteWithInitParamTest.java
b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketRouteWithInitParamTest.java
index 13e3e77..f0f8182 100644
--- a/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketRouteWithInitParamTest.java
+++ b/components/camel-atmosphere-websocket/src/test/java/org/apache/camel/component/atmosphere/websocket/WebsocketRouteWithInitParamTest.java
@@ -30,6 +30,13 @@ public class WebsocketRouteWithInitParamTest extends WebsocketCamelRouterWithIni
         wsclient.close();
     }
 
+    @Test
+    public void testPassParametersWebsocketOnOpen() throws Exception {
+        TestClient wsclient = new TestClient("ws://localhost:" + PORT + "/hola1?param1=value1&param2=value2");
+        wsclient.connect();
+        wsclient.close();
+    }
+
     // START SNIPPET: payload
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
@@ -40,6 +47,13 @@ public class WebsocketRouteWithInitParamTest extends WebsocketCamelRouterWithIni
                         checkEventsResendingEnabled(exchange);
                     }
                 });
+
+                // route for events resending enabled with parameters from url
+                from("atmosphere-websocket:///hola1").to("log:info").process(new Processor()
{
+                    public void process(final Exchange exchange) throws Exception {
+                        checkPassedParameters(exchange);
+                    }
+                });
             }
         };
     }
@@ -56,5 +70,22 @@ public class WebsocketRouteWithInitParamTest extends WebsocketCamelRouterWithIni
             assertTrue(eventType.equals(WebsocketConstants.ONOPEN_EVENT_TYPE) || eventType.equals(WebsocketConstants.ONCLOSE_EVENT_TYPE)
|| eventType.equals(WebsocketConstants.ONERROR_EVENT_TYPE));
         }
     }
+
+    private static void checkPassedParameters(Exchange exchange) {
+        Object connectionKey = exchange.getIn().getHeader(WebsocketConstants.CONNECTION_KEY);
+        Object eventType = exchange.getIn().getHeader(WebsocketConstants.EVENT_TYPE);
+        Object msg = exchange.getIn().getBody();
+
+        assertEquals(null, msg);
+        assertTrue(connectionKey != null);
+
+        if ((eventType instanceof Integer) && eventType.equals(WebsocketConstants.ONOPEN_EVENT_TYPE))
{
+
+            String param1 = (String)exchange.getIn().getHeader("param1");
+            String param2 = (String)exchange.getIn().getHeader("param2");
+
+            assertTrue(param1.equals("value1") && param2.equals("value2"));
+        }
+    }
     // END SNIPPET: payload
 }


Mime
View raw message