diff --git a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompFrame.java b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompFrame.java index d113ed6f2b..8df4c65043 100644 --- a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompFrame.java +++ b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompFrame.java @@ -31,10 +31,16 @@ public class DefaultStompFrame extends DefaultStompHeadersSubframe implements St } public DefaultStompFrame(StompCommand command, ByteBuf content) { - super(command); + this(command, content, null); + } + + DefaultStompFrame(StompCommand command, ByteBuf content, DefaultStompHeaders headers) { + super(command, headers); + if (content == null) { throw new NullPointerException("content"); } + this.content = content; } @@ -60,7 +66,7 @@ public class DefaultStompFrame extends DefaultStompHeadersSubframe implements St @Override public StompFrame replace(ByteBuf content) { - return new DefaultStompFrame(command, content); + return new DefaultStompFrame(command, content, headers.copy()); } @Override diff --git a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeaders.java b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeaders.java index 80fccf0f66..e9ad5b8d5b 100644 --- a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeaders.java +++ b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeaders.java @@ -16,14 +16,14 @@ package io.netty.handler.codec.stomp; -import io.netty.handler.codec.CharSequenceValueConverter; -import io.netty.handler.codec.DefaultHeaders; -import io.netty.handler.codec.HeadersUtils; - import java.util.Iterator; import java.util.List; import java.util.Map.Entry; +import io.netty.handler.codec.CharSequenceValueConverter; +import io.netty.handler.codec.DefaultHeaders; +import io.netty.handler.codec.HeadersUtils; + import static io.netty.util.AsciiString.CASE_INSENSITIVE_HASHER; import static io.netty.util.AsciiString.CASE_SENSITIVE_HASHER; @@ -58,4 +58,11 @@ public class DefaultStompHeaders return contains(name, value, ignoreCase ? CASE_INSENSITIVE_HASHER : CASE_SENSITIVE_HASHER); } + + @Override + public DefaultStompHeaders copy() { + DefaultStompHeaders copyHeaders = new DefaultStompHeaders(); + copyHeaders.addImpl(this); + return copyHeaders; + } } diff --git a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeadersSubframe.java b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeadersSubframe.java index ca7db33d13..728a8894be 100644 --- a/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeadersSubframe.java +++ b/codec-stomp/src/main/java/io/netty/handler/codec/stomp/DefaultStompHeadersSubframe.java @@ -24,13 +24,19 @@ public class DefaultStompHeadersSubframe implements StompHeadersSubframe { protected final StompCommand command; protected DecoderResult decoderResult = DecoderResult.SUCCESS; - protected final StompHeaders headers = new DefaultStompHeaders(); + protected final DefaultStompHeaders headers; public DefaultStompHeadersSubframe(StompCommand command) { + this(command, null); + } + + DefaultStompHeadersSubframe(StompCommand command, DefaultStompHeaders headers) { if (command == null) { throw new NullPointerException("command"); } + this.command = command; + this.headers = headers == null ? new DefaultStompHeaders() : headers; } @Override diff --git a/codec-stomp/src/test/java/io/netty/handler/codec/stomp/DefaultStompFrameTest.java b/codec-stomp/src/test/java/io/netty/handler/codec/stomp/DefaultStompFrameTest.java new file mode 100644 index 0000000000..c995ca7f1b --- /dev/null +++ b/codec-stomp/src/test/java/io/netty/handler/codec/stomp/DefaultStompFrameTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2018 The Netty Project + * + * The Netty Project 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 io.netty.handler.codec.stomp; + +import io.netty.util.AsciiString; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +public class DefaultStompFrameTest { + + @Test + public void testStompFrameCopy() { + StompFrame sourceFrame = new DefaultStompFrame(StompCommand.CONNECT); + + assertTrue(sourceFrame.headers().isEmpty()); + + sourceFrame.headers().set(StompHeaders.HOST, "localhost"); + + StompFrame copyFrame = sourceFrame.copy(); + + assertEquals(sourceFrame.headers(), copyFrame.headers()); + assertEquals(sourceFrame.content(), copyFrame.content()); + + AsciiString copyHeaderName = new AsciiString("foo"); + AsciiString copyHeaderValue = new AsciiString("bar"); + copyFrame.headers().set(copyHeaderName, copyHeaderValue); + + assertFalse(sourceFrame.headers().contains(copyHeaderName, copyHeaderValue)); + assertTrue(copyFrame.headers().contains(copyHeaderName, copyHeaderValue)); + + assertEquals(1, sourceFrame.headers().size()); + assertEquals(2, copyFrame.headers().size()); + assertNotEquals(sourceFrame.headers(), copyFrame.headers()); + + assertTrue(sourceFrame.release()); + assertTrue(copyFrame.release()); + } +}