Merge pull request #4775 from eclipse/jetty-10.0.x-4771-WebSocketMessageHandlerTests

Issue #4771 - cleanup and add tests for the unused ws message handlers
This commit is contained in:
Lachlan 2020-04-20 08:43:39 +10:00 committed by GitHub
commit f23236b09d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 1231 additions and 366 deletions

View File

@ -41,10 +41,9 @@ import org.eclipse.jetty.websocket.javax.common.UpgradeRequest;
import org.eclipse.jetty.websocket.javax.common.UpgradeRequestAdapter;
import org.eclipse.jetty.websocket.javax.tests.MessageType;
import org.eclipse.jetty.websocket.javax.tests.SessionMatchers;
import org.eclipse.jetty.websocket.javax.tests.handlers.ByteArrayWholeHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.ByteBufferPartialHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.BinaryHandlers;
import org.eclipse.jetty.websocket.javax.tests.handlers.LongMessageHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.StringWholeHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.TextHandlers;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -93,7 +92,7 @@ public class SessionAddMessageHandlerTest
@Test
public void testMessageHandlerBinary()
{
session.addMessageHandler(new ByteBufferPartialHandler());
session.addMessageHandler(new BinaryHandlers.ByteBufferPartialHandler());
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT)));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.PONG)));
@ -102,7 +101,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteBufferPartialHandler.class)
instanceOf(BinaryHandlers.ByteBufferPartialHandler.class)
)
)
);
@ -111,8 +110,8 @@ public class SessionAddMessageHandlerTest
@Test
public void testMessageHandlerBoth()
{
session.addMessageHandler(new StringWholeHandler());
session.addMessageHandler(new ByteArrayWholeHandler());
session.addMessageHandler(new TextHandlers.StringWholeHandler());
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler());
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.PONG)));
@ -121,7 +120,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
@ -130,7 +129,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.TEXT),
instanceOf(StringWholeHandler.class)
instanceOf(TextHandlers.StringWholeHandler.class)
)
)
);
@ -139,9 +138,9 @@ public class SessionAddMessageHandlerTest
@Test
public void testMessageHandlerReplaceTextHandler()
{
MessageHandler strHandler = new StringWholeHandler();
MessageHandler strHandler = new TextHandlers.StringWholeHandler();
session.addMessageHandler(strHandler); // add a TEXT handler
session.addMessageHandler(new ByteArrayWholeHandler()); // add BINARY handler
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler()); // add BINARY handler
session.removeMessageHandler(strHandler); // remove original TEXT handler
session.addMessageHandler(new LongMessageHandler()); // add new TEXT handler
@ -154,7 +153,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
@ -177,7 +176,7 @@ public class SessionAddMessageHandlerTest
MessageHandler.Whole<String> lamdaHandler = (msg) -> received.add(msg);
session.addMessageHandler(String.class, lamdaHandler); // add a TEXT handler lambda
session.addMessageHandler(new ByteArrayWholeHandler()); // add BINARY handler
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler()); // add BINARY handler
session.removeMessageHandler(lamdaHandler); // remove original TEXT handler
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
@ -189,7 +188,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
@ -198,7 +197,7 @@ public class SessionAddMessageHandlerTest
@Test
public void testMessageHandlerText()
{
session.addMessageHandler(new StringWholeHandler());
session.addMessageHandler(new TextHandlers.StringWholeHandler());
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY)));
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT));
@ -209,7 +208,7 @@ public class SessionAddMessageHandlerTest
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.TEXT),
instanceOf(StringWholeHandler.class)
instanceOf(TextHandlers.StringWholeHandler.class)
)
)
);

View File

