Search in sources :

Example 1 with TransportHandler

use of org.springframework.web.socket.sockjs.transport.TransportHandler in project spring-integration by spring-projects.

the class WebSocketParserTests method testDefaultInboundChannelAdapterAndServerContainer.

@Test
@SuppressWarnings("unckecked")
public void testDefaultInboundChannelAdapterAndServerContainer() {
    Map<?, ?> urlMap = TestUtils.getPropertyValue(this.handlerMapping, "urlMap", Map.class);
    assertEquals(1, urlMap.size());
    assertTrue(urlMap.containsKey("/ws/**"));
    Object mappedHandler = urlMap.get("/ws/**");
    // WebSocketHttpRequestHandler -> ExceptionWebSocketHandlerDecorator - > LoggingWebSocketHandlerDecorator
    // -> IntegrationWebSocketContainer$IntegrationWebSocketHandler
    assertSame(TestUtils.getPropertyValue(this.serverWebSocketContainer, "webSocketHandler"), TestUtils.getPropertyValue(mappedHandler, "webSocketHandler.delegate.delegate"));
    assertSame(this.handshakeHandler, TestUtils.getPropertyValue(this.serverWebSocketContainer, "handshakeHandler"));
    HandshakeInterceptor[] interceptors = TestUtils.getPropertyValue(this.serverWebSocketContainer, "interceptors", HandshakeInterceptor[].class);
    assertNotNull(interceptors);
    assertEquals(1, interceptors.length);
    assertSame(this.handshakeInterceptor, interceptors[0]);
    assertEquals(100, TestUtils.getPropertyValue(this.serverWebSocketContainer, "sendTimeLimit"));
    assertEquals(100000, TestUtils.getPropertyValue(this.serverWebSocketContainer, "sendBufferSizeLimit"));
    assertArrayEquals(new String[] { "http://foo.com" }, TestUtils.getPropertyValue(this.serverWebSocketContainer, "origins", String[].class));
    WebSocketHandlerDecoratorFactory[] decoratorFactories = TestUtils.getPropertyValue(this.serverWebSocketContainer, "decoratorFactories", WebSocketHandlerDecoratorFactory[].class);
    assertNotNull(decoratorFactories);
    assertEquals(1, decoratorFactories.length);
    assertSame(this.decoratorFactory, decoratorFactories[0]);
    TransportHandlingSockJsService sockJsService = TestUtils.getPropertyValue(mappedHandler, "sockJsService", TransportHandlingSockJsService.class);
    assertSame(this.taskScheduler, sockJsService.getTaskScheduler());
    assertSame(this.sockJsMessageCodec, sockJsService.getMessageCodec());
    Map<TransportType, TransportHandler> transportHandlers = sockJsService.getTransportHandlers();
    // If "handshake-handler" is provided, "transport-handlers" isn't allowed
    assertEquals(8, transportHandlers.size());
    assertSame(this.handshakeHandler, TestUtils.getPropertyValue(transportHandlers.get(TransportType.WEBSOCKET), "handshakeHandler"));
    assertEquals(4000L, sockJsService.getDisconnectDelay());
    assertEquals(30000L, sockJsService.getHeartbeatTime());
    assertEquals(10000, sockJsService.getHttpMessageCacheSize());
    assertEquals(2000, sockJsService.getStreamBytesLimit());
    assertEquals("https://foo.sock.js", sockJsService.getSockJsClientLibraryUrl());
    assertFalse(sockJsService.isSessionCookieNeeded());
    assertFalse(sockJsService.isWebSocketEnabled());
    assertTrue(sockJsService.shouldSuppressCors());
    assertSame(this.serverWebSocketContainer, TestUtils.getPropertyValue(this.defaultInboundAdapter, "webSocketContainer"));
    assertNull(TestUtils.getPropertyValue(this.defaultInboundAdapter, "messageConverters"));
    assertEquals(TestUtils.getPropertyValue(this.defaultInboundAdapter, "messageConverter.converters"), TestUtils.getPropertyValue(this.defaultInboundAdapter, "defaultConverters"));
    assertEquals(String.class, TestUtils.getPropertyValue(this.defaultInboundAdapter, "payloadType", AtomicReference.class).get());
    assertTrue(TestUtils.getPropertyValue(this.defaultInboundAdapter, "useBroker", Boolean.class));
    assertSame(this.brokerHandler, TestUtils.getPropertyValue(this.defaultInboundAdapter, "brokerHandler"));
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = TestUtils.getPropertyValue(this.defaultInboundAdapter, "subProtocolHandlerRegistry", SubProtocolHandlerRegistry.class);
    assertThat(TestUtils.getPropertyValue(subProtocolHandlerRegistry, "defaultProtocolHandler"), instanceOf(PassThruSubProtocolHandler.class));
    assertTrue(TestUtils.getPropertyValue(subProtocolHandlerRegistry, "protocolHandlers", Map.class).isEmpty());
}
Also used : TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SubProtocolHandlerRegistry(org.springframework.integration.websocket.support.SubProtocolHandlerRegistry) WebSocketHandlerDecoratorFactory(org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) PassThruSubProtocolHandler(org.springframework.integration.websocket.support.PassThruSubProtocolHandler) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) Test(org.junit.Test)

Example 2 with TransportHandler

use of org.springframework.web.socket.sockjs.transport.TransportHandler 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 TransportHandler

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

the class DefaultSockJsServiceTests method customizedTransportHandlerList.

@Test
public void customizedTransportHandlerList() {
    TransportHandlingSockJsService service = new TransportHandlingSockJsService(mock(TaskScheduler.class), new XhrPollingTransportHandler(), new XhrReceivingTransportHandler());
    Map<TransportType, TransportHandler> actualHandlers = service.getTransportHandlers();
    assertThat(actualHandlers.size()).isEqualTo(2);
}
Also used : TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.jupiter.api.Test)

Example 4 with TransportHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins.

@Test
public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    String origin = "https://mydomain.com";
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo/**"));
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService).isNotNull();
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(sockJsService.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(sockJsService.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(sockJsService.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
    assertThat(sockJsService.getAllowedOrigins().contains(origin)).isTrue();
}
Also used : WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) List(java.util.List) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with TransportHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerInterceptorWithSockJsService.

@Test
public void handshakeHandlerInterceptorWithSockJsService() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).withSockJS();
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo/**"));
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService).isNotNull();
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertThat(transportHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(sockJsService.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(sockJsService.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(sockJsService.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
}
Also used : WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) List(java.util.List) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)5 TransportType (org.springframework.web.socket.sockjs.transport.TransportType)5 Test (org.junit.jupiter.api.Test)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)3 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)3 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)3 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)3 List (java.util.List)2 Map (java.util.Map)2 MultiValueMap (org.springframework.util.MultiValueMap)2 HttpRequestHandler (org.springframework.web.HttpRequestHandler)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)2 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 Test (org.junit.Test)1 PassThruSubProtocolHandler (org.springframework.integration.websocket.support.PassThruSubProtocolHandler)1 SubProtocolHandlerRegistry (org.springframework.integration.websocket.support.SubProtocolHandlerRegistry)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)1