Search in sources :

Example 11 with DefaultHandshakeHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerAndInterceptor.

@Test
public void handshakeHandlerAndInterceptor() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);
    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"));
    WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
    assertThat(requestHandler.getWebSocketHandler()).isNotNull();
    assertThat(requestHandler.getHandshakeHandler()).isSameAs(handshakeHandler);
    assertThat(requestHandler.getHandshakeInterceptors().size()).isEqualTo(2);
    assertThat(requestHandler.getHandshakeInterceptors().get(0)).isEqualTo(interceptor);
    assertThat(requestHandler.getHandshakeInterceptors().get(1).getClass()).isEqualTo(OriginHandshakeInterceptor.class);
}
Also used : 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) List(java.util.List) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.jupiter.api.Test)

Example 12 with DefaultHandshakeHandler

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

Example 13 with DefaultHandshakeHandler

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

the class TestMessageCodec method webSocketHandlers.

@Test
public void webSocketHandlers() {
    loadBeanDefinitions("websocket-config-handlers.xml");
    Map<String, HandlerMapping> handlersMap = this.appContext.getBeansOfType(HandlerMapping.class);
    assertThat(handlersMap).isNotNull();
    assertThat(handlersMap).hasSize(2);
    for (HandlerMapping hm : handlersMap.values()) {
        boolean condition2 = hm instanceof SimpleUrlHandlerMapping;
        assertThat(condition2).isTrue();
        SimpleUrlHandlerMapping shm = (SimpleUrlHandlerMapping) hm;
        if (shm.getUrlMap().keySet().contains("/foo")) {
            assertThat(shm.getUrlMap()).containsOnlyKeys("/foo", "/bar");
            WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) shm.getUrlMap().get("/foo");
            assertThat(handler).isNotNull();
            unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
            HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
            assertThat(handshakeHandler).isNotNull();
            boolean condition1 = handshakeHandler instanceof DefaultHandshakeHandler;
            assertThat(condition1).isTrue();
            assertThat(handler.getHandshakeInterceptors().isEmpty()).isFalse();
            boolean condition = handler.getHandshakeInterceptors().get(0) instanceof OriginHandshakeInterceptor;
            assertThat(condition).isTrue();
        } else {
            assertThat(shm.getUrlMap()).containsOnlyKeys("/test");
            WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) shm.getUrlMap().get("/test");
            assertThat(handler).isNotNull();
            unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
            HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
            assertThat(handshakeHandler).isNotNull();
            boolean condition1 = handshakeHandler instanceof DefaultHandshakeHandler;
            assertThat(condition1).isTrue();
            assertThat(handler.getHandshakeInterceptors().isEmpty()).isFalse();
            boolean condition = handler.getHandshakeInterceptors().get(0) instanceof OriginHandshakeInterceptor;
            assertThat(condition).isTrue();
        }
    }
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)13 Test (org.junit.jupiter.api.Test)7 List (java.util.List)5 Map (java.util.Map)5 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)5 MultiValueMap (org.springframework.util.MultiValueMap)4 HttpRequestHandler (org.springframework.web.HttpRequestHandler)4 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 Principal (java.security.Principal)3 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)3 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)2 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)2