@ -0,0 +1,69 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.EndpointConfig;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/")
public class AbstractAnnotatedHandler
{
protected Session _session;
@OnOpen
public void onOpen(Session session, EndpointConfig config)
{
_session = session;
}
@OnError
public void onError(Session session, Throwable thr)
{
thr.printStackTrace();
}
public void sendText(String message, boolean last)
{
try
{
_session.getBasicRemote().sendText(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getBasicRemote().sendBinary(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,68 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
public class AbstractHandler extends Endpoint implements MessageHandler
{
protected Session _session;
@Override
public void onOpen(Session session, EndpointConfig config)
{
_session = session;
_session.addMessageHandler(this);
}
@Override
public void onError(Session session, Throwable thr)
{
thr.printStackTrace();
}
public void sendText(String message, boolean last)
{
try
{
_session.getBasicRemote().sendText(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getBasicRemote().sendBinary(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -20,11 +20,11 @@ package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class BaseMessageHandler implements MessageHandler.Whole<String>
public class BaseMessageHandler extends AbstractHandler implements MessageHandler.Whole<String>
{
@Override
public void onMessage(String message)
{
// TODO Auto-generated method stub
sendText(message, true);
}
}

View File

@ -0,0 +1,169 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.stream.Stream;
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.params.provider.Arguments;
public class BinaryHandlers
{
public static Stream<Arguments> getBinaryHandlers()
{
return Stream.of(
ByteArrayWholeHandler.class,
ByteArrayPartialHandler.class,
ByteBufferWholeHandler.class,
ByteBufferPartialHandler.class,
InputStreamWholeHandler.class,
AnnotatedByteBufferWholeHandler.class,
AnnotatedByteBufferPartialHandler.class,
AnnotatedByteArrayWholeHandler.class,
AnnotatedByteArrayPartialHandler.class,
AnnotatedInputStreamWholeHandler.class,
AnnotatedReverseArgumentPartialHandler.class
).map(Arguments::of);
}
public static class ByteArrayWholeHandler extends AbstractHandler implements MessageHandler.Whole<byte[]>
{
@Override
public void onMessage(byte[] message)
{
sendBinary(BufferUtil.toBuffer(message), true);
}
}
public static class ByteArrayPartialHandler extends AbstractHandler implements MessageHandler.Partial<byte[]>
{
@Override
public void onMessage(byte[] partialMessage, boolean last)
{
sendBinary(BufferUtil.toBuffer(partialMessage), last);
}
}
public static class ByteBufferWholeHandler extends AbstractHandler implements MessageHandler.Whole<ByteBuffer>
{
@Override
public void onMessage(ByteBuffer message)
{
sendBinary(message, true);
}
}
public static class ByteBufferPartialHandler extends AbstractHandler implements MessageHandler.Partial<ByteBuffer>
{
@Override
public void onMessage(ByteBuffer partialMessage, boolean last)
{
sendBinary(partialMessage, last);
}
}
public static class InputStreamWholeHandler extends AbstractHandler implements MessageHandler.Whole<InputStream>
{
@Override
public void onMessage(InputStream stream)
{
sendBinary(readBytes(stream), true);
}
}
@ServerEndpoint("/")
public static class AnnotatedByteBufferWholeHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(ByteBuffer message)
{
sendBinary(message, true);
}
}
@ServerEndpoint("/")
public static class AnnotatedByteBufferPartialHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(ByteBuffer message, boolean last)
{
sendBinary(message, last);
}
}
@ServerEndpoint("/")
public static class AnnotatedByteArrayWholeHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(byte[] message)
{
sendBinary(BufferUtil.toBuffer(message), true);
}
}
@ServerEndpoint("/")
public static class AnnotatedByteArrayPartialHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(byte[] message, boolean last)
{
sendBinary(BufferUtil.toBuffer(message), last);
}
}
@ServerEndpoint("/")
public static class AnnotatedInputStreamWholeHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(InputStream stream)
{
sendBinary(readBytes(stream), true);
}
}
@ServerEndpoint("/")
public static class AnnotatedReverseArgumentPartialHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(boolean last, Session session, byte[] message)
{
sendBinary(BufferUtil.toBuffer(message), last);
}
}
private static ByteBuffer readBytes(InputStream stream)
{
try
{
return BufferUtil.toBuffer(IO.readBytes(stream));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -1,30 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class ByteArrayPartialHandler implements MessageHandler.Partial<byte[]>
{
@Override
public void onMessage(byte[] partialMessage, boolean last)
{
// TODO Auto-generated method stub
}
}

View File

@ -1,30 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class ByteArrayWholeHandler implements MessageHandler.Whole<byte[]>
{
@Override
public void onMessage(byte[] message)
{
// TODO Auto-generated method stub
}
}

View File

@ -1,30 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.nio.ByteBuffer;
import javax.websocket.MessageHandler;
public class ByteBufferPartialHandler implements MessageHandler.Partial<ByteBuffer>
{
@Override
public void onMessage(ByteBuffer partialMessage, boolean last)
{
}
}

View File

@ -1,31 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.nio.ByteBuffer;
import javax.websocket.MessageHandler;
public class ByteBufferWholeHandler implements MessageHandler.Whole<ByteBuffer>
{
@Override
public void onMessage(ByteBuffer message)
{
// TODO Auto-generated method stub
}
}

View File

@ -24,17 +24,17 @@ import javax.websocket.MessageHandler;
/**
* A particularly annoying type of MessageHandler. One defining 2 implementations.
*/
public class ComboMessageHandler implements MessageHandler.Whole<String>, MessageHandler.Partial<ByteBuffer>
public class ComboMessageHandler extends AbstractHandler implements MessageHandler.Whole<String>, MessageHandler.Partial<ByteBuffer>
{
@Override
public void onMessage(ByteBuffer partialMessage, boolean last)
{
// TODO Auto-generated method stub
sendBinary(partialMessage, last);
}
@Override
public void onMessage(String message)
{
// TODO Auto-generated method stub
sendText(message, true);
}
}

View File

@ -26,6 +26,6 @@ public class ExtendedMessageHandler extends BaseMessageHandler implements Messag
@Override
public void onMessage(ByteBuffer partialMessage, boolean last)
{
// TODO Auto-generated method stub
sendBinary(partialMessage, last);
}
}

View File

@ -1,31 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.InputStream;
import javax.websocket.MessageHandler;
public class InputStreamWholeHandler implements MessageHandler.Whole<InputStream>
{
@Override
public void onMessage(InputStream stream)
{
// TODO Auto-generated method stub
}
}

View File

@ -20,10 +20,11 @@ package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class LongMessageHandler implements MessageHandler.Whole<Long>
public class LongMessageHandler extends AbstractHandler implements MessageHandler.Whole<Long>
{
@Override
public void onMessage(Long message)
{
sendText(message.toString(), true);
}
}

View File

@ -0,0 +1,167 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.javax.tests.EventSocket;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static javax.websocket.CloseReason.CloseCodes.NORMAL_CLOSURE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class MessageHandlerTest
{
private Server server;
private URI serverUri;
private WebSocketContainer client;
private static Stream<Arguments> getBinaryHandlers()
{
return Stream.concat(BinaryHandlers.getBinaryHandlers(),
Stream.of(ComboMessageHandler.class, ExtendedMessageHandler.class).map(Arguments::of));
}
private static Stream<Arguments> getTextHandlers()
{
return Stream.concat(TextHandlers.getTextHandlers(),
Stream.of(ComboMessageHandler.class, ExtendedMessageHandler.class).map(Arguments::of));
}
@BeforeEach
public void before() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
JavaxWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
{
Stream<Arguments> argumentsStream = Stream.concat(getBinaryHandlers(), getTextHandlers());
for (Class<?> c : getClassListFromArguments(argumentsStream))
{
container.addEndpoint(ServerEndpointConfig.Builder.create(c, "/" + c.getSimpleName()).build());
}
container.addEndpoint(ServerEndpointConfig.Builder.create(LongMessageHandler.class,
"/" + LongMessageHandler.class.getSimpleName()).build());
});
server.setHandler(contextHandler);
server.start();
serverUri = URI.create("ws://localhost:" + connector.getLocalPort() + "/");
client = ContainerProvider.getWebSocketContainer();
}
@AfterEach
public void after() throws Exception
{
LifeCycle.stop(client);
server.stop();
}
@ParameterizedTest
@MethodSource("getBinaryHandlers")
public void testBinaryHandlers(Class<?> clazz) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
Session session = client.connectToServer(clientEndpoint, serverUri.resolve(clazz.getSimpleName()));
// Send and receive echo on client.
ByteBuffer payload = BufferUtil.toBuffer("hello world");
session.getBasicRemote().sendBinary(payload);
ByteBuffer echoMessage = clientEndpoint.binaryMessages.poll(5, TimeUnit.SECONDS);
assertThat(echoMessage, is(payload));
// Close normally.
session.close(new CloseReason(NORMAL_CLOSURE, "standard close"));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.closeReason.getCloseCode(), is(NORMAL_CLOSURE));
assertThat(clientEndpoint.closeReason.getReasonPhrase(), is("standard close"));
}
@ParameterizedTest
@MethodSource("getTextHandlers")
public void testTextHandlers(Class<?> clazz) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
Session session = client.connectToServer(clientEndpoint, serverUri.resolve(clazz.getSimpleName()));
// Send and receive echo on client.
String payload = "hello world";
session.getBasicRemote().sendText(payload);
String echoMessage = clientEndpoint.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(echoMessage, is(payload));
// Close normally.
session.close(new CloseReason(NORMAL_CLOSURE, "standard close"));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.closeReason.getCloseCode(), is(NORMAL_CLOSURE));
assertThat(clientEndpoint.closeReason.getReasonPhrase(), is("standard close"));
}
@Test
public void testLongDecoderHandler() throws Exception
{
EventSocket clientEndpoint = new EventSocket();
Session session = client.connectToServer(clientEndpoint, serverUri.resolve(LongMessageHandler.class.getSimpleName()));
// Send and receive echo on client.
String payload = Long.toString(Long.MAX_VALUE);
session.getBasicRemote().sendText(payload);
String echoMessage = clientEndpoint.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(echoMessage, is(payload));
// Close normally.
session.close(new CloseReason(NORMAL_CLOSURE, "standard close"));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.closeReason.getCloseCode(), is(NORMAL_CLOSURE));
assertThat(clientEndpoint.closeReason.getReasonPhrase(), is("standard close"));
}
private List<Class<?>> getClassListFromArguments(Stream<Arguments> stream)
{
return stream.map(arguments -> (Class<?>)arguments.get()[0]).collect(Collectors.toList());
}
}

