From f64c43572730630916786d01daceec35a9341190 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 25 Sep 2015 14:10:20 +0200 Subject: [PATCH 1/2] ARTEMIS-228 accept binary WebSocket frames --- .../stomp/WebSocketServerHandler.java | 4 +-- ...va => AbstractStompOverWebsocketTest.java} | 8 ++--- .../stomp/StompOverWebsocketBinaryTest.java | 30 +++++++++++++++++++ .../stomp/StompOverWebsocketTextTest.java | 27 +++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) rename tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/{StompOverWebsocketTest.java => AbstractStompOverWebsocketTest.java} (95%) create mode 100644 tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java create mode 100644 tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/stomp/WebSocketServerHandler.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/stomp/WebSocketServerHandler.java index 92493feabe..62c41d3d51 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/stomp/WebSocketServerHandler.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/stomp/WebSocketServerHandler.java @@ -110,7 +110,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler ctx.writeAndFlush(new PongWebSocketFrame(frame.content().retain())); return false; } - else if (!(frame instanceof TextWebSocketFrame)) { + else if (!(frame instanceof TextWebSocketFrame) && !(frame instanceof BinaryWebSocketFrame)) { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } return true; @@ -153,4 +153,4 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler } } -} \ No newline at end of file +} diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java similarity index 95% rename from tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java rename to tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java index 38e9e69ba8..ba760fb9c8 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java @@ -31,14 +31,13 @@ import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketVersion; import io.netty.handler.codec.string.StringDecoder; -public class StompOverWebsocketTest extends StompTest { +public abstract class AbstractStompOverWebsocketTest extends StompTest { private ChannelPromise handshakeFuture; @@ -112,8 +111,7 @@ public class StompOverWebsocketTest extends StompTest { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { try { if (msg instanceof String) { - TextWebSocketFrame frame = new TextWebSocketFrame((String) msg); - ctx.write(frame, promise); + ctx.write(createFrame((String) msg), promise); } else { super.write(ctx, msg, promise); @@ -124,4 +122,6 @@ public class StompOverWebsocketTest extends StompTest { } } } + + abstract WebSocketFrame createFrame(String msg); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java new file mode 100644 index 0000000000..f0d37825a3 --- /dev/null +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.artemis.tests.integration.stomp; + +import java.nio.charset.Charset; + +import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import io.netty.buffer.Unpooled; + +public class StompOverWebsocketBinaryTest extends AbstractStompOverWebsocketTest { + + protected WebSocketFrame createFrame(String msg) { + return new BinaryWebSocketFrame(Unpooled.copiedBuffer(msg, Charset.forName("UTF-8"))); + } +} diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java new file mode 100644 index 0000000000..ac11d71542 --- /dev/null +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.artemis.tests.integration.stomp; + +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketFrame; + +public class StompOverWebsocketTextTest extends AbstractStompOverWebsocketTest { + + protected WebSocketFrame createFrame(String msg) { + return new TextWebSocketFrame(msg); + } +} From f0fd89f24f738765891495366e6f7bc5c94a0789 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Fri, 25 Sep 2015 14:47:49 -0400 Subject: [PATCH 2/2] Small tweak on test: Using Parameterized instead of exension --- .../stomp/StompOverWebsocketBinaryTest.java | 30 ---------------- ...tTest.java => StompOverWebsocketTest.java} | 35 +++++++++++++++++-- .../stomp/StompOverWebsocketTextTest.java | 27 -------------- 3 files changed, 33 insertions(+), 59 deletions(-) delete mode 100644 tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java rename tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/{AbstractStompOverWebsocketTest.java => StompOverWebsocketTest.java} (83%) delete mode 100644 tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java deleted file mode 100644 index f0d37825a3..0000000000 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketBinaryTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.activemq.artemis.tests.integration.stomp; - -import java.nio.charset.Charset; - -import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; -import io.netty.handler.codec.http.websocketx.WebSocketFrame; -import io.netty.buffer.Unpooled; - -public class StompOverWebsocketBinaryTest extends AbstractStompOverWebsocketTest { - - protected WebSocketFrame createFrame(String msg) { - return new BinaryWebSocketFrame(Unpooled.copiedBuffer(msg, Charset.forName("UTF-8"))); - } -} diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java similarity index 83% rename from tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java rename to tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java index ba760fb9c8..4d900babce 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/AbstractStompOverWebsocketTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTest.java @@ -18,8 +18,13 @@ package org.apache.activemq.artemis.tests.integration.stomp; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; @@ -31,16 +36,33 @@ import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketVersion; import io.netty.handler.codec.string.StringDecoder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; -public abstract class AbstractStompOverWebsocketTest extends StompTest { +@RunWith(Parameterized.class) +public class StompOverWebsocketTest extends StompTest { private ChannelPromise handshakeFuture; + private final boolean useBinaryFrames; + + @Parameterized.Parameters(name = "useBinaryFrames={0}") + public static Collection data() { + List list = Arrays.asList(new Object[][]{{Boolean.TRUE}, {Boolean.FALSE}}); + return list; + } + + public StompOverWebsocketTest(Boolean useBinaryFrames) { + super(); + this.useBinaryFrames = useBinaryFrames; + } + @Override protected void addChannelHandlers(SocketChannel ch) throws URISyntaxException { ch.pipeline().addLast("http-codec", new HttpClientCodec()); @@ -123,5 +145,14 @@ public abstract class AbstractStompOverWebsocketTest extends StompTest { } } - abstract WebSocketFrame createFrame(String msg); + + protected WebSocketFrame createFrame(String msg) { + if (useBinaryFrames) { + return new BinaryWebSocketFrame(Unpooled.copiedBuffer(msg, Charset.forName("UTF-8"))); + } + else { + return new TextWebSocketFrame(msg); + } + } + } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java deleted file mode 100644 index ac11d71542..0000000000 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/StompOverWebsocketTextTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.activemq.artemis.tests.integration.stomp; - -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; -import io.netty.handler.codec.http.websocketx.WebSocketFrame; - -public class StompOverWebsocketTextTest extends AbstractStompOverWebsocketTest { - - protected WebSocketFrame createFrame(String msg) { - return new TextWebSocketFrame(msg); - } -}