From 77e42fb05addd1affc10b55c59d638a23acea181 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 6 May 2015 10:23:48 -0700 Subject: [PATCH] Updating WebSocket JettyListenerEventDriver for new Listeners + Basing implementation on instances of WebSocketConnectionListener + Adding support for: * WebSocketListener * WebSocketPartialListener * WebSocketPingPongListener * WebSocketFrameListener --- .../events/JettyListenerEventDriver.java | 84 ++++++++++++++++--- .../common/events/JettyListenerImpl.java | 5 +- .../common/events/EventDriverTest.java | 2 +- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerEventDriver.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerEventDriver.java index 376a1ce96b2..9d5e8a534c1 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerEventDriver.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerEventDriver.java @@ -25,12 +25,19 @@ import java.nio.ByteBuffer; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.websocket.api.WebSocketConnectionListener; +import org.eclipse.jetty.websocket.api.WebSocketFrameListener; import org.eclipse.jetty.websocket.api.WebSocketListener; +import org.eclipse.jetty.websocket.api.WebSocketPartialListener; +import org.eclipse.jetty.websocket.api.WebSocketPingPongListener; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.extensions.Frame; +import org.eclipse.jetty.websocket.api.extensions.Frame.Type; import org.eclipse.jetty.websocket.common.CloseInfo; +import org.eclipse.jetty.websocket.common.frames.ReadOnlyDelegatedFrame; import org.eclipse.jetty.websocket.common.message.SimpleBinaryMessage; import org.eclipse.jetty.websocket.common.message.SimpleTextMessage; +import org.eclipse.jetty.websocket.common.util.Utf8PartialBuilder; /** * Handler for {@link WebSocketListener} based User WebSocket implementations. @@ -38,10 +45,11 @@ import org.eclipse.jetty.websocket.common.message.SimpleTextMessage; public class JettyListenerEventDriver extends AbstractEventDriver { private static final Logger LOG = Log.getLogger(JettyListenerEventDriver.class); - private final WebSocketListener listener; + private final WebSocketConnectionListener listener; + private Utf8PartialBuilder utf8Partial; private boolean hasCloseBeenCalled = false; - public JettyListenerEventDriver(WebSocketPolicy policy, WebSocketListener listener) + public JettyListenerEventDriver(WebSocketPolicy policy, WebSocketConnectionListener listener) { super(policy,listener); this.listener = listener; @@ -50,18 +58,29 @@ public class JettyListenerEventDriver extends AbstractEventDriver @Override public void onBinaryFrame(ByteBuffer buffer, boolean fin) throws IOException { - if (activeMessage == null) + if (listener instanceof WebSocketListener) { - activeMessage = new SimpleBinaryMessage(this); + if (activeMessage == null) + { + activeMessage = new SimpleBinaryMessage(this); + } + + appendMessage(buffer,fin); } - appendMessage(buffer,fin); + if (listener instanceof WebSocketPartialListener) + { + ((WebSocketPartialListener)listener).onWebSocketPartialBinary(buffer.slice().asReadOnlyBuffer(),fin); + } } @Override public void onBinaryMessage(byte[] data) { - listener.onWebSocketBinary(data,0,data.length); + if (listener instanceof WebSocketListener) + { + ((WebSocketListener)listener).onWebSocketBinary(data,0,data.length); + } } @Override @@ -96,7 +115,22 @@ public class JettyListenerEventDriver extends AbstractEventDriver @Override public void onFrame(Frame frame) { - /* ignore, not supported by WebSocketListener */ + if (listener instanceof WebSocketFrameListener) + { + ((WebSocketFrameListener)listener).onWebSocketFrame(new ReadOnlyDelegatedFrame(frame)); + } + + if (listener instanceof WebSocketPingPongListener) + { + if (frame.getType() == Type.PING) + { + ((WebSocketPingPongListener)listener).onWebSocketPing(frame.getPayload().asReadOnlyBuffer()); + } + else if (frame.getType() == Type.PONG) + { + ((WebSocketPingPongListener)listener).onWebSocketPong(frame.getPayload().asReadOnlyBuffer()); + } + } } @Override @@ -114,18 +148,46 @@ public class JettyListenerEventDriver extends AbstractEventDriver @Override public void onTextFrame(ByteBuffer buffer, boolean fin) throws IOException { - if (activeMessage == null) + if (listener instanceof WebSocketListener) { - activeMessage = new SimpleTextMessage(this); + if (activeMessage == null) + { + activeMessage = new SimpleTextMessage(this); + } + + appendMessage(buffer,fin); } - appendMessage(buffer,fin); + if (listener instanceof WebSocketPartialListener) + { + if (utf8Partial == null) + { + utf8Partial = new Utf8PartialBuilder(); + } + + String partial = utf8Partial.toPartialString(buffer); + + ((WebSocketPartialListener)listener).onWebSocketPartialText(partial,fin); + + if (fin) + { + partial = null; + } + } } + /** + * Whole Message event. + * + * @param message the whole message + */ @Override public void onTextMessage(String message) { - listener.onWebSocketText(message); + if (listener instanceof WebSocketListener) + { + ((WebSocketListener)listener).onWebSocketText(message); + } } @Override diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerImpl.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerImpl.java index 720864a8664..0da607523a6 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerImpl.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/JettyListenerImpl.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.websocket.common.events; +import org.eclipse.jetty.websocket.api.WebSocketConnectionListener; import org.eclipse.jetty.websocket.api.WebSocketListener; import org.eclipse.jetty.websocket.api.WebSocketPolicy; @@ -26,7 +27,7 @@ public class JettyListenerImpl implements EventDriverImpl @Override public EventDriver create(Object websocket, WebSocketPolicy policy) { - WebSocketListener listener = (WebSocketListener)websocket; + WebSocketConnectionListener listener = (WebSocketConnectionListener)websocket; return new JettyListenerEventDriver(policy,listener); } @@ -39,6 +40,6 @@ public class JettyListenerImpl implements EventDriverImpl @Override public boolean supports(Object websocket) { - return (websocket instanceof WebSocketListener); + return (websocket instanceof WebSocketConnectionListener); } } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java index 04dc8bced4c..006f2d6dbe6 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java @@ -165,7 +165,7 @@ public class EventDriverTest } @Test - public void testListener_Text() throws Exception + public void testListenerBasic_Text() throws Exception { ListenerBasicSocket socket = new ListenerBasicSocket(); EventDriver driver = wrap(socket);