View File

@ -1,31 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.Reader;
import javax.websocket.MessageHandler;
public class ReaderWholeHandler implements MessageHandler.Whole<Reader>
{
@Override
public void onMessage(Reader reader)
{
// TODO Auto-generated method stub
}
}

View File

@ -1,30 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class StringPartialHandler implements MessageHandler.Partial<String>
{
@Override
public void onMessage(String partialMessage, boolean last)
{
// TODO Auto-generated method stub
}
}

View File

@ -1,30 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import javax.websocket.MessageHandler;
public class StringWholeHandler implements MessageHandler.Whole<String>
{
@Override
public void onMessage(String message)
{
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,125 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.tests.handlers;
import java.io.IOException;
import java.io.Reader;
import java.util.stream.Stream;
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.params.provider.Arguments;
public class TextHandlers
{
public static Stream<Arguments> getTextHandlers()
{
return Stream.of(
StringWholeHandler.class,
StringPartialHandler.class,
ReaderWholeHandler.class,
AnnotatedStringWholeHandler.class,
AnnotatedStringPartialHandler.class,
AnnotatedReaderHandler.class,
AnnotatedReverseArgumentsPartialHandler.class
).map(Arguments::of);
}
public static class StringWholeHandler extends AbstractHandler implements MessageHandler.Whole<String>
{
@Override
public void onMessage(String message)
{
sendText(message, true);
}
}
public static class StringPartialHandler extends AbstractHandler implements MessageHandler.Partial<String>
{
@Override
public void onMessage(String partialMessage, boolean last)
{
sendText(partialMessage, last);
}
}
public static class ReaderWholeHandler extends AbstractHandler implements MessageHandler.Whole<Reader>
{
@Override
public void onMessage(Reader reader)
{
sendText(readString(reader), true);
}
}
@ServerEndpoint("/")
public static class AnnotatedStringWholeHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(String message)
{
sendText(message, true);
}
}
@ServerEndpoint("/")
public static class AnnotatedStringPartialHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(String message, boolean last)
{
sendText(message, last);
}
}
@ServerEndpoint("/")
public static class AnnotatedReaderHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(Reader reader)
{
sendText(readString(reader), true);
}
}
@ServerEndpoint("/")
public static class AnnotatedReverseArgumentsPartialHandler extends AbstractAnnotatedHandler
{
@OnMessage
public void onMessage(boolean last, String message, Session session)
{
sendText(message, last);
}
}
private static String readString(Reader reader)
{
try
{
return IO.toString(reader);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -31,7 +31,9 @@ public interface WebSocketConnectionListener
* @param statusCode the close status code. (See {@link StatusCode})
* @param reason the optional reason for the close.
*/
void onWebSocketClose(int statusCode, String reason);
default void onWebSocketClose(int statusCode, String reason)
{
}
/**
* A WebSocket {@link Session} has connected successfully and is ready to be used.
@ -40,7 +42,9 @@ public interface WebSocketConnectionListener
*
* @param session the websocket session.
*/
void onWebSocketConnect(Session session);
default void onWebSocketConnect(Session session)
{
}
/**
* A WebSocket exception has occurred.
@ -53,5 +57,7 @@ public interface WebSocketConnectionListener
*
* @param cause the error that occurred.
*/
void onWebSocketError(Throwable cause);
default void onWebSocketError(Throwable cause)
{
}
}

View File

@ -30,12 +30,16 @@ public interface WebSocketListener extends WebSocketConnectionListener
* @param offset the offset in the payload array where the data starts
* @param len the length of bytes in the payload
*/
void onWebSocketBinary(byte[] payload, int offset, int len);
default void onWebSocketBinary(byte[] payload, int offset, int len)
{
}
/**
* A WebSocket Text frame was received.
*
* @param message the message
*/
void onWebSocketText(String message);
default void onWebSocketText(String message)
{
}
}

