diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/AbstractWebSocketMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/AbstractWebSocketMessage.java index 23727cd20f..c4c272b266 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/AbstractWebSocketMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/AbstractWebSocketMessage.java @@ -69,10 +69,6 @@ public abstract class AbstractWebSocketMessage implements WebSocketMessage return this.last; } - @Override - public int hashCode() { - return AbstractWebSocketMessage.class.hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.payload); - } @Override public boolean equals(Object other) { @@ -86,10 +82,15 @@ public abstract class AbstractWebSocketMessage implements WebSocketMessage return ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload); } + @Override + public int hashCode() { + return ObjectUtils.nullSafeHashCode(this.payload); + } + @Override public String toString() { - return getClass().getSimpleName() + " payload= " + toStringPayload() - + ", byteCount=" + getPayloadLength() + ", last=" + isLast() + "]"; + return getClass().getSimpleName() + " payload=[" + toStringPayload() + + "], byteCount=" + getPayloadLength() + ", last=" + isLast() + "]"; } protected abstract String toStringPayload(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/BinaryMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/BinaryMessage.java index ab7137f847..b6023503c8 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/BinaryMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/BinaryMessage.java @@ -26,7 +26,6 @@ import java.nio.ByteBuffer; */ public final class BinaryMessage extends AbstractWebSocketMessage { - /** * Create a new binary WebSocket message with the given ByteBuffer payload. * @param payload the non-null payload @@ -78,7 +77,7 @@ public final class BinaryMessage extends AbstractWebSocketMessage { * @param isLast if the message is the last of a series of partial messages */ public BinaryMessage(byte[] payload, int offset, int length, boolean isLast) { - super((payload != null) ? ByteBuffer.wrap(payload, offset, length) : null, isLast); + super(payload != null ? ByteBuffer.wrap(payload, offset, length) : null, isLast); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/PingMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/PingMessage.java index a2c9c0bd77..32b57c6b79 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/PingMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/PingMessage.java @@ -26,7 +26,6 @@ import java.nio.ByteBuffer; */ public final class PingMessage extends AbstractWebSocketMessage { - /** * Create a new ping message with an empty payload. */ @@ -45,12 +44,12 @@ public final class PingMessage extends AbstractWebSocketMessage { @Override public int getPayloadLength() { - return (getPayload() != null) ? getPayload().remaining() : 0; + return (getPayload() != null ? getPayload().remaining() : 0); } @Override protected String toStringPayload() { - return (getPayload() != null) ? getPayload().toString() : null; + return (getPayload() != null ? getPayload().toString() : null); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/PongMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/PongMessage.java index 5ede3a3cf1..271aafa3dd 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/PongMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/PongMessage.java @@ -26,7 +26,6 @@ import java.nio.ByteBuffer; */ public final class PongMessage extends AbstractWebSocketMessage { - /** * Create a new pong message with an empty payload. */ @@ -45,12 +44,12 @@ public final class PongMessage extends AbstractWebSocketMessage { @Override public int getPayloadLength() { - return (getPayload() != null) ? getPayload().remaining() : 0; + return (getPayload() != null ? getPayload().remaining() : 0); } @Override protected String toStringPayload() { - return (getPayload() != null) ? getPayload().toString() : null; + return (getPayload() != null ? getPayload().toString() : null); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java index 7d482a3766..7586d83851 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/TextMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,9 +41,8 @@ public final class TextMessage extends AbstractWebSocketMessage { } /** - * Create a new text WebSocket message from the given byte[]. It is assumed the - * byte array can be encoded into an UTF-8 String. - * + * Create a new text WebSocket message from the given byte[]. It is assumed + * the byte array can be encoded into an UTF-8 String. * @param payload the non-null payload */ public TextMessage(byte[] payload) { @@ -76,7 +75,8 @@ public final class TextMessage extends AbstractWebSocketMessage { @Override protected String toStringPayload() { - return (getPayloadLength() > 10) ? getPayload().substring(0, 10) + ".." : getPayload(); + String payload = getPayload(); + return (payload.length() > 10 ? payload.substring(0, 10) + ".." : payload); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java index 6130fe925c..be435a2b2b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,12 +51,10 @@ public interface WebSocketSession { /** * Return the map with attributes associated with the WebSocket session. - * *

When the WebSocketSession is created, on the server side, the map can be * through a {@link org.springframework.web.socket.server.HandshakeInterceptor}. * On the client side, the map can be populated by passing attributes to the - * {@link org.springframework.web.socket.client.WebSocketClient} handshake - * methods. + * {@link org.springframework.web.socket.client.WebSocketClient} handshake methods. */ Map getAttributes(); @@ -109,17 +107,16 @@ public interface WebSocketSession { */ List getExtensions(); + /** + * Send a WebSocket message: either {@link TextMessage} or {@link BinaryMessage}. + */ + void sendMessage(WebSocketMessage message) throws IOException; + /** * Return whether the connection is still open. */ boolean isOpen(); - /** - * Send a WebSocket message either {@link TextMessage} or - * {@link BinaryMessage}. - */ - void sendMessage(WebSocketMessage message) throws IOException; - /** * Close the WebSocket connection with status 1000, i.e. equivalent to: *

diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/TextMessageTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/TextMessageTests.java
new file mode 100644
index 0000000000..650a259974
--- /dev/null
+++ b/spring-websocket/src/test/java/org/springframework/web/socket/TextMessageTests.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * Licensed 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.springframework.web.socket;
+
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Test fixture for {@link TextMessage}.
+ *
+ * @author Shinobu Aoki
+ * @author Juergen Hoeller
+ */
+public class TextMessageTests {
+
+	@Test
+	public void toStringWithAscii() {
+		String expected = "foo,bar";
+		TextMessage actual = new TextMessage(expected);
+		assertThat(actual.getPayload(), Matchers.is(expected));
+		assertThat(actual.toString(), Matchers.containsString(expected));
+	}
+
+	@Test
+	public void toStringWithMultibyteString() {
+		String expected = "\u3042\u3044\u3046\u3048\u304a";
+		TextMessage actual = new TextMessage(expected);
+		assertThat(actual.getPayload(), Matchers.is(expected));
+		assertThat(actual.toString(), Matchers.containsString(expected));
+	}
+
+}