diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java index 06b2b154432..ec33f0e1266 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java @@ -19,7 +19,6 @@ package org.eclipse.jetty.websocket.javax.client.internal; import javax.websocket.ClientEndpoint; -import javax.websocket.Endpoint; import javax.websocket.EndpointConfig; import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketContainer; @@ -49,7 +48,7 @@ public class JavaxWebSocketClientFrameHandlerFactory extends JavaxWebSocketFrame public JavaxWebSocketFrameHandlerMetadata getMetadata(Class endpointClass, EndpointConfig endpointConfig) { if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass)) - return createEndpointMetadata((Class)endpointClass, endpointConfig); + return createEndpointMetadata(endpointConfig); if (endpointClass.getAnnotation(ClientEndpoint.class) == null) return null; diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java index 6087274b386..853dc88d639 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java @@ -250,20 +250,20 @@ public abstract class JavaxWebSocketFrameHandlerFactory } } - protected JavaxWebSocketFrameHandlerMetadata createEndpointMetadata(Class endpointClass, EndpointConfig endpointConfig) + protected JavaxWebSocketFrameHandlerMetadata createEndpointMetadata(EndpointConfig endpointConfig) { JavaxWebSocketFrameHandlerMetadata metadata = new JavaxWebSocketFrameHandlerMetadata(endpointConfig); - MethodHandles.Lookup lookup = getApplicationMethodHandleLookup(endpointClass); + MethodHandles.Lookup lookup = getServerMethodHandleLookup(); - Method openMethod = ReflectUtils.findMethod(endpointClass, "onOpen", Session.class, EndpointConfig.class); + Method openMethod = ReflectUtils.findMethod(Endpoint.class, "onOpen", Session.class, EndpointConfig.class); MethodHandle open = toMethodHandle(lookup, openMethod); metadata.setOpenHandler(open, openMethod); - Method closeMethod = ReflectUtils.findMethod(endpointClass, "onClose", Session.class, CloseReason.class); + Method closeMethod = ReflectUtils.findMethod(Endpoint.class, "onClose", Session.class, CloseReason.class); MethodHandle close = toMethodHandle(lookup, closeMethod); metadata.setCloseHandler(close, closeMethod); - Method errorMethod = ReflectUtils.findMethod(endpointClass, "onError", Session.class, Throwable.class); + Method errorMethod = ReflectUtils.findMethod(Endpoint.class, "onError", Session.class, Throwable.class); MethodHandle error = toMethodHandle(lookup, errorMethod); metadata.setErrorHandler(error, errorMethod); diff --git a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java index 12d00d0c67d..5ac2117464a 100644 --- a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java @@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.javax.common; import javax.websocket.ClientEndpoint; import javax.websocket.ClientEndpointConfig; -import javax.websocket.Endpoint; import javax.websocket.EndpointConfig; import org.eclipse.jetty.websocket.util.InvokerUtils; @@ -43,7 +42,7 @@ public class DummyFrameHandlerFactory extends JavaxWebSocketFrameHandlerFactory { if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass)) { - return createEndpointMetadata((Class)endpointClass, endpointConfig); + return createEndpointMetadata(endpointConfig); } if (endpointClass.getAnnotation(ClientEndpoint.class) == null) diff --git a/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxWebSocketServerFrameHandlerFactory.java b/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxWebSocketServerFrameHandlerFactory.java index 4f6c29a38f4..480d5acad5a 100644 --- a/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxWebSocketServerFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxWebSocketServerFrameHandlerFactory.java @@ -18,7 +18,6 @@ package org.eclipse.jetty.websocket.javax.server.internal; -import javax.websocket.Endpoint; import javax.websocket.EndpointConfig; import javax.websocket.server.ServerEndpoint; @@ -42,7 +41,7 @@ public class JavaxWebSocketServerFrameHandlerFactory extends JavaxWebSocketClien public JavaxWebSocketFrameHandlerMetadata getMetadata(Class endpointClass, EndpointConfig endpointConfig) { if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass)) - return createEndpointMetadata((Class)endpointClass, endpointConfig); + return createEndpointMetadata(endpointConfig); ServerEndpoint anno = endpointClass.getAnnotation(ServerEndpoint.class); if (anno == null) diff --git a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/EndpointEchoTest.java b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/EndpointEchoTest.java index c61cab9b0c1..e62d4c364e6 100644 --- a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/EndpointEchoTest.java +++ b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/EndpointEchoTest.java @@ -115,7 +115,6 @@ public class EndpointEchoTest @Test public void testEchoAnonymousInstance() throws Exception { - CountDownLatch openLatch = new CountDownLatch(1); CountDownLatch closeLatch = new CountDownLatch(1); BlockingQueue textMessages = new BlockingArrayQueue<>(); diff --git a/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java b/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java index 0d08b373c4f..da579a24477 100644 --- a/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java +++ b/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/JettyWebSocketFrameHandlerFactory.java @@ -195,28 +195,31 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle private JettyWebSocketFrameHandlerMetadata createListenerMetadata(Class endpointClass) { JettyWebSocketFrameHandlerMetadata metadata = new JettyWebSocketFrameHandlerMetadata(); - MethodHandles.Lookup lookup = JettyWebSocketFrameHandlerFactory.getApplicationMethodHandleLookup(endpointClass); + MethodHandles.Lookup lookup = JettyWebSocketFrameHandlerFactory.getServerMethodHandleLookup(); - Method openMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketConnect", Session.class); + if (!WebSocketConnectionListener.class.isAssignableFrom(endpointClass)) + throw new IllegalArgumentException("Class " + endpointClass + " does not implement " + WebSocketConnectionListener.class); + + Method openMethod = ReflectUtils.findMethod(WebSocketConnectionListener.class, "onWebSocketConnect", Session.class); MethodHandle open = toMethodHandle(lookup, openMethod); metadata.setOpenHandler(open, openMethod); - Method closeMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketClose", int.class, String.class); + Method closeMethod = ReflectUtils.findMethod(WebSocketConnectionListener.class, "onWebSocketClose", int.class, String.class); MethodHandle close = toMethodHandle(lookup, closeMethod); metadata.setCloseHandler(close, closeMethod); - Method errorMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketError", Throwable.class); + Method errorMethod = ReflectUtils.findMethod(WebSocketConnectionListener.class, "onWebSocketError", Throwable.class); MethodHandle error = toMethodHandle(lookup, errorMethod); metadata.setErrorHandler(error, errorMethod); // Simple Data Listener if (WebSocketListener.class.isAssignableFrom(endpointClass)) { - Method textMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketText", String.class); + Method textMethod = ReflectUtils.findMethod(WebSocketListener.class, "onWebSocketText", String.class); MethodHandle text = toMethodHandle(lookup, textMethod); metadata.setTextHandler(StringMessageSink.class, text, textMethod); - Method binaryMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketBinary", byte[].class, int.class, int.class); + Method binaryMethod = ReflectUtils.findMethod(WebSocketListener.class, "onWebSocketBinary", byte[].class, int.class, int.class); MethodHandle binary = toMethodHandle(lookup, binaryMethod); metadata.setBinaryHandle(ByteArrayMessageSink.class, binary, binaryMethod); } @@ -224,11 +227,11 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle // Ping/Pong Listener if (WebSocketPingPongListener.class.isAssignableFrom(endpointClass)) { - Method pongMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketPong", ByteBuffer.class); + Method pongMethod = ReflectUtils.findMethod(WebSocketPingPongListener.class, "onWebSocketPong", ByteBuffer.class); MethodHandle pong = toMethodHandle(lookup, pongMethod); metadata.setPongHandle(pong, pongMethod); - Method pingMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketPing", ByteBuffer.class); + Method pingMethod = ReflectUtils.findMethod(WebSocketPingPongListener.class, "onWebSocketPing", ByteBuffer.class); MethodHandle ping = toMethodHandle(lookup, pingMethod); metadata.setPingHandle(ping, pingMethod); } @@ -236,11 +239,11 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle // Partial Data / Message Listener if (WebSocketPartialListener.class.isAssignableFrom(endpointClass)) { - Method textMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketPartialText", String.class, boolean.class); + Method textMethod = ReflectUtils.findMethod(WebSocketPartialListener.class, "onWebSocketPartialText", String.class, boolean.class); MethodHandle text = toMethodHandle(lookup, textMethod); metadata.setTextHandler(PartialStringMessageSink.class, text, textMethod); - Method binaryMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketPartialBinary", ByteBuffer.class, boolean.class); + Method binaryMethod = ReflectUtils.findMethod(WebSocketPartialListener.class, "onWebSocketPartialBinary", ByteBuffer.class, boolean.class); MethodHandle binary = toMethodHandle(lookup, binaryMethod); metadata.setBinaryHandle(PartialByteBufferMessageSink.class, binary, binaryMethod); } @@ -248,7 +251,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle // Frame Listener if (WebSocketFrameListener.class.isAssignableFrom(endpointClass)) { - Method frameMethod = ReflectUtils.findMethod(endpointClass, "onWebSocketFrame", Frame.class); + Method frameMethod = ReflectUtils.findMethod(WebSocketFrameListener.class, "onWebSocketFrame", Frame.class); MethodHandle frame = toMethodHandle(lookup, frameMethod); metadata.setFrameHandler(frame, frameMethod); } diff --git a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/listeners/WebSocketListenerTest.java b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/listeners/WebSocketListenerTest.java index b65747cfe14..09050619ea2 100644 --- a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/listeners/WebSocketListenerTest.java +++ b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/listeners/WebSocketListenerTest.java @@ -140,7 +140,8 @@ public class WebSocketListenerTest CountDownLatch openLatch = new CountDownLatch(1); CountDownLatch closeLatch = new CountDownLatch(1); BlockingQueue textMessages = new BlockingArrayQueue<>(); - WebSocketListener clientEndpoint = new WebSocketListener() { + WebSocketListener clientEndpoint = new WebSocketListener() + { @Override public void onWebSocketConnect(Session session) {