Search in sources :

Example 1 with HandshakeInterceptor

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

the class AbstractWebSocketHandlerRegistration method getInterceptors.

protected HandshakeInterceptor[] getInterceptors() {
    List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
    interceptors.addAll(this.interceptors);
    interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
    return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]);
}
Also used : OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 2 with HandshakeInterceptor

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

the class AbstractWebSocketHandlerRegistration method getMappings.

protected final M getMappings() {
    M mappings = createMappings();
    if (this.sockJsServiceRegistration != null) {
        SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
        for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
            for (String path : this.handlerMap.get(wsHandler)) {
                String pathPattern = (path.endsWith("/") ? path + "**" : path + "/**");
                addSockJsServiceMapping(mappings, sockJsService, wsHandler, pathPattern);
            }
        }
    } else {
        HandshakeHandler handshakeHandler = getOrCreateHandshakeHandler();
        HandshakeInterceptor[] interceptors = getInterceptors();
        for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
            for (String path : this.handlerMap.get(wsHandler)) {
                addWebSocketHandlerMapping(mappings, wsHandler, handshakeHandler, interceptors, path);
            }
        }
    }
    return mappings;
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) SockJsService(org.springframework.web.socket.sockjs.SockJsService) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor)

Example 3 with HandshakeInterceptor

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

the class TestMessageCodec method webSocketHandlersAttributes.

@Test
@SuppressWarnings("unchecked")
public void webSocketHandlersAttributes() {
    loadBeanDefinitions("websocket-config-handlers-attributes.xml");
    HandlerMapping handlerMapping = this.appContext.getBean(HandlerMapping.class);
    assertNotNull(handlerMapping);
    assertTrue(handlerMapping instanceof SimpleUrlHandlerMapping);
    SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
    assertEquals(2, urlHandlerMapping.getOrder());
    WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/foo");
    assertNotNull(handler);
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
    HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    List<HandshakeInterceptor> interceptors = handler.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
    handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/test");
    assertNotNull(handler);
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    handshakeHandler = handler.getHandshakeHandler();
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    interceptors = handler.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
}
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) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.Test)

Example 4 with HandshakeInterceptor

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

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

the class WebSocketHandlerRegistrationTests method interceptorsPassedToSockJsRegistration.

@Test
public void interceptorsPassedToSockJsRegistration() {
    WebSocketHandler handler = new TextWebSocketHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com").withSockJS();
    List<Mapping> mappings = this.registration.getMappings();
    assertEquals(1, mappings.size());
    Mapping mapping = mappings.get(0);
    assertEquals(handler, mapping.webSocketHandler);
    assertEquals("/foo/**", mapping.path);
    assertNotNull(mapping.sockJsService);
    assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
    List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
    assertEquals(interceptor, interceptors.get(0));
    assertEquals(OriginHandshakeInterceptor.class, interceptors.get(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) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) Test(org.junit.Test)

Aggregations

HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)13 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)11 Test (org.junit.Test)5 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 SockJsService (org.springframework.web.socket.sockjs.SockJsService)5 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)4 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)4 ArrayList (java.util.ArrayList)3 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)3 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)3 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)3 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 HttpRequestHandler (org.springframework.web.HttpRequestHandler)2 HandlerMapping (org.springframework.web.servlet.HandlerMapping)2 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)2 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)2 List (java.util.List)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1