Search in sources :

Example 1 with SockJsService

use of org.springframework.web.socket.sockjs.SockJsService 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 2 with SockJsService

use of org.springframework.web.socket.sockjs.SockJsService 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 3 with SockJsService

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

the class WebMvcStompWebSocketEndpointRegistration method getMappings.

public final MultiValueMap<HttpRequestHandler, String> getMappings() {
    MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<>();
    if (this.registration != null) {
        SockJsService sockJsService = this.registration.getSockJsService();
        for (String path : this.paths) {
            String pattern = path.endsWith("/") ? path + "**" : path + "/**";
            SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler);
            mappings.add(handler, pattern);
        }
    } else {
        for (String path : this.paths) {
            WebSocketHttpRequestHandler handler;
            if (this.handshakeHandler != null) {
                handler = new WebSocketHttpRequestHandler(this.webSocketHandler, this.handshakeHandler);
            } else {
                handler = new WebSocketHttpRequestHandler(this.webSocketHandler);
            }
            HandshakeInterceptor[] interceptors = getInterceptors();
            if (interceptors.length > 0) {
                handler.setHandshakeInterceptors(Arrays.asList(interceptors));
            }
            mappings.add(handler, path);
        }
    }
    return mappings;
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) SockJsService(org.springframework.web.socket.sockjs.SockJsService) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)

Example 4 with SockJsService

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

the class TestMessageCodec method sockJsAttributes.

@Test
@SuppressWarnings("unchecked")
public void sockJsAttributes() {
    loadBeanDefinitions("websocket-config-handlers-sockjs-attributes.xml");
    SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(handlerMapping);
    SockJsHttpRequestHandler handler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertNotNull(handler);
    unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService sockJsService = handler.getSockJsService();
    assertNotNull(sockJsService);
    assertThat(sockJsService, instanceOf(TransportHandlingSockJsService.class));
    TransportHandlingSockJsService transportService = (TransportHandlingSockJsService) sockJsService;
    assertThat(transportService.getTaskScheduler(), instanceOf(TestTaskScheduler.class));
    assertThat(transportService.getTransportHandlers().values(), containsInAnyOrder(instanceOf(XhrPollingTransportHandler.class), instanceOf(XhrStreamingTransportHandler.class)));
    assertEquals("testSockJsService", transportService.getName());
    assertFalse(transportService.isWebSocketEnabled());
    assertFalse(transportService.isSessionCookieNeeded());
    assertEquals(2048, transportService.getStreamBytesLimit());
    assertEquals(256, transportService.getDisconnectDelay());
    assertEquals(1024, transportService.getHttpMessageCacheSize());
    assertEquals(20, transportService.getHeartbeatTime());
    assertEquals("/js/sockjs.min.js", transportService.getSockJsClientLibraryUrl());
    assertEquals(TestMessageCodec.class, transportService.getMessageCodec().getClass());
    List<HandshakeInterceptor> interceptors = transportService.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(OriginHandshakeInterceptor.class)));
    assertTrue(transportService.shouldSuppressCors());
    assertTrue(transportService.getAllowedOrigins().contains("http://mydomain1.com"));
    assertTrue(transportService.getAllowedOrigins().contains("http://mydomain2.com"));
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 5 with SockJsService

use of org.springframework.web.socket.sockjs.SockJsService in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurer method afterSingletonsInstantiated.

public void afterSingletonsInstantiated() {
    if (sameOriginDisabled()) {
        return;
    }
    String beanName = "stompWebSocketHandlerMapping";
    SimpleUrlHandlerMapping mapping = context.getBean(beanName, SimpleUrlHandlerMapping.class);
    Map<String, Object> mappings = mapping.getHandlerMap();
    for (Object object : mappings.values()) {
        if (object instanceof SockJsHttpRequestHandler) {
            SockJsHttpRequestHandler sockjsHandler = (SockJsHttpRequestHandler) object;
            SockJsService sockJsService = sockjsHandler.getSockJsService();
            if (!(sockJsService instanceof TransportHandlingSockJsService)) {
                throw new IllegalStateException("sockJsService must be instance of TransportHandlingSockJsService got " + sockJsService);
            }
            TransportHandlingSockJsService transportHandlingSockJsService = (TransportHandlingSockJsService) sockJsService;
            List<HandshakeInterceptor> handshakeInterceptors = transportHandlingSockJsService.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            transportHandlingSockJsService.setHandshakeInterceptors(interceptorsToSet);
        } else if (object instanceof WebSocketHttpRequestHandler) {
            WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
            List<HandshakeInterceptor> handshakeInterceptors = handler.getHandshakeInterceptors();
            List<HandshakeInterceptor> interceptorsToSet = new ArrayList<HandshakeInterceptor>(handshakeInterceptors.size() + 1);
            interceptorsToSet.add(new CsrfTokenHandshakeInterceptor());
            interceptorsToSet.addAll(handshakeInterceptors);
            handler.setHandshakeInterceptors(interceptorsToSet);
        } else {
            throw new IllegalStateException("Bean " + beanName + " is expected to contain mappings to either a SockJsHttpRequestHandler or a WebSocketHttpRequestHandler but got " + object);
        }
    }
    if (inboundRegistry.containsMapping() && !inboundRegistry.isSimpDestPathMatcherConfigured()) {
        PathMatcher pathMatcher = getDefaultPathMatcher();
        inboundRegistry.simpDestPathMatcher(pathMatcher);
    }
}
Also used : CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) ArrayList(java.util.ArrayList) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) CsrfTokenHandshakeInterceptor(org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) AntPathMatcher(org.springframework.util.AntPathMatcher) PathMatcher(org.springframework.util.PathMatcher) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) SockJsService(org.springframework.web.socket.sockjs.SockJsService) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)5 SockJsService (org.springframework.web.socket.sockjs.SockJsService)5 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)3 Test (org.junit.Test)2 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)2 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1 CsrfTokenHandshakeInterceptor (org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor)1 AntPathMatcher (org.springframework.util.AntPathMatcher)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 PathMatcher (org.springframework.util.PathMatcher)1 HttpRequestHandler (org.springframework.web.HttpRequestHandler)1 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)1 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)1 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)1