Issue #3884 - Adding NullMessage to satisfy PR review

+ If an annotated Jetty WebSocket Endpoint doesn't have
  a handler for data types TEXT or BINARY then the
  new NullMessage (sink) is used for that specific data type
  to consume (quietly) those ignored Messages.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-07-18 08:03:16 -05:00
parent 02c49efc5f
commit f36358c631
3 changed files with 86 additions and 9 deletions

View File

@ -33,6 +33,7 @@ import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.RemoteEndpoint; import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
@ -204,6 +205,43 @@ public class WebSocketClientTest
} }
} }
@Test
public void testBasicEcho_PartialText_WithPartialBinary_FromClient() throws Exception
{
CloseTrackingEndpoint cliSock = new CloseTrackingEndpoint();
client.getPolicy().setIdleTimeout(10000);
URI wsUri = WSURI.toWebsocket(server.getURI().resolve("/echo"));
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols("echo");
Future<Session> future = client.connect(cliSock, wsUri, request);
try (Session sess = future.get(30, TimeUnit.SECONDS))
{
assertThat("Session", sess, notNullValue());
assertThat("Session.open", sess.isOpen(), is(true));
assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
Collection<WebSocketSession> sessions = client.getOpenSessions();
assertThat("client.sessions.size", sessions.size(), is(1));
RemoteEndpoint remote = cliSock.getSession().getRemote();
remote.sendPartialString("Hello", false);
remote.sendPartialString(" ", false);
remote.sendPartialString("World", true);
remote.sendPartialBytes(BufferUtil.toBuffer("It's a big enough umbrella, "), false);
remote.sendPartialBytes(BufferUtil.toBuffer("but it's always me that "), false);
remote.sendPartialBytes(BufferUtil.toBuffer("ends up getting wet."), true);
// wait for response from server
String received = cliSock.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Message", received, containsString("Hello World"));
}
}
@Test @Test
public void testBasicEcho_UsingCallback() throws Exception public void testBasicEcho_UsingCallback() throws Exception
{ {

View File

@ -33,6 +33,7 @@ import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.message.MessageAppender; import org.eclipse.jetty.websocket.common.message.MessageAppender;
import org.eclipse.jetty.websocket.common.message.MessageInputStream; import org.eclipse.jetty.websocket.common.message.MessageInputStream;
import org.eclipse.jetty.websocket.common.message.MessageReader; import org.eclipse.jetty.websocket.common.message.MessageReader;
import org.eclipse.jetty.websocket.common.message.NullMessage;
import org.eclipse.jetty.websocket.common.message.SimpleBinaryMessage; import org.eclipse.jetty.websocket.common.message.SimpleBinaryMessage;
import org.eclipse.jetty.websocket.common.message.SimpleTextMessage; import org.eclipse.jetty.websocket.common.message.SimpleTextMessage;
@ -84,6 +85,11 @@ public class JettyAnnotatedEventDriver extends AbstractEventDriver
if (events.onBinary == null) if (events.onBinary == null)
{ {
// not interested in binary events // not interested in binary events
if (activeMessage == null)
{
activeMessage = NullMessage.INSTANCE;
}
return; return;
} }
@ -152,15 +158,6 @@ public class JettyAnnotatedEventDriver extends AbstractEventDriver
} }
} }
@Override
public void onContinuationFrame(ByteBuffer buffer, boolean fin) throws IOException
{
if (events.onText != null || events.onBinary != null)
{
super.onContinuationFrame(buffer, fin);
}
}
@Override @Override
public void onError(Throwable cause) public void onError(Throwable cause)
{ {
@ -207,6 +204,10 @@ public class JettyAnnotatedEventDriver extends AbstractEventDriver
if (events.onText == null) if (events.onText == null)
{ {
// not interested in text events // not interested in text events
if (activeMessage == null)
{
activeMessage = NullMessage.INSTANCE;
}
return; return;
} }

View File

@ -0,0 +1,38 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.common.message;
import java.nio.ByteBuffer;
public class NullMessage implements MessageAppender
{
public static final MessageAppender INSTANCE = new NullMessage();
@Override
public void appendFrame(ByteBuffer framePayload, boolean isLast)
{
// consume payload
framePayload.position(framePayload.limit());
}
@Override
public void messageComplete()
{
}
}