Search in sources :

Example 6 with SockJsHttpRequestHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method disableCorsWithSockJsService.

// SPR-12283
@Test
public void disableCorsWithSockJsService() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    registration.withSockJS().setSupressCors(true);
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) mappings.entrySet().iterator().next().getKey();
    assertThat(requestHandler.getSockJsService()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService.shouldSuppressCors()).isTrue();
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) Test(org.junit.jupiter.api.Test)

Example 7 with SockJsHttpRequestHandler

use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler 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 8 with SockJsHttpRequestHandler

use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler 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 9 with SockJsHttpRequestHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method allowedOriginsWithSockJsService.

@Test
public void allowedOriginsWithSockJsService() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    String origin = "https://mydomain.com";
    registration.setAllowedOrigins(origin).withSockJS();
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) mappings.entrySet().iterator().next().getKey();
    assertThat(requestHandler.getSockJsService()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService.getAllowedOrigins().contains(origin)).isTrue();
    assertThat(sockJsService.shouldSuppressCors()).isFalse();
    registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    registration.withSockJS().setAllowedOrigins(origin);
    mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    requestHandler = (SockJsHttpRequestHandler) mappings.entrySet().iterator().next().getKey();
    assertThat(requestHandler.getSockJsService()).isNotNull();
    sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService.getAllowedOrigins().contains(origin)).isTrue();
    assertThat(sockJsService.shouldSuppressCors()).isFalse();
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) Test(org.junit.jupiter.api.Test)

Example 10 with SockJsHttpRequestHandler

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

the class WebMvcStompWebSocketEndpointRegistrationTests method allowedOriginPatterns.

@Test
public void allowedOriginPatterns() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    String origin = "https://*.mydomain.com";
    registration.setAllowedOriginPatterns(origin).withSockJS();
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) mappings.entrySet().iterator().next().getKey();
    assertThat(requestHandler.getSockJsService()).isNotNull();
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService.getAllowedOriginPatterns().contains(origin)).isTrue();
    registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    registration.withSockJS().setAllowedOriginPatterns(origin);
    mappings = registration.getMappings();
    assertThat(mappings.size()).isEqualTo(1);
    requestHandler = (SockJsHttpRequestHandler) mappings.entrySet().iterator().next().getKey();
    assertThat(requestHandler.getSockJsService()).isNotNull();
    sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertThat(sockJsService.getAllowedOriginPatterns().contains(origin)).isTrue();
}
Also used : SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) Test(org.junit.jupiter.api.Test)

Aggregations

SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)12 Test (org.junit.jupiter.api.Test)9 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)9 HttpRequestHandler (org.springframework.web.HttpRequestHandler)8 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)8 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)4 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)4 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)4 SockJsService (org.springframework.web.socket.sockjs.SockJsService)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 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)2 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)2