Search in sources :

Example 1 with TransportType

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

the class TestMessageCodec method sockJs.

@Test
@SuppressWarnings("unchecked")
public void sockJs() {
    loadBeanDefinitions("websocket-config-handlers-sockjs.xml");
    SimpleUrlHandlerMapping handlerMapping = this.appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(handlerMapping);
    SockJsHttpRequestHandler testHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertNotNull(testHandler);
    unwrapAndCheckDecoratedHandlerType(testHandler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService testSockJsService = testHandler.getSockJsService();
    SockJsHttpRequestHandler fooHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/foo/**");
    assertNotNull(fooHandler);
    unwrapAndCheckDecoratedHandlerType(fooHandler.getWebSocketHandler(), FooWebSocketHandler.class);
    SockJsService sockJsService = fooHandler.getSockJsService();
    assertNotNull(sockJsService);
    assertSame(testSockJsService, sockJsService);
    assertThat(sockJsService, instanceOf(DefaultSockJsService.class));
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsService;
    assertThat(defaultSockJsService.getTaskScheduler(), instanceOf(ThreadPoolTaskScheduler.class));
    assertFalse(defaultSockJsService.shouldSuppressCors());
    Map<TransportType, TransportHandler> transportHandlers = defaultSockJsService.getTransportHandlers();
    assertThat(transportHandlers.values(), containsInAnyOrder(instanceOf(XhrPollingTransportHandler.class), instanceOf(XhrReceivingTransportHandler.class), instanceOf(JsonpPollingTransportHandler.class), instanceOf(JsonpReceivingTransportHandler.class), instanceOf(XhrStreamingTransportHandler.class), instanceOf(EventSourceTransportHandler.class), instanceOf(HtmlFileTransportHandler.class), instanceOf(WebSocketTransportHandler.class)));
    WebSocketTransportHandler handler = (WebSocketTransportHandler) transportHandlers.get(TransportType.WEBSOCKET);
    assertEquals(TestHandshakeHandler.class, handler.getHandshakeHandler().getClass());
    List<HandshakeInterceptor> interceptors = defaultSockJsService.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
}
Also used : JsonpReceivingTransportHandler(org.springframework.web.socket.sockjs.transport.handler.JsonpReceivingTransportHandler) 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) JsonpPollingTransportHandler(org.springframework.web.socket.sockjs.transport.handler.JsonpPollingTransportHandler) 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) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) Test(org.junit.Test)

Example 2 with TransportType

use of org.springframework.web.socket.sockjs.transport.TransportType 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();
    assertEquals(2, actualHandlers.size());
}
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.Test)

Example 3 with TransportType

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

the class SockJsClient method createRequest.

private DefaultTransportRequest createRequest(SockJsUrlInfo urlInfo, HttpHeaders headers, ServerInfo serverInfo) {
    List<DefaultTransportRequest> requests = new ArrayList<>(this.transports.size());
    for (Transport transport : this.transports) {
        for (TransportType type : transport.getTransportTypes()) {
            if (serverInfo.isWebSocketEnabled() || !TransportType.WEBSOCKET.equals(type)) {
                requests.add(new DefaultTransportRequest(urlInfo, headers, getHttpRequestHeaders(headers), transport, type, getMessageCodec()));
            }
        }
    }
    if (CollectionUtils.isEmpty(requests)) {
        throw new IllegalStateException("No transports: " + urlInfo + ", webSocketEnabled=" + serverInfo.isWebSocketEnabled());
    }
    for (int i = 0; i < requests.size() - 1; i++) {
        DefaultTransportRequest request = requests.get(i);
        request.setUser(getUser());
        if (this.connectTimeoutScheduler != null) {
            request.setTimeoutValue(serverInfo.getRetransmissionTimeout());
            request.setTimeoutScheduler(this.connectTimeoutScheduler);
        }
        request.setFallbackRequest(requests.get(i + 1));
    }
    return requests.get(0);
}
Also used : ArrayList(java.util.ArrayList) TransportType(org.springframework.web.socket.sockjs.transport.TransportType)

Example 4 with TransportType

use of org.springframework.web.socket.sockjs.transport.TransportType 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 = "http://mydomain.com";
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertEquals(1, mappings.size());
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertEquals(Arrays.asList("/foo/**"), entry.getValue());
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertNotNull(requestHandler.getWebSocketHandler());
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertNotNull(sockJsService);
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
    assertEquals(2, sockJsService.getHandshakeInterceptors().size());
    assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
    assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
    assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
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.Test)

Example 5 with TransportType

use of org.springframework.web.socket.sockjs.transport.TransportType 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();
    assertEquals(1, mappings.size());
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertEquals(Arrays.asList("/foo/**"), entry.getValue());
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertNotNull(requestHandler.getWebSocketHandler());
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertNotNull(sockJsService);
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
    assertEquals(2, sockJsService.getHandshakeInterceptors().size());
    assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
    assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
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.Test)

Aggregations

TransportType (org.springframework.web.socket.sockjs.transport.TransportType)5 Test (org.junit.Test)4 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)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 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 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)2 ArrayList (java.util.ArrayList)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)1 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)1 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)1