1616
1717package org .springframework .integration .stomp .inbound ;
1818
19- import java .util .Collections ;
19+ import java .util .List ;
2020import java .util .Map ;
2121
2222import org .junit .jupiter .api .Test ;
3030import org .springframework .context .annotation .Bean ;
3131import org .springframework .context .annotation .Configuration ;
3232import org .springframework .http .MediaType ;
33- import org .springframework .http .server .ServerHttpRequest ;
34- import org .springframework .http .server .ServerHttpResponse ;
3533import org .springframework .integration .channel .QueueChannel ;
3634import org .springframework .integration .config .EnableIntegration ;
3735import org .springframework .integration .event .inbound .ApplicationEventListeningMessageProducer ;
6260import org .springframework .test .context .ContextConfiguration ;
6361import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
6462import org .springframework .util .MultiValueMap ;
65- import org .springframework .web .socket .WebSocketHandler ;
6663import org .springframework .web .socket .WebSocketHttpHeaders ;
6764import org .springframework .web .socket .client .WebSocketClient ;
6865import org .springframework .web .socket .client .standard .StandardWebSocketClient ;
7168import org .springframework .web .socket .config .annotation .WebSocketMessageBrokerConfigurer ;
7269import org .springframework .web .socket .messaging .SessionSubscribeEvent ;
7370import org .springframework .web .socket .messaging .WebSocketStompClient ;
74- import org .springframework .web .socket .server .HandshakeInterceptor ;
7571import org .springframework .web .socket .server .standard .StandardWebSocketUpgradeStrategy ;
7672import org .springframework .web .socket .server .support .DefaultHandshakeHandler ;
73+ import org .springframework .web .socket .sockjs .client .RestTemplateXhrTransport ;
7774import org .springframework .web .socket .sockjs .client .SockJsClient ;
7875import org .springframework .web .socket .sockjs .client .WebSocketTransport ;
7976
@@ -229,7 +226,7 @@ private boolean containsDestination(String destination, SubscriptionRegistry sub
229226
230227 // STOMP Client
231228
232- @ Configuration
229+ @ Configuration ( proxyBeanMethods = false )
233230 @ EnableIntegration
234231 public static class ContextConfiguration {
235232
@@ -240,26 +237,30 @@ public TomcatWebSocketTestServer server() {
240237
241238 @ Bean
242239 public WebSocketClient webSocketClient () {
243- return new SockJsClient (Collections .singletonList (new WebSocketTransport (new StandardWebSocketClient ())));
240+ return new SockJsClient (List .of (new RestTemplateXhrTransport (),
241+ new WebSocketTransport (new StandardWebSocketClient ())));
244242 }
245243
246244 @ Bean
247- public WebSocketStompClient stompClient (
248- @ Qualifier ("taskScheduler" ) TaskScheduler taskScheduler ) {
249- WebSocketStompClient webSocketStompClient = new WebSocketStompClient (webSocketClient ());
245+ public WebSocketStompClient stompClient (@ Qualifier ("taskScheduler" ) TaskScheduler taskScheduler ,
246+ WebSocketClient webSocketClient ) {
247+
248+ WebSocketStompClient webSocketStompClient = new WebSocketStompClient (webSocketClient );
250249 webSocketStompClient .setMessageConverter (new JacksonJsonMessageConverter ());
251250 webSocketStompClient .setTaskScheduler (taskScheduler );
252251 return webSocketStompClient ;
253252 }
254253
255254 @ Bean
256- public StompSessionManager stompSessionManager (WebSocketStompClient stompClient ) {
255+ public StompSessionManager stompSessionManager (WebSocketStompClient stompClient ,
256+ TomcatWebSocketTestServer server ) {
257+
257258 WebSocketStompSessionManager webSocketStompSessionManager =
258- new WebSocketStompSessionManager (stompClient , server () .getWsBaseUrl () + "/ws" );
259+ new WebSocketStompSessionManager (stompClient , server .getWsBaseUrl () + "/ws" );
259260 webSocketStompSessionManager .setAutoReceipt (true );
260261 webSocketStompSessionManager .setRecoveryInterval (1000 );
261262 WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders ();
262- handshakeHeaders .setOrigin ("https://www.example.com/ " );
263+ handshakeHeaders .setOrigin ("https://www.example.com" );
263264 webSocketStompSessionManager .setHandshakeHeaders (handshakeHeaders );
264265 StompHeaders stompHeaders = new StompHeaders ();
265266 stompHeaders .setHeartbeat (new long [] {10000 , 10000 });
@@ -278,11 +279,13 @@ public PollableChannel errorChannel() {
278279 }
279280
280281 @ Bean
281- public StompInboundChannelAdapter stompInboundChannelAdapter (StompSessionManager stompSessionFactory ) {
282+ public StompInboundChannelAdapter stompInboundChannelAdapter (StompSessionManager stompSessionFactory ,
283+ PollableChannel stompInputChannel , PollableChannel errorChannel ) {
284+
282285 StompInboundChannelAdapter adapter = new StompInboundChannelAdapter (stompSessionFactory , "/topic/myTopic" );
283286 adapter .setPayloadType (Map .class );
284- adapter .setOutputChannel (stompInputChannel () );
285- adapter .setErrorChannel (errorChannel () );
287+ adapter .setOutputChannel (stompInputChannel );
288+ adapter .setErrorChannel (errorChannel );
286289 return adapter ;
287290 }
288291
@@ -292,10 +295,10 @@ public PollableChannel stompEvents() {
292295 }
293296
294297 @ Bean
295- public ApplicationListener <ApplicationEvent > stompEventListener () {
298+ public ApplicationListener <ApplicationEvent > stompEventListener (PollableChannel stompEvents ) {
296299 ApplicationEventListeningMessageProducer producer = new ApplicationEventListeningMessageProducer ();
297300 producer .setEventTypes (StompIntegrationEvent .class );
298- producer .setOutputChannel (stompEvents () );
301+ producer .setOutputChannel (stompEvents );
299302 return producer ;
300303 }
301304
@@ -317,22 +320,6 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
317320 registry .addEndpoint ("/ws" )
318321 .setHandshakeHandler (handshakeHandler ())
319322 .setAllowedOrigins ("https://www.example.com" )
320- .addInterceptors (new HandshakeInterceptor () {
321-
322- @ Override
323- public boolean beforeHandshake (ServerHttpRequest request , ServerHttpResponse response ,
324- WebSocketHandler wsHandler , Map <String , Object > attributes ) {
325-
326- return request .getHeaders ().getOrigin () != null ;
327- }
328-
329- @ Override
330- public void afterHandshake (ServerHttpRequest request , ServerHttpResponse response ,
331- WebSocketHandler wsHandler , Exception exception ) {
332-
333- }
334-
335- })
336323 .withSockJS ();
337324 }
338325
0 commit comments