View File

@ -35,7 +35,9 @@ public interface WebSocketPartialListener extends WebSocketConnectionListener
* @param payload the binary message frame payload
* @param fin true if this is the final frame, false otherwise
*/
void onWebSocketPartialBinary(ByteBuffer payload, boolean fin);
default void onWebSocketPartialBinary(ByteBuffer payload, boolean fin)
{
}
/**
* A WebSocket TEXT (or associated CONTINUATION) frame has been received.
@ -50,5 +52,7 @@ public interface WebSocketPartialListener extends WebSocketConnectionListener
* will be held over until the next frame is received.
* @param fin true if this is the final frame, false otherwise
*/
void onWebSocketPartialText(String payload, boolean fin);
default void onWebSocketPartialText(String payload, boolean fin)
{
}
}

View File

@ -30,12 +30,16 @@ public interface WebSocketPingPongListener extends WebSocketConnectionListener
*
* @param payload the ping payload
*/
void onWebSocketPing(ByteBuffer payload);
default void onWebSocketPing(ByteBuffer payload)
{
}
/**
* A WebSocket PONG has been received.
*
* @param payload the pong payload
*/
void onWebSocketPong(ByteBuffer payload);
default void onWebSocketPong(ByteBuffer payload)
{
}
}

View File

@ -35,9 +35,9 @@ import org.eclipse.jetty.websocket.api.Session;
* <p>
* <u>Text Message Versions</u>
* <ol>
* <li>{@code public void methodName(String text)}</li>
* <li><code>public void methodName(String text)</code></li>
* <li><code>public void methodName({@link Session} session, String text)</code></li>
* <li>{@code public void methodName(Reader reader)}</li>
* <li><code>public void methodName(Reader reader)</code></li>
* <li><code>public void methodName({@link Session} session, Reader reader)</code></li>
* </ol>
* Note: that the {@link Reader} in this case will always use UTF-8 encoding/charset (this is dictated by the RFC 6455 spec for Text Messages. If you need to
@ -45,11 +45,11 @@ import org.eclipse.jetty.websocket.api.Session;
* <p>
* <u>Binary Message Versions</u>
* <ol>
* <li>{@code public void methodName(ByteBuffer message)}</li>
* <li><code>public void methodName(ByteBuffer message)</code></li>
* <li><code>public void methodName({@link Session} session, ByteBuffer message)</code></li>
* <li>{@code public void methodName(byte buf[], int offset, int length)}</li>
* <li><code>public void methodName(byte buf[], int offset, int length)</code></li>
* <li><code>public void methodName({@link Session} session, byte buf[], int offset, int length)</code></li>
* <li>{@code public void methodName(InputStream stream)}</li>
* <li><code>public void methodName(InputStream stream)</code></li>
* <li><code>public void methodName({@link Session} session, InputStream stream)</code></li>
* </ol>
*/

View File

@ -296,15 +296,9 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
final InvokerUtils.Arg STATUS_CODE = new InvokerUtils.Arg(int.class);
final InvokerUtils.Arg REASON = new InvokerUtils.Arg(String.class);
MethodHandle methodHandle = InvokerUtils.mutatedInvoker(lookup, endpointClass, onmethod, SESSION, STATUS_CODE, REASON);
// TODO: need mutation of args? ...
// Session + CloseInfo ->
// setOnClose((closeInfo) ->{
// args[0] = getSession();
// args[1] = closeInfo.getStatusCode();
// args[2] = closeInfo.getReason();
// invoker.apply(endpoint, args);
metadata.setCloseHandler(methodHandle, onmethod);
}
// OnWebSocketError [0..1]
onmethod = ReflectUtils.findAnnotatedMethod(endpointClass, OnWebSocketError.class);
if (onmethod != null)
@ -360,7 +354,6 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
new InvokerUtils.Arg(Reader.class).required()
};
onmessageloop:
for (Method onMsg : onMessages)
{
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
@ -371,7 +364,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
// Normal Text Message
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
metadata.setTextHandler(StringMessageSink.class, methodHandle, onMsg);
continue onmessageloop;
continue;
}
methodHandle = InvokerUtils.optionalMutatedInvoker(lookup, endpointClass, onMsg, binaryBufferCallingArgs);
@ -380,7 +373,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
// ByteBuffer Binary Message
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
metadata.setBinaryHandle(ByteBufferMessageSink.class, methodHandle, onMsg);
continue onmessageloop;
continue;
}
methodHandle = InvokerUtils.optionalMutatedInvoker(lookup, endpointClass, onMsg, binaryArrayCallingArgs);
@ -389,7 +382,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
// byte[] Binary Message
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
metadata.setBinaryHandle(ByteArrayMessageSink.class, methodHandle, onMsg);
continue onmessageloop;
continue;
}
methodHandle = InvokerUtils.optionalMutatedInvoker(lookup, endpointClass, onMsg, inputStreamCallingArgs);
@ -398,7 +391,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
// InputStream Binary Message
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
metadata.setBinaryHandle(InputStreamMessageSink.class, methodHandle, onMsg);
continue onmessageloop;
continue;
}
methodHandle = InvokerUtils.optionalMutatedInvoker(lookup, endpointClass, onMsg, readerCallingArgs);
@ -407,7 +400,7 @@ public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
// Reader Text Message
assertSignatureValid(endpointClass, onMsg, OnWebSocketMessage.class);
metadata.setTextHandler(ReaderMessageSink.class, methodHandle, onMsg);
continue onmessageloop;
continue;
}
else
{

View File

@ -122,15 +122,15 @@ public class ConcurrentConnectTest
for (EventSocket l : listeners)
{
l.session.getRemote().sendString("ping");
assertThat(l.messageQueue.poll(5, TimeUnit.SECONDS), is("ping"));
assertThat(l.textMessages.poll(5, TimeUnit.SECONDS), is("ping"));
l.session.close(StatusCode.NORMAL, "close from client");
}
for (EventSocket l : listeners)
{
assertTrue(l.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(l.statusCode, is(StatusCode.NORMAL));
assertThat(l.reason, is("close from client"));
assertThat(l.closeCode, is(StatusCode.NORMAL));
assertThat(l.closeReason, is("close from client"));
assertNull(l.error);
}

View File

@ -42,10 +42,10 @@ public class EventSocket
public Session session;
private String behavior;
public BlockingQueue<String> messageQueue = new BlockingArrayQueue<>();
public BlockingQueue<ByteBuffer> binaryMessageQueue = new BlockingArrayQueue<>();
public volatile int statusCode = StatusCode.UNDEFINED;
public volatile String reason;
public BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
public BlockingQueue<ByteBuffer> binaryMessages = new BlockingArrayQueue<>();
public volatile int closeCode = StatusCode.UNDEFINED;
public volatile String closeReason;
public volatile Throwable error = null;
public CountDownLatch openLatch = new CountDownLatch(1);
@ -67,7 +67,7 @@ public class EventSocket
{
if (LOG.isDebugEnabled())
LOG.debug("{} onMessage(): {}", toString(), message);
messageQueue.offer(message);
textMessages.offer(message);
}
@OnWebSocketMessage
@ -76,7 +76,7 @@ public class EventSocket
ByteBuffer message = ByteBuffer.wrap(buf, offset, len);
if (LOG.isDebugEnabled())
LOG.debug("{} onMessage(): {}", toString(), message);
binaryMessageQueue.offer(message);
binaryMessages.offer(message);
}
@OnWebSocketClose
@ -84,8 +84,8 @@ public class EventSocket
{
if (LOG.isDebugEnabled())
LOG.debug("{} onClose(): {}:{}", toString(), statusCode, reason);
this.statusCode = statusCode;
this.reason = reason;
this.closeCode = statusCode;
this.closeReason = reason;
closeLatch.countDown();
}

View File

@ -130,8 +130,8 @@ public class JettyOnCloseTest
clientEndpoint.session.close();
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.SERVICE_RESTART));
assertThat(clientEndpoint.reason, is("custom close reason"));
assertThat(clientEndpoint.closeCode, is(StatusCode.SERVICE_RESTART));
assertThat(clientEndpoint.closeReason, is("custom close reason"));
}
@Test
@ -146,8 +146,8 @@ public class JettyOnCloseTest
serverEndpoint.session.close(StatusCode.NORMAL, "first close");
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.NORMAL));
assertThat(clientEndpoint.reason, is("first close"));
assertThat(clientEndpoint.closeCode, is(StatusCode.NORMAL));
assertThat(clientEndpoint.closeReason, is("first close"));
}
@Test
@ -166,8 +166,8 @@ public class JettyOnCloseTest
serverEndpoint.session.close(StatusCode.PROTOCOL, "abnormal close 1");
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.PROTOCOL));
assertThat(clientEndpoint.reason, is("abnormal close 1"));
assertThat(clientEndpoint.closeCode, is(StatusCode.PROTOCOL));
assertThat(clientEndpoint.closeReason, is("abnormal close 1"));
}
@Test
@ -185,8 +185,8 @@ public class JettyOnCloseTest
clientEndpoint.session.close();
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.SERVER_ERROR));
assertThat(clientEndpoint.reason, containsString("trigger onError from onClose"));
assertThat(clientEndpoint.closeCode, is(StatusCode.SERVER_ERROR));
assertThat(clientEndpoint.closeReason, containsString("trigger onError from onClose"));
assertTrue(serverEndpoint.errorLatch.await(5, TimeUnit.SECONDS));
assertThat(serverEndpoint.error, instanceOf(RuntimeException.class));

