Skip to content

Commit cf96416

Browse files
committed
StompSubProtocolHandler allows version 1.0
Issue: SPR-17258
1 parent 97cea7f commit cf96416

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
8585
*/
8686
public static final String CONNECTED_USER_HEADER = "user-name";
8787

88+
private static final String[] SUPPORTED_VERSIONS = {"1.2", "1.1", "1.0"};
89+
8890
private static final Log logger = LogFactory.getLog(StompSubProtocolHandler.class);
8991

9092
private static final byte[] EMPTY_PAYLOAD = new byte[0];
@@ -524,15 +526,12 @@ private StompHeaderAccessor convertConnectAcktoStompConnected(StompHeaderAccesso
524526

525527
if (connectHeaders != null) {
526528
Set<String> acceptVersions = connectHeaders.getAcceptVersion();
527-
if (acceptVersions.contains("1.2")) {
528-
connectedHeaders.setVersion("1.2");
529-
}
530-
else if (acceptVersions.contains("1.1")) {
531-
connectedHeaders.setVersion("1.1");
532-
}
533-
else if (!acceptVersions.isEmpty()) {
534-
throw new IllegalArgumentException("Unsupported STOMP version '" + acceptVersions + "'");
535-
}
529+
connectedHeaders.setVersion(
530+
Arrays.stream(SUPPORTED_VERSIONS)
531+
.filter(acceptVersions::contains)
532+
.findAny()
533+
.orElseThrow(() -> new IllegalArgumentException(
534+
"Unsupported STOMP version '" + acceptVersions + "'")));
536535
}
537536

538537
long[] heartbeat = (long[]) connectAckHeaders.getHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER);

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void handleMessageToClientWithSimpConnectAck() {
126126

127127
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
128128
accessor.setHeartbeat(10000, 10000);
129-
accessor.setAcceptVersion("1.0,1.1");
129+
accessor.setAcceptVersion("1.0,1.1,1.2");
130130
Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
131131

132132
SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
@@ -137,7 +137,7 @@ public void handleMessageToClientWithSimpConnectAck() {
137137

138138
assertEquals(1, this.session.getSentMessages().size());
139139
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
140-
assertEquals("CONNECTED\n" + "version:1.1\n" + "heart-beat:15000,15000\n" +
140+
assertEquals("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" +
141141
"user-name:joe\n" + "\n" + "\u0000", actual.getPayload());
142142
}
143143

@@ -146,7 +146,7 @@ public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {
146146

147147
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
148148
accessor.setHeartbeat(10000, 10000);
149-
accessor.setAcceptVersion("1.0,1.1");
149+
accessor.setAcceptVersion("1.0");
150150
Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
151151

152152
SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
@@ -156,7 +156,7 @@ public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {
156156

157157
assertEquals(1, this.session.getSentMessages().size());
158158
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
159-
assertEquals("CONNECTED\n" + "version:1.1\n" + "heart-beat:0,0\n" +
159+
assertEquals("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" +
160160
"user-name:joe\n" + "\n" + "\u0000", actual.getPayload());
161161
}
162162

0 commit comments

Comments
 (0)