diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index e5d708b8beb..d13bab3859b 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.annotations.WebSocket; import org.eclipse.jetty.websocket.api.EventMethodsCache; import org.eclipse.jetty.websocket.api.ExtensionConfig; import org.eclipse.jetty.websocket.api.WebSocketEventDriver; +import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.extensions.Extension; import org.eclipse.jetty.websocket.extensions.deflate.DeflateFrameExtension; @@ -47,7 +48,7 @@ import org.eclipse.jetty.websocket.server.handshake.HandshakeRFC6455; /** * Factory to create WebSocket connections */ -public class WebSocketServerFactory extends AbstractLifeCycle +public class WebSocketServerFactory extends AbstractLifeCycle implements WebSocketCreator { private static final Logger LOG = Log.getLogger(WebSocketServerFactory.class); private final Queue connections = new ConcurrentLinkedQueue(); @@ -70,11 +71,13 @@ public class WebSocketServerFactory extends AbstractLifeCycle private WebSocketPolicy basePolicy; private WebSocketCreator creator; private EventMethodsCache methodsCache; + private Class firstRegisteredClass; public WebSocketServerFactory(WebSocketPolicy policy) { this.basePolicy = policy; this.methodsCache = new EventMethodsCache(); + this.creator = this; // Create supportedVersions List versions = new ArrayList<>(); @@ -132,6 +135,29 @@ public class WebSocketServerFactory extends AbstractLifeCycle } } + @Override + public Object createWebSocket(WebSocketRequest req, WebSocketResponse resp) + { + if (methodsCache.count() < 1) + { + throw new WebSocketException("No WebSockets have been registered with the factory. Cannot use default implementation of WebSocketCreator."); + } + + if (methodsCache.count() > 1) + { + LOG.warn("You have registered more than 1 websocket object, and are using the default WebSocketCreator! Using first registered websocket."); + } + + try + { + return firstRegisteredClass.newInstance(); + } + catch (InstantiationException | IllegalAccessException e) + { + throw new WebSocketException("Unable to create instance of " + firstRegisteredClass,e); + } + } + @Override protected void doStop() throws Exception { @@ -247,6 +273,10 @@ public class WebSocketServerFactory extends AbstractLifeCycle public void register(Class websocketClass) { + if (firstRegisteredClass == null) + { + firstRegisteredClass = websocketClass; + } methodsCache.register(websocketClass); } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketServletRFCTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketServletRFCTest.java index ea66b0e2198..ff67f9c13b8 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketServletRFCTest.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketServletRFCTest.java @@ -44,7 +44,7 @@ import org.junit.Test; public class WebSocketServletRFCTest { @SuppressWarnings("serial") - private static class RFCServlet extends WebSocketServlet + public static class RFCServlet extends WebSocketServlet { @Override public void registerWebSockets(WebSocketServerFactory factory) @@ -53,7 +53,7 @@ public class WebSocketServletRFCTest } } - private static class RFCSocket extends WebSocketAdapter + public static class RFCSocket extends WebSocketAdapter { @Override public void onWebSocketText(String message) @@ -204,8 +204,9 @@ public class WebSocketServletRFCTest ByteBuffer txtbuf = bufferPool.acquire(policy.getBufferSize(),false); try { + BufferUtil.flipToFill(txtbuf); generator.generate(txtbuf,txt); - txtbuf.flip(); + BufferUtil.flipToFlush(txtbuf,0); // Write Text Frame BufferUtil.writeTo(txtbuf,out); @@ -219,10 +220,11 @@ public class WebSocketServletRFCTest ByteBuffer rbuf = bufferPool.acquire(policy.getBufferSize(),false); try { + BufferUtil.flipToFill(rbuf); read(in,rbuf); // Parse Frame - rbuf.flip(); + BufferUtil.flipToFlush(rbuf,0); parser.parse(rbuf); } finally