View File

@ -130,7 +130,7 @@ public class JettyWebSocketExtensionConfigTest
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(correctResponseExtensions.await(5, TimeUnit.SECONDS));
String msg = socket.messageQueue.poll();
String msg = socket.textMessages.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -80,7 +80,7 @@ public class JettyWebSocketFilterTest
}
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
String msg = socket.messageQueue.poll();
String msg = socket.textMessages.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -92,7 +92,7 @@ public class JettyWebSocketServletTest
}
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
String msg = socket.messageQueue.poll();
String msg = socket.textMessages.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -110,16 +110,16 @@ public class SuspendResumeTest
clientSocket.session.getRemote().sendString("suspend");
clientSocket.session.getRemote().sendString("hello world");
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("suspend"));
assertNull(serverSocket.messageQueue.poll(1, TimeUnit.SECONDS));
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
serverSocket.suspendToken.resume();
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("suspend"));
assertNull(serverSocket.messageQueue.poll(1, TimeUnit.SECONDS));
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend"));
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
serverSocket.suspendToken.resume();
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("hello world"));
assertNull(serverSocket.messageQueue.poll(1, TimeUnit.SECONDS));
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("hello world"));
assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS));
// make sure both sides are closed
clientSocket.session.close();
@ -142,22 +142,22 @@ public class SuspendResumeTest
// verify connection by sending a message from server to client
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
serverSocket.session.getRemote().sendString("verification");
assertThat(clientSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("verification"));
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("verification"));
// suspend the client so that no read events occur
SuspendToken suspendToken = clientSocket.session.suspend();
// verify client can still send messages
clientSocket.session.getRemote().sendString("message-from-client");
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("message-from-client"));
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-client"));
// the message is not received as it is suspended
serverSocket.session.getRemote().sendString("message-from-server");
assertNull(clientSocket.messageQueue.poll(2, TimeUnit.SECONDS));
assertNull(clientSocket.textMessages.poll(2, TimeUnit.SECONDS));
// client should receive message after it resumes
suspendToken.resume();
assertThat(clientSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("message-from-server"));
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-server"));
// make sure both sides are closed
clientSocket.session.close();
@ -180,7 +180,7 @@ public class SuspendResumeTest
// verify connection by sending a message from server to client
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
serverSocket.session.getRemote().sendString("verification");
assertThat(clientSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("verification"));
assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("verification"));
// make sure both sides are closed
clientSocket.session.close();

