From 1aaadb39c055cdb9a871a83587e7d0382e243c9f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 27 Mar 2019 16:19:11 -0400 Subject: [PATCH] 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 --- .../simp/stomp/StompBrokerRelayMessageHandler.java | 4 ++-- .../messaging/simp/stomp/StompEncoder.java | 7 ++++--- .../messaging/simp/stomp/StompHeaderAccessor.java | 8 ++++++-- .../messaging/simp/stomp/StompHeaderAccessorTests.java | 6 +++--- .../web/socket/messaging/StompSubProtocolHandler.java | 4 ++-- .../socket/messaging/StompSubProtocolHandlerTests.java | 6 +++--- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java index 82a14d998c..a130fd241f 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -521,7 +521,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler return; } - if (StompCommand.CONNECT.equals(command)) { + if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) { if (logger.isDebugEnabled()) { logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD)); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java index b43160f113..4833ed7f5f 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -138,7 +138,8 @@ public class StompEncoder { return; } - boolean shouldEscape = (command != StompCommand.CONNECT && command != StompCommand.CONNECTED); + boolean shouldEscape = (command != StompCommand.CONNECT && command != StompCommand.STOMP + && command != StompCommand.CONNECTED); for (Entry> entry : nativeHeaders.entrySet()) { if (command.requiresContentLength() && "content-length".equals(entry.getKey())) { @@ -146,7 +147,7 @@ public class StompEncoder { } List values = entry.getValue(); - if (StompCommand.CONNECT.equals(command) && + if ((StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) && StompHeaderAccessor.STOMP_PASSCODE_HEADER.equals(entry.getKey())) { values = Collections.singletonList(StompHeaderAccessor.getPasscode(headers)); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java index f5a71b9702..df39889901 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -160,7 +160,7 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { super.setSubscriptionId(value); } } - else if (StompCommand.CONNECT.equals(command)) { + else if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) { protectPasscode(); } } @@ -425,6 +425,10 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { Principal user = getUser(); return "CONNECT" + (user != null ? " user=" + user.getName() : "") + appendSession(); } + else if (StompCommand.STOMP.equals(command)) { + Principal user = getUser(); + return "STOMP" + (user != null ? " user=" + user.getName() : "") + appendSession(); + } else if (StompCommand.CONNECTED.equals(command)) { return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession(); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompHeaderAccessorTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompHeaderAccessorTests.java index 50ca1bbd30..68f8a5c43c 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompHeaderAccessorTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompHeaderAccessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -102,9 +102,9 @@ public class StompHeaderAccessorTests { extHeaders.add(StompHeaderAccessor.STOMP_LOGIN_HEADER, "joe"); extHeaders.add(StompHeaderAccessor.STOMP_PASSCODE_HEADER, "joe123"); - StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.CONNECT, extHeaders); + StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.STOMP, extHeaders); - assertEquals(StompCommand.CONNECT, headerAccessor.getCommand()); + assertEquals(StompCommand.STOMP, headerAccessor.getCommand()); assertEquals(SimpMessageType.CONNECT, headerAccessor.getMessageType()); assertNotNull(headerAccessor.getHeader("stompCredentials")); assertEquals("joe", headerAccessor.getLogin()); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index 35c6a03040..cfdfbd7606 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -271,7 +271,7 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE } StompCommand command = headerAccessor.getCommand(); - boolean isConnect = StompCommand.CONNECT.equals(command); + boolean isConnect = StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command); if (isConnect) { this.stats.incrementConnectCount(); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java index e5bb34fb54..e93a8f1473 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -289,7 +289,7 @@ public class StompSubProtocolHandlerTests { @Test public void handleMessageFromClient() { - TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).headers( + TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.STOMP).headers( "login:guest", "passcode:guest", "accept-version:1.1,1.0", "heart-beat:10000,10000").build(); this.protocolHandler.afterSessionStarted(this.session, this.channel); @@ -307,7 +307,7 @@ public class StompSubProtocolHandlerTests { assertArrayEquals(new long[] {10000, 10000}, SimpMessageHeaderAccessor.getHeartbeat(actual.getHeaders())); StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(actual); - assertEquals(StompCommand.CONNECT, stompAccessor.getCommand()); + assertEquals(StompCommand.STOMP, stompAccessor.getCommand()); assertEquals("guest", stompAccessor.getLogin()); assertEquals("guest", stompAccessor.getPasscode()); assertArrayEquals(new long[] {10000, 10000}, stompAccessor.getHeartbeat()); -- GitLab