Fix StompFrame.copy() does not copy headers

Motivation:
Incorrect behavior for StompFrame.copy() method.

Modification:
Added copying of frame headers

Result:
When you call the StompFrame.copy() method, the headers are also copied.

Fixes [#7561].
This commit is contained in:
amizurov 2018-01-03 19:15:33 +03:00 committed by Norman Maurer
parent 1879433ae6
commit 4afabd3f3c
4 changed files with 81 additions and 7 deletions

View File

@ -31,10 +31,16 @@ public class DefaultStompFrame extends DefaultStompHeadersSubframe implements St
} }
public DefaultStompFrame(StompCommand command, ByteBuf content) { 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) { if (content == null) {
throw new NullPointerException("content"); throw new NullPointerException("content");
} }
this.content = content; this.content = content;
} }
@ -60,7 +66,7 @@ public class DefaultStompFrame extends DefaultStompHeadersSubframe implements St
@Override @Override
public StompFrame replace(ByteBuf content) { public StompFrame replace(ByteBuf content) {
return new DefaultStompFrame(command, content); return new DefaultStompFrame(command, content, headers.copy());
} }
@Override @Override

View File

@ -16,14 +16,14 @@
package io.netty.handler.codec.stomp; 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.Iterator;
import java.util.List; import java.util.List;
import java.util.Map.Entry; 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_INSENSITIVE_HASHER;
import static io.netty.util.AsciiString.CASE_SENSITIVE_HASHER; import static io.netty.util.AsciiString.CASE_SENSITIVE_HASHER;
@ -58,4 +58,11 @@ public class DefaultStompHeaders
return contains(name, value, return contains(name, value,
ignoreCase ? CASE_INSENSITIVE_HASHER : CASE_SENSITIVE_HASHER); ignoreCase ? CASE_INSENSITIVE_HASHER : CASE_SENSITIVE_HASHER);
} }
@Override
public DefaultStompHeaders copy() {
DefaultStompHeaders copyHeaders = new DefaultStompHeaders();
copyHeaders.addImpl(this);
return copyHeaders;
}
} }

View File

@ -24,13 +24,19 @@ public class DefaultStompHeadersSubframe implements StompHeadersSubframe {
protected final StompCommand command; protected final StompCommand command;
protected DecoderResult decoderResult = DecoderResult.SUCCESS; protected DecoderResult decoderResult = DecoderResult.SUCCESS;
protected final StompHeaders headers = new DefaultStompHeaders(); protected final DefaultStompHeaders headers;
public DefaultStompHeadersSubframe(StompCommand command) { public DefaultStompHeadersSubframe(StompCommand command) {
this(command, null);
}
DefaultStompHeadersSubframe(StompCommand command, DefaultStompHeaders headers) {
if (command == null) { if (command == null) {
throw new NullPointerException("command"); throw new NullPointerException("command");
} }
this.command = command; this.command = command;
this.headers = headers == null ? new DefaultStompHeaders() : headers;
} }
@Override @Override

View File

@ -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());
}
}