Search in sources :

Example 6 with WebSocketTransportHandler

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

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

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