View File

@ -167,13 +167,13 @@ public class WebSocketOverHTTP2Test
String text = "websocket";
session.getRemote().sendString(text);
String message = wsEndPoint.messageQueue.poll(5, TimeUnit.SECONDS);
String message = wsEndPoint.textMessages.poll(5, TimeUnit.SECONDS);
assertNotNull(message);
assertEquals(text, message);
session.close(StatusCode.NORMAL, null);
assertTrue(wsEndPoint.closeLatch.await(5, TimeUnit.SECONDS));
assertEquals(StatusCode.NORMAL, wsEndPoint.statusCode);
assertEquals(StatusCode.NORMAL, wsEndPoint.closeCode);
assertNull(wsEndPoint.error);
}
@ -230,7 +230,7 @@ public class WebSocketOverHTTP2Test
String text = "websocket";
session.getRemote().sendString(text);
String message = wsEndPoint.messageQueue.poll(5, TimeUnit.SECONDS);
String message = wsEndPoint.textMessages.poll(5, TimeUnit.SECONDS);
assertNotNull(message);
assertEquals(text, message);

View File

@ -123,7 +123,7 @@ public class WebSocketServletExamplesTest
String message = "hello world";
session.getRemote().sendString(message);
String response = socket.messageQueue.poll(5, TimeUnit.SECONDS);
String response = socket.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(response, is(message));
}
@ -147,7 +147,7 @@ public class WebSocketServletExamplesTest
String message = "hello world";
session.getRemote().sendString(message);
String response = socket.messageQueue.poll(5, TimeUnit.SECONDS);
String response = socket.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(response, is(message));
}
@ -173,7 +173,7 @@ public class WebSocketServletExamplesTest
String message = "hello world";
session.getRemote().sendString(message);
String response = socket.messageQueue.poll(5, TimeUnit.SECONDS);
String response = socket.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(response, is(message));
}

View File

@ -102,8 +102,8 @@ public class WebSocketStopTest
client.stop();
assertTrue(clientSocket1.closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(clientSocket2.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientSocket1.statusCode, is(StatusCode.SHUTDOWN));
assertThat(clientSocket2.statusCode, is(StatusCode.SHUTDOWN));
assertThat(clientSocket1.closeCode, is(StatusCode.SHUTDOWN));
assertThat(clientSocket2.closeCode, is(StatusCode.SHUTDOWN));
}
@Test
@ -116,7 +116,7 @@ public class WebSocketStopTest
upgradeRequest.addExtensions("permessage-deflate");
Session session = client.connect(clientSocket, uri, upgradeRequest).get(5, TimeUnit.SECONDS);
clientSocket.session.getRemote().sendString("init deflater");
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("init deflater"));
assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("init deflater"));
session.close(StatusCode.NORMAL, null);
// make sure both sides are closed
@ -125,8 +125,8 @@ public class WebSocketStopTest
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
// check we closed normally
assertThat(clientSocket.statusCode, is(StatusCode.NORMAL));
assertThat(serverSocket.statusCode, is(StatusCode.NORMAL));
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
assertThat(serverSocket.closeCode, is(StatusCode.NORMAL));
IOException error = assertThrows(IOException.class,
() -> session.getRemote().sendString("this should fail before ExtensionStack"));

View File

@ -161,7 +161,7 @@ public class JettyAutobahnClient
if (waitForUpgrade(wsUri, response))
{
String msg = onCaseCount.messageQueue.poll(10, TimeUnit.SECONDS);
String msg = onCaseCount.textMessages.poll(10, TimeUnit.SECONDS);
onCaseCount.session.close(StatusCode.SHUTDOWN, null);
assertTrue(onCaseCount.closeLatch.await(2, TimeUnit.SECONDS));
assertNotNull(msg);

View File

@ -159,7 +159,7 @@ public class ClientConfigTest
assertNull(clientEndpoint.error);
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(serverSocket.statusCode, is(StatusCode.NO_CODE));
assertThat(serverSocket.closeCode, is(StatusCode.NO_CODE));
}
@ParameterizedTest
@ -177,7 +177,7 @@ public class ClientConfigTest
assertThat(clientEndpoint.error, instanceOf(MessageTooLargeException.class));
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(serverSocket.statusCode, is(StatusCode.MESSAGE_TOO_LARGE));
assertThat(serverSocket.closeCode, is(StatusCode.MESSAGE_TOO_LARGE));
}
@ParameterizedTest
@ -196,7 +196,7 @@ public class ClientConfigTest
assertThat(clientEndpoint.error, instanceOf(WebSocketTimeoutException.class));
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(serverSocket.statusCode, is(StatusCode.SHUTDOWN));
assertThat(serverSocket.closeCode, is(StatusCode.SHUTDOWN));
}
@ParameterizedTest
@ -214,6 +214,6 @@ public class ClientConfigTest
assertThat(clientEndpoint.error, instanceOf(MessageTooLargeException.class));
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(serverSocket.statusCode, is(StatusCode.MESSAGE_TOO_LARGE));
assertThat(serverSocket.closeCode, is(StatusCode.MESSAGE_TOO_LARGE));
}
}

