Merge pull request #3419 from eclipse/jetty-9.4.x-3389-javax.websocket-willdecode

Issue #3389 javax.websocket willDecode
This commit is contained in:
Joakim Erdfelt 2019-03-04 09:26:34 -05:00 committed by GitHub
commit ff6599bf77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 33 deletions

View File

@ -20,10 +20,8 @@ package org.eclipse.jetty.websocket.jsr356.annotations;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.OnMessage;
import org.eclipse.jetty.websocket.jsr356.JsrSession;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -55,12 +53,20 @@ public class OnMessageBinaryCallable extends OnMessageCallable
public Object call(Object endpoint, ByteBuffer buf, boolean partialFlag) throws DecodeException
{
super.args[idxMessageObject] = binaryDecoder.decode(buf);
if (idxPartialMessageFlag >= 0)
if (binaryDecoder.willDecode(buf.slice()))
{
super.args[idxPartialMessageFlag] = partialFlag;
super.args[idxMessageObject] = binaryDecoder.decode(buf);
if (idxPartialMessageFlag >= 0)
{
super.args[idxPartialMessageFlag] = partialFlag;
}
return super.call(endpoint, super.args);
}
else
{
// Per JSR356, if you cannot decode, discard the message.
return null;
}
return super.call(endpoint,super.args);
}
@Override

View File

@ -18,12 +18,9 @@
package org.eclipse.jetty.websocket.jsr356.annotations;
import java.io.Reader;
import java.lang.reflect.Method;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.OnMessage;
import org.eclipse.jetty.websocket.jsr356.JsrSession;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -55,12 +52,20 @@ public class OnMessageTextCallable extends OnMessageCallable
public Object call(Object endpoint, String str, boolean partialFlag) throws DecodeException
{
super.args[idxMessageObject] = textDecoder.decode(str);
if (idxPartialMessageFlag >= 0)
if (textDecoder.willDecode(str))
{
super.args[idxPartialMessageFlag] = partialFlag;
super.args[idxMessageObject] = textDecoder.decode(str);
if (idxPartialMessageFlag >= 0)
{
super.args[idxPartialMessageFlag] = partialFlag;
}
return super.call(endpoint, super.args);
}
else
{
// Per JSR356, if you cannot decode, discard the message.
return null;
}
return super.call(endpoint,super.args);
}
@Override

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.websocket.jsr356.messages;
import java.nio.ByteBuffer;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.Decoder.Binary;
@ -54,14 +55,19 @@ public class BinaryWholeMessage extends SimpleBinaryMessage
DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
Decoder.Binary<Object> binaryDecoder = (Binary<Object>)decoder.getDecoder();
try
ByteBuffer msg = BufferUtil.toBuffer(data);
if (binaryDecoder.willDecode(msg.slice()))
{
Object obj = binaryDecoder.decode(BufferUtil.toBuffer(data));
wholeHandler.onMessage(obj);
}
catch (DecodeException e)
{
throw new WebSocketException("Unable to decode binary data",e);
try
{
Object obj = binaryDecoder.decode(msg);
wholeHandler.onMessage(obj);
}
catch (DecodeException e)
{
throw new WebSocketException("Unable to decode binary data", e);
}
}
}
}

View File

@ -50,14 +50,18 @@ public class TextWholeMessage extends SimpleTextMessage
DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
Decoder.Text<Object> textDecoder = (Decoder.Text<Object>)decoder.getDecoder();
try
String msg = utf.toString();
if (textDecoder.willDecode(msg))
{
Object obj = textDecoder.decode(utf.toString());
wholeHandler.onMessage(obj);
}
catch (DecodeException e)
{
throw new WebSocketException("Unable to decode text data",e);
try
{
Object obj = textDecoder.decode(msg);
wholeHandler.onMessage(obj);
}
catch (DecodeException e)
{
throw new WebSocketException("Unable to decode text data", e);
}
}
}
}

View File

@ -18,9 +18,6 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
@ -28,7 +25,6 @@ import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
@ -62,12 +58,17 @@ import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.InputStreamSo
import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.ReaderParamSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.ReaderSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.StringReturnReaderParamSocket;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
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.containsString;
import static org.hamcrest.Matchers.nullValue;
public class EchoTest
{
private static final List<EchoCase> TESTCASES = new ArrayList<>();
@ -84,7 +85,7 @@ public class EchoTest
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage(Boolean.FALSE).expect("false");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("true").expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("TRUe").expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("Apple").expect("false");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("Apple").expect(null); // fails willDecode
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("false").expect("false");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage(true).expect("true");
@ -278,7 +279,8 @@ public class EchoTest
for (String expected : testcase.expectedStrings)
{
String actual = received.poll(Timeouts.POLL_EVENT, Timeouts.POLL_EVENT_UNIT);
assertThat("Received Echo Responses",actual,containsString(expected));
Matcher expectation = expected == null ? nullValue() : containsString(expected);
assertThat("Received Echo Responses", actual, expectation);
}
}
finally