cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject git commit: make WebSocketTransportFactory instantiable for either of the available jetty versions
Date Wed, 13 Aug 2014 09:13:28 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 4e374cc6e -> 7e95279dd


make WebSocketTransportFactory instantiable for either of the available jetty versions


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/7e95279d
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7e95279d
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7e95279d

Branch: refs/heads/master
Commit: 7e95279dd1170f5878687d7eea27f7d7f03a434b
Parents: 4e374cc
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Wed Aug 13 11:11:11 2014 +0200
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Wed Aug 13 11:11:11 2014 +0200

----------------------------------------------------------------------
 .../websocket/WebSocketDestinationFactory.java  |  3 +-
 .../jetty/JettyWebSocketDestination.java        | 28 ++++-----
 .../websocket/jetty/JettyWebSocketManager.java  |  4 +-
 .../jetty/JettyWebSocketServletDestination.java | 27 +++++----
 .../jetty9/Jetty9WebSocketDestination.java      | 60 +++++++++++---------
 .../jetty/JettyWebSocketManagerTest.java        |  9 ++-
 6 files changed, 74 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/WebSocketDestinationFactory.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/WebSocketDestinationFactory.java
b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/WebSocketDestinationFactory.java
index 8cdf745..fbe658f 100644
--- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/WebSocketDestinationFactory.java
+++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/WebSocketDestinationFactory.java
@@ -56,8 +56,9 @@ public class WebSocketDestinationFactory implements HttpDestinationFactory
{
                 .getExtension(JettyHTTPServerEngineFactory.class);
             if (serverEngineFactory.isJetty8()) {
                 return new JettyWebSocketDestination(bus, registry, endpointInfo, serverEngineFactory);
+            } else {
+                return new Jetty9WebSocketDestination(bus, registry, endpointInfo, serverEngineFactory);
             }
-            return new Jetty9WebSocketDestination(bus, registry, endpointInfo, serverEngineFactory);
         } else {
             //REVISIT other way of getting the registry of http so that the plain cxf servlet
finds the destination?
             registry = getDestinationRegistry(bus);

http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketDestination.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketDestination.java
b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketDestination.java
index c2c2234..a3a6b8a 100644
--- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketDestination.java
+++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketDestination.java
@@ -42,14 +42,14 @@ import org.eclipse.jetty.websocket.WebSocketFactory;
  * 
  */
 public class JettyWebSocketDestination extends JettyHTTPDestination implements 
-    WebSocketDestinationService, WebSocketFactory.Acceptor {
+    WebSocketDestinationService {
     private JettyWebSocketManager webSocketManager;
-
+    
     public JettyWebSocketDestination(Bus bus, DestinationRegistry registry, EndpointInfo
ei,
                                      JettyHTTPServerEngineFactory serverEngineFactory) throws
IOException {
         super(bus, registry, ei, serverEngineFactory);
         webSocketManager = new JettyWebSocketManager();
-        webSocketManager.init(this);
+        webSocketManager.init(this, new Acceptor());
     }
     
     @Override
@@ -82,16 +82,6 @@ public class JettyWebSocketDestination extends JettyHTTPDestination implements
     }
 
     @Override
-    public boolean checkOrigin(HttpServletRequest arg0, String arg1) {
-        return true;
-    }
-
-    @Override
-    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
-        return new JettyWebSocket(webSocketManager, request, protocol);
-    }
-
-    @Override
     public void shutdown() {
         try {
             webSocketManager.destroy();
@@ -102,4 +92,16 @@ public class JettyWebSocketDestination extends JettyHTTPDestination implements
         }
     }
 
+    private class Acceptor implements WebSocketFactory.Acceptor {
+
+        @Override
+        public boolean checkOrigin(HttpServletRequest request, String protocol) {
+            return true;
+        }
+
+        @Override
+        public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
{
+            return new JettyWebSocket(webSocketManager, request, protocol);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManager.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManager.java
b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManager.java
index 7aef4b7..56489a1 100644
--- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManager.java
+++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManager.java
@@ -45,11 +45,11 @@ class JettyWebSocketManager {
     private ServletContext servletContext;
     private Executor executor;
 
-    public void init(AbstractHTTPDestination dest) {
+    public void init(AbstractHTTPDestination dest, Acceptor acceptor) {
         this.destination = dest;
 
         //TODO customize websocket factory configuration options when using the destination.
-        webSocketFactory = new WebSocketFactory((Acceptor)dest, 8192);
+        webSocketFactory = new WebSocketFactory(acceptor, 8192);
 
         // the executor for decoupling the service invocation from websocket's onMessage
call which is
         // synchronously blocked

http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketServletDestination.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketServletDestination.java
b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketServletDestination.java
index 296410b..314a820 100644
--- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketServletDestination.java
+++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketServletDestination.java
@@ -38,14 +38,14 @@ import org.eclipse.jetty.websocket.WebSocketFactory;
  * 
  */
 public class JettyWebSocketServletDestination extends ServletDestination implements 
-    WebSocketDestinationService, WebSocketFactory.Acceptor {
+    WebSocketDestinationService {
     private JettyWebSocketManager webSocketManager;
 
     public JettyWebSocketServletDestination(Bus bus, DestinationRegistry registry, EndpointInfo
ei,
                                             String path) throws IOException {
         super(bus, registry, ei, path);
         webSocketManager = new JettyWebSocketManager();
-        webSocketManager.init(this);
+        webSocketManager.init(this, new Acceptor());
     }
 
     @Override
@@ -65,16 +65,6 @@ public class JettyWebSocketServletDestination extends ServletDestination
impleme
     }
 
     @Override
-    public boolean checkOrigin(HttpServletRequest arg0, String arg1) {
-        return true;
-    }
-
-    @Override
-    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
-        return new JettyWebSocket(webSocketManager, request, protocol);
-    }
-
-    @Override
     public void shutdown() {
         try {
             webSocketManager.destroy();
@@ -84,4 +74,17 @@ public class JettyWebSocketServletDestination extends ServletDestination
impleme
             super.shutdown();
         }
     }
+
+    private class Acceptor implements WebSocketFactory.Acceptor {
+
+        @Override
+        public boolean checkOrigin(HttpServletRequest request, String protocol) {
+            return true;
+        }
+
+        @Override
+        public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
{
+            return new JettyWebSocket(webSocketManager, request, protocol);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty9/Jetty9WebSocketDestination.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty9/Jetty9WebSocketDestination.java
b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty9/Jetty9WebSocketDestination.java
index 0139479..ec2eeb2 100644
--- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty9/Jetty9WebSocketDestination.java
+++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/jetty9/Jetty9WebSocketDestination.java
@@ -61,7 +61,7 @@ import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
  * 
  */
 public class Jetty9WebSocketDestination extends JettyHTTPDestination implements 
-    WebSocketDestinationService,  WebSocketCreator {
+    WebSocketDestinationService {
 
     //REVISIT make these keys configurable
     private String requestIdKey = WebSocketConstants.DEFAULT_REQUEST_ID_KEY;
@@ -80,7 +80,7 @@ public class Jetty9WebSocketDestination extends JettyHTTPDestination implements
         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException
e) {
             throw new RuntimeException(e);
         }
-        webSocketFactory.setCreator(this);
+        webSocketFactory.setCreator(new Creator());
         executor = bus.getExtension(WorkQueueManager.class).getAutomaticWorkQueue();
     }
     
@@ -125,31 +125,6 @@ public class Jetty9WebSocketDestination extends JettyHTTPDestination
implements
         }
     }
     
-    @Override
-    public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
{
-        return new WebSocketAdapter() {
-            Session session;
-            @Override
-            public void onWebSocketConnect(Session session) {
-                this.session = session;
-            }
-            @Override
-            public void onWebSocketBinary(byte[] payload, int offset, int len) {
-                invoke(payload, offset, len, session);
-            }
-            @Override
-            public void onWebSocketText(String message) {
-                //TODO may want use string directly instead of converting it to byte[]
-                try {
-                    byte[] bdata = message.getBytes("utf-8");
-                    onWebSocketBinary(bdata, 0, bdata.length);
-                } catch (UnsupportedEncodingException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                }
-            }
-        };
-    }
     private void invoke(final byte[] data, final int offset, final int length, final Session
session) {
         // invoke the service asynchronously as the jetty websocket's onMessage is synchronously
blocked
         // make sure the byte array passed to this method is immutable, as the websocket
framework
@@ -197,6 +172,37 @@ public class Jetty9WebSocketDestination extends JettyHTTPDestination
implements
     private WebSocketVirtualServletResponse createServletResponse(WebSocketServletHolder
holder) throws IOException {
         return new WebSocketVirtualServletResponse(holder);
     }
+
+    // hide this jetty9 interface here to avoid CNFE on WebSocketCreator
+    private class Creator implements WebSocketCreator {
+
+        @Override
+        public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
{
+            return new WebSocketAdapter() {
+                Session session;
+                @Override
+                public void onWebSocketConnect(Session session) {
+                    this.session = session;
+                }
+                @Override
+                public void onWebSocketBinary(byte[] payload, int offset, int len) {
+                    invoke(payload, offset, len, session);
+                }
+                @Override
+                public void onWebSocketText(String message) {
+                    //TODO may want use string directly instead of converting it to byte[]
+                    try {
+                        byte[] bdata = message.getBytes("utf-8");
+                        onWebSocketBinary(bdata, 0, bdata.length);
+                    } catch (UnsupportedEncodingException e) {
+                        // TODO Auto-generated catch block
+                        e.printStackTrace();
+                    }
+                }
+            };
+        }
+        
+    }
     
     class Jetty9WebSocketHolder implements WebSocketServletHolder {
         final Session session;

http://git-wip-us.apache.org/repos/asf/cxf/blob/7e95279d/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManagerTest.java
----------------------------------------------------------------------
diff --git a/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManagerTest.java
b/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManagerTest.java
index f595fea..d4671fe 100644
--- a/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManagerTest.java
+++ b/rt/transports/websocket/src/test/java/org/apache/cxf/transport/websocket/jetty/JettyWebSocketManagerTest.java
@@ -30,6 +30,7 @@ import org.apache.cxf.workqueue.AutomaticWorkQueue;
 import org.apache.cxf.workqueue.WorkQueueManager;
 import org.easymock.EasyMock;
 import org.easymock.IMocksControl;
+import org.eclipse.jetty.websocket.WebSocketFactory;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -53,6 +54,8 @@ public class JettyWebSocketManagerTest extends Assert {
         JettyWebSocketDestination dest = control.createMock(JettyWebSocketDestination.class);
         setupDestination(dest);
 
+        WebSocketFactory.Acceptor acceptor = control.createMock(WebSocketFactory.Acceptor.class);
+
         HttpServletRequest request = control.createMock(HttpServletRequest.class);
         HttpServletResponse response = control.createMock(HttpServletResponse.class);
         
@@ -61,7 +64,7 @@ public class JettyWebSocketManagerTest extends Assert {
         EasyMock.expectLastCall();
         
         control.replay();
-        jwsm.init(dest);
+        jwsm.init(dest, acceptor);
 
         jwsm.service(request, response);
         control.verify();
@@ -74,6 +77,8 @@ public class JettyWebSocketManagerTest extends Assert {
         JettyWebSocketServletDestination dest = control.createMock(JettyWebSocketServletDestination.class);
         setupDestination(dest);
 
+        WebSocketFactory.Acceptor acceptor = control.createMock(WebSocketFactory.Acceptor.class);
+
         HttpServletRequest request = control.createMock(HttpServletRequest.class);
         HttpServletResponse response = control.createMock(HttpServletResponse.class);
         
@@ -81,7 +86,7 @@ public class JettyWebSocketManagerTest extends Assert {
                     EasyMock.eq(request), EasyMock.eq(response));
         EasyMock.expectLastCall();
         control.replay();
-        jwsm.init(dest);
+        jwsm.init(dest, acceptor);
         
         jwsm.service(request, response);
         control.verify();


Mime
View raw message