View File

@ -0,0 +1,69 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests.listeners;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
public class AbstractAnnotatedListener
{
protected Session _session;
@OnWebSocketConnect
public void onWebSocketConnect(Session session)
{
_session = session;
}
@OnWebSocketError
public void onWebSocketError(Throwable thr)
{
thr.printStackTrace();
}
public void sendText(String message, boolean last)
{
try
{
_session.getRemote().sendPartialString(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getRemote().sendPartialBytes(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,66 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests.listeners;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketConnectionListener;
public class AbstractListener implements WebSocketConnectionListener
{
protected Session _session;
@Override
public void onWebSocketConnect(Session session)
{
_session = session;
}
@Override
public void onWebSocketError(Throwable thr)
{
thr.printStackTrace();
}
public void sendText(String message, boolean last)
{
try
{
_session.getRemote().sendPartialString(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getRemote().sendPartialBytes(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,129 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests.listeners;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.stream.Stream;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.WebSocketPartialListener;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.junit.jupiter.params.provider.Arguments;
public class BinaryListeners
{
public static Stream<Arguments> getBinaryListeners()
{
return Stream.of(
OffsetByteArrayWholeListener.class,
OffsetByteBufferPartialListener.class,
AnnotatedByteBufferWholeListener.class,
AnnotatedByteArrayWholeListener.class,
AnnotatedOffsetByteArrayWholeListener.class,
AnnotatedInputStreamWholeListener.class,
AnnotatedReverseArgumentPartialListener.class
).map(Arguments::of);
}
public static class OffsetByteArrayWholeListener extends AbstractListener implements WebSocketListener
{
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len)
{
sendBinary(BufferUtil.toBuffer(payload, offset, len), true);
}
}
public static class OffsetByteBufferPartialListener extends AbstractListener implements WebSocketPartialListener
{
@Override
public void onWebSocketPartialBinary(ByteBuffer payload, boolean fin)
{
sendBinary(payload, fin);
}
}
@WebSocket
public static class AnnotatedByteBufferWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(ByteBuffer message)
{
sendBinary(message, true);
}
}
@WebSocket
public static class AnnotatedByteArrayWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(byte[] message)
{
sendBinary(BufferUtil.toBuffer(message), true);
}
}
@WebSocket
public static class AnnotatedOffsetByteArrayWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(byte[] message, int offset, int length)
{
sendBinary(BufferUtil.toBuffer(message, offset, length), true);
}
}
@WebSocket
public static class AnnotatedInputStreamWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(InputStream stream)
{
sendBinary(readBytes(stream), true);
}
}
@WebSocket
public static class AnnotatedReverseArgumentPartialListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(Session session, ByteBuffer message)
{
sendBinary(message, true);
}
}
public static ByteBuffer readBytes(InputStream stream)
{
try
{
return BufferUtil.toBuffer(IO.readBytes(stream));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,105 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests.listeners;
import java.io.IOException;
import java.io.Reader;
import java.util.stream.Stream;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.api.WebSocketPartialListener;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.junit.jupiter.params.provider.Arguments;
public class TextListeners
{
public static Stream<Arguments> getTextListeners()
{
return Stream.of(
StringWholeListener.class,
StringPartialListener.class,
AnnotatedStringWholeListener.class,
AnnotatedReaderWholeListener.class,
AnnotatedReverseArgumentPartialListener.class
).map(Arguments::of);
}
public static class StringWholeListener extends AbstractListener implements WebSocketListener
{
@Override
public void onWebSocketText(String message)
{
sendText(message, true);
}
}
public static class StringPartialListener extends AbstractListener implements WebSocketPartialListener
{
@Override
public void onWebSocketPartialText(String message, boolean fin)
{
sendText(message, fin);
}
}
@WebSocket
public static class AnnotatedStringWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(String message)
{
sendText(message, true);
}
}
@WebSocket
public static class AnnotatedReaderWholeListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(Reader reader)
{
sendText(readString(reader), true);
}
}
@WebSocket
public static class AnnotatedReverseArgumentPartialListener extends AbstractAnnotatedListener
{
@OnWebSocketMessage
public void onMessage(Session session, String message)
{
sendText(message, true);
}
}
public static String readString(Reader reader)
{
try
{
return IO.toString(reader);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,146 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests.listeners;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.tests.EventSocket;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class WebSocketListenerTest
{
private Server server;
private URI serverUri;
private WebSocketClient client;
@BeforeEach
public void before() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
JettyWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
{
for (Class<?> c : getClassListFromArguments(TextListeners.getTextListeners()))
{
container.addMapping("/text/" + c.getSimpleName(), (req, res) -> construct(c));
}
for (Class<?> c : getClassListFromArguments(BinaryListeners.getBinaryListeners()))
{
container.addMapping("/binary/" + c.getSimpleName(), (req, res) -> construct(c));
}
});
server.setHandler(contextHandler);
server.start();
serverUri = URI.create("ws://localhost:" + connector.getLocalPort() + "/");
client = new WebSocketClient();
client.start();
}
@AfterEach
public void after() throws Exception
{
client.stop();
server.stop();
}
@ParameterizedTest
@MethodSource("org.eclipse.jetty.websocket.tests.listeners.TextListeners#getTextListeners")
public void testTextListeners(Class<?> clazz) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
client.connect(clientEndpoint, serverUri.resolve("/text/" + clazz.getSimpleName())).get(5, TimeUnit.SECONDS);
// Send and receive echo on client.
String payload = "hello world";
clientEndpoint.session.getRemote().sendString(payload);
String echoMessage = clientEndpoint.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(echoMessage, is(payload));
// Close normally.
clientEndpoint.session.close(StatusCode.NORMAL, "standard close");
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.closeCode, is(StatusCode.NORMAL));
assertThat(clientEndpoint.closeReason, is("standard close"));
}
@ParameterizedTest
@MethodSource("org.eclipse.jetty.websocket.tests.listeners.BinaryListeners#getBinaryListeners")
public void testBinaryListeners(Class<?> clazz) throws Exception
{
EventSocket clientEndpoint = new EventSocket();
client.connect(clientEndpoint, serverUri.resolve("/binary/" + clazz.getSimpleName())).get(5, TimeUnit.SECONDS);
// Send and receive echo on client.
ByteBuffer payload = BufferUtil.toBuffer("hello world");
clientEndpoint.session.getRemote().sendBytes(payload);
ByteBuffer echoMessage = clientEndpoint.binaryMessages.poll(5, TimeUnit.SECONDS);
assertThat(echoMessage, is(payload));
// Close normally.
clientEndpoint.session.close(StatusCode.NORMAL, "standard close");
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.closeCode, is(StatusCode.NORMAL));
assertThat(clientEndpoint.closeReason, is("standard close"));
}
private List<Class<?>> getClassListFromArguments(Stream<Arguments> stream)
{
return stream.map(arguments -> (Class<?>)arguments.get()[0]).collect(Collectors.toList());
}
private <T> T construct(Class<T> clazz)
{
try
{
@SuppressWarnings("unchecked")
T instance = (T)clazz.getConstructors()[0].newInstance();
return instance;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -230,7 +230,7 @@ public class ServerConfigTest
assertNull(serverEndpoint.error);
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.NO_CODE));
assertThat(clientEndpoint.closeCode, is(StatusCode.NO_CODE));
listener.assertClosed();
}
@ -251,7 +251,7 @@ public class ServerConfigTest
assertThat(serverEndpoint.error, instanceOf(MessageTooLargeException.class));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.MESSAGE_TOO_LARGE));
assertThat(clientEndpoint.closeCode, is(StatusCode.MESSAGE_TOO_LARGE));
listener.assertClosed();
}
@ -267,7 +267,7 @@ public class ServerConfigTest
connect.get(5, TimeUnit.SECONDS);
clientEndpoint.session.getRemote().sendString("hello world");
String msg = serverEndpoint.messageQueue.poll(500, TimeUnit.MILLISECONDS);
String msg = serverEndpoint.textMessages.poll(500, TimeUnit.MILLISECONDS);
assertThat(msg, is("hello world"));
Thread.sleep(idleTimeout + 500);
@ -275,7 +275,7 @@ public class ServerConfigTest
assertThat(serverEndpoint.error, instanceOf(WebSocketTimeoutException.class));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.SHUTDOWN));
assertThat(clientEndpoint.closeCode, is(StatusCode.SHUTDOWN));
listener.assertClosed();
}
@ -296,7 +296,7 @@ public class ServerConfigTest
assertThat(serverEndpoint.error, instanceOf(MessageTooLargeException.class));
assertTrue(clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(clientEndpoint.statusCode, is(StatusCode.MESSAGE_TOO_LARGE));
assertThat(clientEndpoint.closeCode, is(StatusCode.MESSAGE_TOO_LARGE));
listener.assertClosed();
}

