Skip to content

Commit e87355b

Browse files
committed
STOMP client supports setting accept-version
Issue: SPR-16844
1 parent 1dc8201 commit e87355b

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ public void afterConnected(TcpConnection<byte[]> connection) {
379379
}
380380
StompHeaderAccessor accessor = createHeaderAccessor(StompCommand.CONNECT);
381381
accessor.addNativeHeaders(this.connectHeaders);
382-
accessor.setAcceptVersion("1.1,1.2");
382+
if (this.connectHeaders.getAcceptVersion() == null) {
383+
accessor.setAcceptVersion("1.1,1.2");
384+
}
383385
Message<byte[]> message = createMessage(accessor, EMPTY_PAYLOAD);
384386
execute(message);
385387
}

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

Lines changed: 31 additions & 1 deletion
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-2018 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.simp.stomp;
1818

1919
import java.io.Serializable;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.LinkedHashMap;
@@ -31,6 +32,7 @@
3132
import org.springframework.util.MimeType;
3233
import org.springframework.util.MimeTypeUtils;
3334
import org.springframework.util.MultiValueMap;
35+
import org.springframework.util.ObjectUtils;
3436
import org.springframework.util.StringUtils;
3537

3638
/**
@@ -66,6 +68,8 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
6668

6769
public static final String HOST = "host";
6870

71+
public static final String ACCEPT_VERSION = "accept-version";
72+
6973
public static final String LOGIN = "login";
7074

7175
public static final String PASSCODE = "passcode";
@@ -194,6 +198,32 @@ public String getHost() {
194198
return getFirst(HOST);
195199
}
196200

201+
/**
202+
* Set the accept-version header. Must be one of "1.1", "1.2", or both.
203+
* Applies to the CONNECT frame.
204+
* @since 5.0.7
205+
*/
206+
public void setAcceptVersion(@Nullable String[] acceptVersions) {
207+
if (ObjectUtils.isEmpty(acceptVersions)) {
208+
set(ACCEPT_VERSION, null);
209+
return;
210+
}
211+
Arrays.stream(acceptVersions).forEach(version ->
212+
Assert.isTrue(version != null && (version.equals("1.1") || version.equals("1.2")),
213+
"Invalid version: " + version));
214+
set(ACCEPT_VERSION, StringUtils.arrayToCommaDelimitedString(acceptVersions));
215+
}
216+
217+
/**
218+
* Get the accept-version header.
219+
* @since 5.0.7
220+
*/
221+
@Nullable
222+
public String[] getAcceptVersion() {
223+
String value = getFirst(ACCEPT_VERSION);
224+
return value != null ? StringUtils.commaDelimitedListToStringArray(value) : null;
225+
}
226+
197227
/**
198228
* Set the login header.
199229
* Applies to the CONNECT frame.

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

Lines changed: 43 additions & 30 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-2018 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.
@@ -76,7 +76,7 @@ public class DefaultStompSessionTests {
7676

7777

7878
@Before
79-
public void setUp() throws Exception {
79+
public void setUp() {
8080
MockitoAnnotations.initMocks(this);
8181

8282
this.sessionHandler = mock(StompSessionHandler.class);
@@ -91,14 +91,14 @@ public void setUp() throws Exception {
9191

9292

9393
@Test
94-
public void afterConnected() throws Exception {
94+
public void afterConnected() {
9595
assertFalse(this.session.isConnected());
9696
this.connectHeaders.setHost("my-host");
9797
this.connectHeaders.setHeartbeat(new long[] {11, 12});
9898

9999
this.session.afterConnected(this.connection);
100-
assertTrue(this.session.isConnected());
101100

101+
assertTrue(this.session.isConnected());
102102
Message<byte[]> message = this.messageCaptor.getValue();
103103
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
104104
assertEquals(StompCommand.CONNECT, accessor.getCommand());
@@ -107,16 +107,29 @@ public void afterConnected() throws Exception {
107107
assertArrayEquals(new long[] {11, 12}, accessor.getHeartbeat());
108108
}
109109

110+
@Test // SPR-16844
111+
public void afterConnectedWithSpecificVersion() {
112+
assertFalse(this.session.isConnected());
113+
this.connectHeaders.setAcceptVersion(new String[] {"1.1"});
114+
115+
this.session.afterConnected(this.connection);
116+
117+
Message<byte[]> message = this.messageCaptor.getValue();
118+
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
119+
assertEquals(StompCommand.CONNECT, accessor.getCommand());
120+
assertThat(accessor.getAcceptVersion(), containsInAnyOrder("1.1"));
121+
}
122+
110123
@Test
111-
public void afterConnectFailure() throws Exception {
124+
public void afterConnectFailure() {
112125
IllegalStateException exception = new IllegalStateException("simulated exception");
113126
this.session.afterConnectFailure(exception);
114127
verify(this.sessionHandler).handleTransportError(this.session, exception);
115128
verifyNoMoreInteractions(this.sessionHandler);
116129
}
117130

118131
@Test
119-
public void handleConnectedFrame() throws Exception {
132+
public void handleConnectedFrame() {
120133
this.session.afterConnected(this.connection);
121134
assertTrue(this.session.isConnected());
122135

@@ -134,7 +147,7 @@ public void handleConnectedFrame() throws Exception {
134147
}
135148

136149
@Test
137-
public void heartbeatValues() throws Exception {
150+
public void heartbeatValues() {
138151
this.session.afterConnected(this.connection);
139152
assertTrue(this.session.isConnected());
140153

@@ -156,7 +169,7 @@ public void heartbeatValues() throws Exception {
156169
}
157170

158171
@Test
159-
public void heartbeatNotSupportedByServer() throws Exception {
172+
public void heartbeatNotSupportedByServer() {
160173
this.session.afterConnected(this.connection);
161174
verify(this.connection).send(any());
162175

@@ -172,7 +185,7 @@ public void heartbeatNotSupportedByServer() throws Exception {
172185
}
173186

174187
@Test
175-
public void heartbeatTasks() throws Exception {
188+
public void heartbeatTasks() {
176189
this.session.afterConnected(this.connection);
177190
verify(this.connection).send(any());
178191

@@ -207,7 +220,7 @@ public void heartbeatTasks() throws Exception {
207220
}
208221

209222
@Test
210-
public void handleErrorFrame() throws Exception {
223+
public void handleErrorFrame() {
211224
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
212225
accessor.setContentType(new MimeType("text", "plain", StandardCharsets.UTF_8));
213226
accessor.addNativeHeader("foo", "bar");
@@ -226,7 +239,7 @@ public void handleErrorFrame() throws Exception {
226239
}
227240

228241
@Test
229-
public void handleErrorFrameWithEmptyPayload() throws Exception {
242+
public void handleErrorFrameWithEmptyPayload() {
230243
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
231244
accessor.addNativeHeader("foo", "bar");
232245
accessor.setLeaveMutable(true);
@@ -238,7 +251,7 @@ public void handleErrorFrameWithEmptyPayload() throws Exception {
238251
}
239252

240253
@Test
241-
public void handleErrorFrameWithConversionException() throws Exception {
254+
public void handleErrorFrameWithConversionException() {
242255
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
243256
accessor.setContentType(MimeTypeUtils.APPLICATION_JSON);
244257
accessor.addNativeHeader("foo", "bar");
@@ -257,7 +270,7 @@ public void handleErrorFrameWithConversionException() throws Exception {
257270
}
258271

259272
@Test
260-
public void handleMessageFrame() throws Exception {
273+
public void handleMessageFrame() {
261274
this.session.afterConnected(this.connection);
262275

263276
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
@@ -284,7 +297,7 @@ public void handleMessageFrame() throws Exception {
284297
}
285298

286299
@Test
287-
public void handleMessageFrameWithConversionException() throws Exception {
300+
public void handleMessageFrameWithConversionException() {
288301
this.session.afterConnected(this.connection);
289302
assertTrue(this.session.isConnected());
290303

@@ -314,7 +327,7 @@ public void handleMessageFrameWithConversionException() throws Exception {
314327
}
315328

316329
@Test
317-
public void handleFailure() throws Exception {
330+
public void handleFailure() {
318331
IllegalStateException exception = new IllegalStateException("simulated exception");
319332
this.session.handleFailure(exception);
320333

@@ -323,15 +336,15 @@ public void handleFailure() throws Exception {
323336
}
324337

325338
@Test
326-
public void afterConnectionClosed() throws Exception {
339+
public void afterConnectionClosed() {
327340
this.session.afterConnectionClosed();
328341

329342
verify(this.sessionHandler).handleTransportError(same(this.session), any(ConnectionLostException.class));
330343
verifyNoMoreInteractions(this.sessionHandler);
331344
}
332345

333346
@Test
334-
public void send() throws Exception {
347+
public void send() {
335348
this.session.afterConnected(this.connection);
336349
assertTrue(this.session.isConnected());
337350

@@ -353,7 +366,7 @@ public void send() throws Exception {
353366
}
354367

355368
@Test
356-
public void sendWithReceipt() throws Exception {
369+
public void sendWithReceipt() {
357370
this.session.afterConnected(this.connection);
358371
assertTrue(this.session.isConnected());
359372

@@ -376,7 +389,7 @@ public void sendWithReceipt() throws Exception {
376389
}
377390

378391
@Test
379-
public void sendWithConversionException() throws Exception {
392+
public void sendWithConversionException() {
380393
this.session.afterConnected(this.connection);
381394
assertTrue(this.session.isConnected());
382395

@@ -391,7 +404,7 @@ public void sendWithConversionException() throws Exception {
391404
}
392405

393406
@Test
394-
public void sendWithExecutionException() throws Exception {
407+
public void sendWithExecutionException() {
395408
this.session.afterConnected(this.connection);
396409
assertTrue(this.session.isConnected());
397410

@@ -409,7 +422,7 @@ public void sendWithExecutionException() throws Exception {
409422
}
410423

411424
@Test
412-
public void subscribe() throws Exception {
425+
public void subscribe() {
413426
this.session.afterConnected(this.connection);
414427
assertTrue(this.session.isConnected());
415428

@@ -428,7 +441,7 @@ public void subscribe() throws Exception {
428441
}
429442

430443
@Test
431-
public void subscribeWithHeaders() throws Exception {
444+
public void subscribeWithHeaders() {
432445
this.session.afterConnected(this.connection);
433446
assertTrue(this.session.isConnected());
434447

@@ -454,7 +467,7 @@ public void subscribeWithHeaders() throws Exception {
454467
}
455468

456469
@Test
457-
public void unsubscribe() throws Exception {
470+
public void unsubscribe() {
458471
this.session.afterConnected(this.connection);
459472
assertTrue(this.session.isConnected());
460473

@@ -473,7 +486,7 @@ public void unsubscribe() throws Exception {
473486
}
474487

475488
@Test // SPR-15131
476-
public void unsubscribeWithCustomHeader() throws Exception {
489+
public void unsubscribeWithCustomHeader() {
477490
this.session.afterConnected(this.connection);
478491
assertTrue(this.session.isConnected());
479492

@@ -501,7 +514,7 @@ public void unsubscribeWithCustomHeader() throws Exception {
501514
}
502515

503516
@Test
504-
public void ack() throws Exception {
517+
public void ack() {
505518
this.session.afterConnected(this.connection);
506519
assertTrue(this.session.isConnected());
507520

@@ -518,7 +531,7 @@ public void ack() throws Exception {
518531
}
519532

520533
@Test
521-
public void nack() throws Exception {
534+
public void nack() {
522535
this.session.afterConnected(this.connection);
523536
assertTrue(this.session.isConnected());
524537

@@ -535,7 +548,7 @@ public void nack() throws Exception {
535548
}
536549

537550
@Test
538-
public void receiptReceived() throws Exception {
551+
public void receiptReceived() {
539552
this.session.afterConnected(this.connection);
540553
this.session.setTaskScheduler(mock(TaskScheduler.class));
541554

@@ -559,7 +572,7 @@ public void receiptReceived() throws Exception {
559572
}
560573

561574
@Test
562-
public void receiptReceivedBeforeTaskAdded() throws Exception {
575+
public void receiptReceivedBeforeTaskAdded() {
563576
this.session.afterConnected(this.connection);
564577
this.session.setTaskScheduler(mock(TaskScheduler.class));
565578

@@ -583,7 +596,7 @@ public void receiptReceivedBeforeTaskAdded() throws Exception {
583596

584597
@Test
585598
@SuppressWarnings({ "unchecked", "rawtypes" })
586-
public void receiptNotReceived() throws Exception {
599+
public void receiptNotReceived() {
587600
TaskScheduler taskScheduler = mock(TaskScheduler.class);
588601

589602
this.session.afterConnected(this.connection);
@@ -614,7 +627,7 @@ public void receiptNotReceived() throws Exception {
614627
}
615628

616629
@Test
617-
public void disconnect() throws Exception {
630+
public void disconnect() {
618631
this.session.afterConnected(this.connection);
619632
assertTrue(this.session.isConnected());
620633

0 commit comments

Comments
 (0)