Search in sources :

Example 6 with HttpSessionHandshakeInterceptor

use of org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor 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 7 with HttpSessionHandshakeInterceptor

use of org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor 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)

Example 8 with HttpSessionHandshakeInterceptor

use of org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor in project spring-framework by spring-projects.

the class WebSocketHandlerRegistrationTests method interceptorsWithAllowedOrigins.

@Test
public void interceptorsWithAllowedOrigins() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(1, mappings.size());
    Mapping mapping = mappings.get(0);
    assertEquals(handler, mapping.webSocketHandler);
    assertEquals("/foo", mapping.path);
    assertEquals(2, mapping.interceptors.length);
    assertEquals(interceptor, mapping.interceptors[0]);
    assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)8 List (java.util.List)4 Map (java.util.Map)4 MultiValueMap (org.springframework.util.MultiValueMap)4 HttpRequestHandler (org.springframework.web.HttpRequestHandler)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)4 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)4 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)2 TransportType (org.springframework.web.socket.sockjs.transport.TransportType)2 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)2 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)1 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)1