Skip to content

Commit

Permalink
Issue #5043 - allow WebSocketListener / Endpoint anonymous classes to…
Browse files Browse the repository at this point in the history
… be used

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jul 16, 2020
1 parent 00777fe commit 0a30525
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +48,7 @@ public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass)
public JavaxWebSocketFrameHandlerMetadata getMetadata(Class<?> endpointClass, EndpointConfig endpointConfig)
{
if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass))
return createEndpointMetadata((Class<? extends Endpoint>)endpointClass, endpointConfig);
return createEndpointMetadata(endpointConfig);

if (endpointClass.getAnnotation(ClientEndpoint.class) == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ private MethodHandle toMethodHandle(MethodHandles.Lookup lookup, Method method)
}
}

protected JavaxWebSocketFrameHandlerMetadata createEndpointMetadata(Class<? extends Endpoint> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;

import org.eclipse.jetty.websocket.util.InvokerUtils;
Expand All @@ -43,7 +42,7 @@ public JavaxWebSocketFrameHandlerMetadata getMetadata(Class<?> endpointClass, En
{
if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass))
{
return createEndpointMetadata((Class<? extends Endpoint>)endpointClass, endpointConfig);
return createEndpointMetadata(endpointConfig);
}

if (endpointClass.getAnnotation(ClientEndpoint.class) == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -42,7 +41,7 @@ public JavaxWebSocketServerFrameHandlerFactory(JavaxWebSocketContainer container
public JavaxWebSocketFrameHandlerMetadata getMetadata(Class<?> endpointClass, EndpointConfig endpointConfig)
{
if (javax.websocket.Endpoint.class.isAssignableFrom(endpointClass))
return createEndpointMetadata((Class<? extends Endpoint>)endpointClass, endpointConfig);
return createEndpointMetadata(endpointConfig);

ServerEndpoint anno = endpointClass.getAnnotation(ServerEndpoint.class);
if (anno == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void testEchoClassRef() throws Exception
@Test
public void testEchoAnonymousInstance() throws Exception
{

CountDownLatch openLatch = new CountDownLatch(1);
CountDownLatch closeLatch = new CountDownLatch(1);
BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,60 +195,63 @@ private MethodHandle toMethodHandle(MethodHandles.Lookup lookup, Method method)
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);
}

// 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);
}

// 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);
}

// 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public void testAnonymousListener() throws Exception
CountDownLatch openLatch = new CountDownLatch(1);
CountDownLatch closeLatch = new CountDownLatch(1);
BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
WebSocketListener clientEndpoint = new WebSocketListener() {
WebSocketListener clientEndpoint = new WebSocketListener()
{
@Override
public void onWebSocketConnect(Session session)
{
Expand Down

0 comments on commit 0a30525

Please sign in to comment.