Search in sources :

Example 1 with WebSocketTransportHandler

use of org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler in project spring-framework by spring-projects.

the class WebSocketHandlerRegistrationTests method handshakeHandlerPassedToSockJsRegistration.

@Test
public void handshakeHandlerPassedToSockJsRegistration() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
    this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);
    List<Mapping> mappings = this.registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Mapping mapping = mappings.get(0);
    assertThat(mapping.webSocketHandler).isEqualTo(handler);
    assertThat(mapping.path).isEqualTo("/foo/**");
    assertThat(mapping.sockJsService).isNotNull();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
    assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) Test(org.junit.jupiter.api.Test)

Example 2 with WebSocketTransportHandler

use of org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler in project spring-framework by spring-projects.

the class TestMessageCodec method sockJs.

@Test
public void sockJs() {
    loadBeanDefinitions("websocket-config-handlers-sockjs.xml");
    SimpleUrlHandlerMapping handlerMapping = this.appContext.getBean(SimpleUrlHandlerMapping.class);
    assertThat(handlerMapping).isNotNull();
    SockJsHttpRequestHandler testHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertThat(testHandler).isNotNull();
    unwrapAndCheckDecoratedHandlerType(testHandler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService testSockJsService = testHandler.getSockJsService();
    SockJsHttpRequestHandler fooHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/foo/**");
    assertThat(fooHandler).isNotNull();
    unwrapAndCheckDecoratedHandlerType(fooHandler.getWebSocketHandler(), FooWebSocketHandler.class);
    SockJsService sockJsService = fooHandler.getSockJsService();
    assertThat(sockJsService).isNotNull();
    assertThat(sockJsService).isSameAs(testSockJsService);
    assertThat(sockJsService).isInstanceOf(DefaultSockJsService.class);
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsService;
    assertThat(defaultSockJsService.getTaskScheduler()).isInstanceOf(ThreadPoolTaskScheduler.class);
    assertThat(defaultSockJsService.shouldSuppressCors()).isFalse();
    Map<TransportType, TransportHandler> handlerMap = defaultSockJsService.getTransportHandlers();
    assertThat(handlerMap.values()).extracting("class").containsExactlyInAnyOrder(XhrPollingTransportHandler.class, XhrReceivingTransportHandler.class, XhrStreamingTransportHandler.class, EventSourceTransportHandler.class, HtmlFileTransportHandler.class, WebSocketTransportHandler.class);
    WebSocketTransportHandler handler = (WebSocketTransportHandler) handlerMap.get(TransportType.WEBSOCKET);
    assertThat(handler.getHandshakeHandler().getClass()).isEqualTo(TestHandshakeHandler.class);
    List<HandshakeInterceptor> interceptors = defaultSockJsService.getHandshakeInterceptors();
    assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
}
Also used : HtmlFileTransportHandler(org.springframework.web.socket.sockjs.transport.handler.HtmlFileTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) XhrStreamingTransportHandler(org.springframework.web.socket.sockjs.transport.handler.XhrStreamingTransportHandler) XhrPollingTransportHandler(org.springframework.web.socket.sockjs.transport.handler.XhrPollingTransportHandler) EventSourceTransportHandler(org.springframework.web.socket.sockjs.transport.handler.EventSourceTransportHandler) XhrReceivingTransportHandler(org.springframework.web.socket.sockjs.transport.handler.XhrReceivingTransportHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) Test(org.junit.jupiter.api.Test)

Example 3 with WebSocketTransportHandler

use of org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler in project spring-framework by spring-projects.

the class WebMvcStompWebSocketEndpointRegistration method withSockJS.

@Override
public SockJsServiceRegistration withSockJS() {
    this.registration = new SockJsServiceRegistration();
    this.registration.setTaskScheduler(this.sockJsTaskScheduler);
    HandshakeInterceptor[] interceptors = getInterceptors();
    if (interceptors.length > 0) {
        this.registration.setInterceptors(interceptors);
    }
    if (this.handshakeHandler != null) {
        WebSocketTransportHandler handler = new WebSocketTransportHandler(this.handshakeHandler);
        this.registration.setTransportHandlerOverrides(handler);
    }
    if (!this.allowedOrigins.isEmpty()) {
        this.registration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
    }
    if (!this.allowedOriginPatterns.isEmpty()) {
        this.registration.setAllowedOriginPatterns(StringUtils.toStringArray(this.allowedOriginPatterns));
    }
    return this.registration;
}
Also used : HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)

Example 4 with WebSocketTransportHandler

use of org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler in project spring-framework by spring-projects.

the class AbstractWebSocketHandlerRegistration method withSockJS.

@Override
public SockJsServiceRegistration withSockJS() {
    this.sockJsServiceRegistration = new SockJsServiceRegistration();
    HandshakeInterceptor[] interceptors = getInterceptors();
    if (interceptors.length > 0) {
        this.sockJsServiceRegistration.setInterceptors(interceptors);
    }
    if (this.handshakeHandler != null) {
        WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
        this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
    }
    if (!this.allowedOrigins.isEmpty()) {
        this.sockJsServiceRegistration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
    }
    if (!this.allowedOriginPatterns.isEmpty()) {
        this.sockJsServiceRegistration.setAllowedOriginPatterns(StringUtils.toStringArray(this.allowedOriginPatterns));
    }
    return this.sockJsServiceRegistration;
}
Also used : HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)

Example 5 with WebSocketTransportHandler

use of org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler in project spring-framework by spring-projects.

the class TestValidator method simpleBroker.

@Test
public void simpleBroker() throws Exception {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
    assertThat(hm).isInstanceOf(SimpleUrlHandlerMapping.class);
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap()).hasSize(4);
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo");
    assertThat(httpRequestHandler).isNotNull();
    assertThat(httpRequestHandler).isInstanceOf(WebSocketHttpRequestHandler.class);
    WebSocketHttpRequestHandler wsHttpRequestHandler = (WebSocketHttpRequestHandler) httpRequestHandler;
    HandshakeHandler handshakeHandler = wsHttpRequestHandler.getHandshakeHandler();
    assertThat(handshakeHandler).isNotNull();
    assertThat(handshakeHandler instanceof TestHandshakeHandler).isTrue();
    List<HandshakeInterceptor> interceptors = wsHttpRequestHandler.getHandshakeInterceptors();
    assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
    WebSocketSession session = new TestWebSocketSession("id");
    wsHttpRequestHandler.getWebSocketHandler().afterConnectionEstablished(session);
    assertThat(session.getAttributes().get("decorated")).asInstanceOf(BOOLEAN).isTrue();
    WebSocketHandler wsHandler = wsHttpRequestHandler.getWebSocketHandler();
    assertThat(wsHandler).isInstanceOf(ExceptionWebSocketHandlerDecorator.class);
    wsHandler = ((ExceptionWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler).isInstanceOf(LoggingWebSocketHandlerDecorator.class);
    wsHandler = ((LoggingWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler).isInstanceOf(TestWebSocketHandlerDecorator.class);
    wsHandler = ((TestWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
    assertThat(this.appContext.getBean(MessageBrokerBeanDefinitionParser.WEB_SOCKET_HANDLER_BEAN_NAME)).isSameAs(wsHandler);
    SubProtocolWebSocketHandler subProtocolWsHandler = (SubProtocolWebSocketHandler) wsHandler;
    assertThat(subProtocolWsHandler.getSubProtocols()).isEqualTo(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
    assertThat(subProtocolWsHandler.getSendTimeLimit()).isEqualTo(25 * 1000);
    assertThat(subProtocolWsHandler.getSendBufferSizeLimit()).isEqualTo(1024 * 1024);
    assertThat(subProtocolWsHandler.getTimeToFirstMessage()).isEqualTo(30 * 1000);
    Map<String, SubProtocolHandler> handlerMap = subProtocolWsHandler.getProtocolHandlerMap();
    StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) handlerMap.get("v12.stomp");
    assertThat(stompHandler).isNotNull();
    assertThat(stompHandler.getMessageSizeLimit()).isEqualTo(128 * 1024);
    assertThat(stompHandler.getErrorHandler()).isNotNull();
    assertThat(stompHandler.getErrorHandler().getClass()).isEqualTo(TestStompErrorHandler.class);
    assertThat(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher")).isNotNull();
    httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
    assertThat(httpRequestHandler).isNotNull();
    assertThat(httpRequestHandler).isInstanceOf(SockJsHttpRequestHandler.class);
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertThat(wsHandler).isNotNull();
    assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
    assertThat(sockJsHttpRequestHandler.getSockJsService()).isNotNull();
    assertThat(sockJsHttpRequestHandler.getSockJsService()).isInstanceOf(DefaultSockJsService.class);
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsHttpRequestHandler.getSockJsService();
    WebSocketTransportHandler wsTransportHandler = (WebSocketTransportHandler) defaultSockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
    assertThat(wsTransportHandler.getHandshakeHandler()).isNotNull();
    assertThat(wsTransportHandler.getHandshakeHandler()).isInstanceOf(TestHandshakeHandler.class);
    assertThat(defaultSockJsService.shouldSuppressCors()).isFalse();
    ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) defaultSockJsService.getTaskScheduler();
    ScheduledThreadPoolExecutor executor = scheduler.getScheduledThreadPoolExecutor();
    assertThat(executor.getCorePoolSize()).isEqualTo(Runtime.getRuntime().availableProcessors());
    assertThat(executor.getRemoveOnCancelPolicy()).isTrue();
    interceptors = defaultSockJsService.getHandshakeInterceptors();
    assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
    assertThat(defaultSockJsService.getAllowedOrigins()).containsExactly("https://mydomain3.com", "https://mydomain4.com");
    assertThat(defaultSockJsService.getAllowedOriginPatterns()).containsExactly("https://*.mydomain.com");
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertThat(userRegistry).isNotNull();
    assertThat(userRegistry.getClass()).isEqualTo(DefaultSimpUserRegistry.class);
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertThat(userDestResolver).isNotNull();
    assertThat(userDestResolver).isInstanceOf(DefaultUserDestinationResolver.class);
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertThat(defaultUserDestResolver.getDestinationPrefix()).isEqualTo("/personal/");
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertThat(userDestHandler).isNotNull();
    SimpleBrokerMessageHandler brokerMessageHandler = this.appContext.getBean(SimpleBrokerMessageHandler.class);
    assertThat(brokerMessageHandler).isNotNull();
    Collection<String> prefixes = brokerMessageHandler.getDestinationPrefixes();
    assertThat(new ArrayList<>(prefixes)).isEqualTo(Arrays.asList("/topic", "/queue"));
    DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry) brokerMessageHandler.getSubscriptionRegistry();
    assertThat(registry.getSelectorHeaderName()).isEqualTo("my-selector");
    assertThat(brokerMessageHandler.getTaskScheduler()).isNotNull();
    assertThat(brokerMessageHandler.getHeartbeatValue()).isEqualTo(new long[] { 15000, 15000 });
    assertThat(brokerMessageHandler.isPreservePublishOrder()).isTrue();
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class);
    testChannel("clientInboundChannel", subscriberTypes, 2);
    testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
    testChannel("clientOutboundChannel", subscriberTypes, 2);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class));
    assertThat(this.appContext.getBean("webSocketScopeConfigurer", CustomScopeConfigurer.class)).isNotNull();
    DirectFieldAccessor accessor = new DirectFieldAccessor(registry);
    Object pathMatcher = accessor.getPropertyValue("pathMatcher");
    String pathSeparator = (String) new DirectFieldAccessor(pathMatcher).getPropertyValue("pathSeparator");
    assertThat(pathSeparator).isEqualTo(".");
}
Also used : StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) ArrayList(java.util.ArrayList) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) DefaultSimpUserRegistry(org.springframework.web.socket.messaging.DefaultSimpUserRegistry) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) WebSocketSession(org.springframework.web.socket.WebSocketSession) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSubscriptionRegistry(org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) UserDestinationResolver(org.springframework.messaging.simp.user.UserDestinationResolver) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.jupiter.api.Test)

Aggregations

WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)7 Test (org.junit.jupiter.api.Test)5 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)4 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)4 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)3 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)3 TransportType (org.springframework.web.socket.sockjs.transport.TransportType)3 List (java.util.List)2 Map (java.util.Map)2 MultiValueMap (org.springframework.util.MultiValueMap)2 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)2 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)2 ArrayList (java.util.ArrayList)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1