View File

@ -42,7 +42,8 @@ public class ByteArrayMessageSink extends AbstractMessageSink
{
super(session, methodHandle);
// byte[] buf
// This uses the offset length byte array signature not supported by javax websocket.
// The javax layer instead uses decoders for whole byte array messages instead of this message sink.
MethodType onMessageType = MethodType.methodType(Void.TYPE, byte[].class, int.class, int.class);
if (methodHandle.type().changeReturnType(void.class) != onMessageType.changeReturnType(void.class))
{

View File

@ -19,13 +19,11 @@
package org.eclipse.jetty.websocket.util.messages;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.util.InvalidSignatureException;
public class PartialByteArrayMessageSink extends AbstractMessageSink
{
@ -34,13 +32,6 @@ public class PartialByteArrayMessageSink extends AbstractMessageSink
public PartialByteArrayMessageSink(CoreSession session, MethodHandle methodHandle)
{
super(session, methodHandle);
// byte[] buf, int offset, int length
MethodType onMessageType = MethodType.methodType(Void.TYPE, byte[].class, int.class, int.class, boolean.class);
if (methodHandle.type() != onMessageType)
{
throw InvalidSignatureException.build(onMessageType, methodHandle.type());
}
}
@Override
@ -51,7 +42,7 @@ public class PartialByteArrayMessageSink extends AbstractMessageSink
if (frame.hasPayload() || frame.isFin())
{
byte[] buffer = frame.hasPayload() ? BufferUtil.toArray(frame.getPayload()) : EMPTY_BUFFER;
methodHandle.invoke(buffer, 0, buffer.length, frame.isFin());
methodHandle.invoke(buffer, frame.isFin());
}
callback.succeeded();

View File

@ -29,14 +29,6 @@ public class PartialByteBufferMessageSink extends AbstractMessageSink
public PartialByteBufferMessageSink(CoreSession session, MethodHandle methodHandle)
{
super(session, methodHandle);
/* TODO: Review
MethodType onMessageType = MethodType.methodType(Void.TYPE, ByteBuffer.class, boolean.class);
if (methodHandle.type() != onMessageType)
{
throw InvalidSignatureException.build(onMessageType, methodHandle.type());
}
*/
}
@Override