Skip to content

Commit 1aaadb3

Browse files
committed
Support STOMP in addition to CONNECT frame
One is literally an alias for the other with "the advantage that a protocol sniffer/discriminator will be able to differentiate the STOMP connection from an HTTP connection". Closes gh-22652
1 parent 50acbae commit 1aaadb3

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -521,7 +521,7 @@ else if (accessor instanceof SimpMessageHeaderAccessor) {
521521
return;
522522
}
523523

524-
if (StompCommand.CONNECT.equals(command)) {
524+
if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) {
525525
if (logger.isDebugEnabled()) {
526526
logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
527527
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,15 +138,16 @@ private void writeHeaders(StompCommand command, Map<String, Object> headers, byt
138138
return;
139139
}
140140

141-
boolean shouldEscape = (command != StompCommand.CONNECT && command != StompCommand.CONNECTED);
141+
boolean shouldEscape = (command != StompCommand.CONNECT && command != StompCommand.STOMP
142+
&& command != StompCommand.CONNECTED);
142143

143144
for (Entry<String, List<String>> entry : nativeHeaders.entrySet()) {
144145
if (command.requiresContentLength() && "content-length".equals(entry.getKey())) {
145146
continue;
146147
}
147148

148149
List<String> values = entry.getValue();
149-
if (StompCommand.CONNECT.equals(command) &&
150+
if ((StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) &&
150151
StompHeaderAccessor.STOMP_PASSCODE_HEADER.equals(entry.getKey())) {
151152
values = Collections.singletonList(StompHeaderAccessor.getPasscode(headers));
152153
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,7 +160,7 @@ else if (StompCommand.SUBSCRIBE.equals(command) || StompCommand.UNSUBSCRIBE.equa
160160
super.setSubscriptionId(value);
161161
}
162162
}
163-
else if (StompCommand.CONNECT.equals(command)) {
163+
else if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) {
164164
protectPasscode();
165165
}
166166
}
@@ -425,6 +425,10 @@ else if (StompCommand.CONNECT.equals(command)) {
425425
Principal user = getUser();
426426
return "CONNECT" + (user != null ? " user=" + user.getName() : "") + appendSession();
427427
}
428+
else if (StompCommand.STOMP.equals(command)) {
429+
Principal user = getUser();
430+
return "STOMP" + (user != null ? " user=" + user.getName() : "") + appendSession();
431+
}
428432
else if (StompCommand.CONNECTED.equals(command)) {
429433
return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession();
430434
}

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompHeaderAccessorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,9 +102,9 @@ public void createWithConnectNativeHeaders() {
102102
extHeaders.add(StompHeaderAccessor.STOMP_LOGIN_HEADER, "joe");
103103
extHeaders.add(StompHeaderAccessor.STOMP_PASSCODE_HEADER, "joe123");
104104

105-
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.CONNECT, extHeaders);
105+
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.STOMP, extHeaders);
106106

107-
assertEquals(StompCommand.CONNECT, headerAccessor.getCommand());
107+
assertEquals(StompCommand.STOMP, headerAccessor.getCommand());
108108
assertEquals(SimpMessageType.CONNECT, headerAccessor.getMessageType());
109109
assertNotNull(headerAccessor.getHeader("stompCredentials"));
110110
assertEquals("joe", headerAccessor.getLogin());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -271,7 +271,7 @@ else if (webSocketMessage instanceof BinaryMessage) {
271271
}
272272

273273
StompCommand command = headerAccessor.getCommand();
274-
boolean isConnect = StompCommand.CONNECT.equals(command);
274+
boolean isConnect = StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command);
275275
if (isConnect) {
276276
this.stats.incrementConnectCount();
277277
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -289,7 +289,7 @@ public void handleMessageToClientWithBinaryWebSocketMessage() {
289289
@Test
290290
public void handleMessageFromClient() {
291291

292-
TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).headers(
292+
TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.STOMP).headers(
293293
"login:guest", "passcode:guest", "accept-version:1.1,1.0", "heart-beat:10000,10000").build();
294294

295295
this.protocolHandler.afterSessionStarted(this.session, this.channel);
@@ -307,7 +307,7 @@ public void handleMessageFromClient() {
307307
assertArrayEquals(new long[] {10000, 10000}, SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders()));
308308

309309
StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(actual);
310-
assertEquals(StompCommand.CONNECT, stompAccessor.getCommand());
310+
assertEquals(StompCommand.STOMP, stompAccessor.getCommand());
311311
assertEquals("guest", stompAccessor.getLogin());
312312
assertEquals("guest", stompAccessor.getPasscode());
313313
assertArrayEquals(new long[] {10000, 10000}, stompAccessor.getHeartbeat());

0 commit comments

Comments
 (0)