Search in sources :

Example 1 with TransportHandlingSockJsService

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

the class SockJsServiceRegistration method getSockJsService.

protected SockJsService getSockJsService() {
    TransportHandlingSockJsService service = createSockJsService();
    service.setHandshakeInterceptors(this.interceptors);
    if (this.clientLibraryUrl != null) {
        service.setSockJsClientLibraryUrl(this.clientLibraryUrl);
    }
    if (this.streamBytesLimit != null) {
        service.setStreamBytesLimit(this.streamBytesLimit);
    }
    if (this.sessionCookieNeeded != null) {
        service.setSessionCookieNeeded(this.sessionCookieNeeded);
    }
    if (this.heartbeatTime != null) {
        service.setHeartbeatTime(this.heartbeatTime);
    }
    if (this.disconnectDelay != null) {
        service.setDisconnectDelay(this.disconnectDelay);
    }
    if (this.httpMessageCacheSize != null) {
        service.setHttpMessageCacheSize(this.httpMessageCacheSize);
    }
    if (this.webSocketEnabled != null) {
        service.setWebSocketEnabled(this.webSocketEnabled);
    }
    if (this.allowedOrigins != null) {
        service.setAllowedOrigins(this.allowedOrigins);
    }
    if (this.suppressCors != null) {
        service.setSuppressCors(this.suppressCors);
    }
    if (this.messageCodec != null) {
        service.setMessageCodec(this.messageCodec);
    }
    return service;
}
Also used : TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)

Example 2 with TransportHandlingSockJsService

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

the class DefaultSockJsServiceTests method customizedTransportHandlerList.

@Test
public void customizedTransportHandlerList() {
    TransportHandlingSockJsService service = new TransportHandlingSockJsService(mock(TaskScheduler.class), new XhrPollingTransportHandler(), new XhrReceivingTransportHandler());
    Map<TransportType, TransportHandler> actualHandlers = service.getTransportHandlers();
    assertEquals(2, actualHandlers.size());
}
Also used : TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test)

Example 3 with TransportHandlingSockJsService

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

the class DefaultSockJsServiceTests method handleTransportRequestJsonp.

@Test
public void handleTransportRequestJsonp() throws Exception {
    TransportHandlingSockJsService jsonpService = new TransportHandlingSockJsService(this.taskScheduler, this.jsonpHandler, this.jsonpSendHandler);
    String sockJsPath = sessionUrlPrefix + "jsonp";
    setRequest("GET", sockJsPrefix + sockJsPath);
    jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertEquals(404, this.servletResponse.getStatus());
    resetRequestAndResponse();
    jsonpService.setAllowedOrigins(Collections.singletonList("http://mydomain1.com"));
    setRequest("GET", sockJsPrefix + sockJsPath);
    jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertEquals(404, this.servletResponse.getStatus());
    resetRequestAndResponse();
    jsonpService.setAllowedOrigins(Collections.singletonList("*"));
    setRequest("GET", sockJsPrefix + sockJsPath);
    jsonpService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertNotEquals(404, this.servletResponse.getStatus());
}
Also used : TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) Test(org.junit.Test)

Example 4 with TransportHandlingSockJsService

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

the class DefaultSockJsServiceTests method setup.

@Before
public void setup() {
    super.setUp();
    MockitoAnnotations.initMocks(this);
    Map<String, Object> attributes = Collections.emptyMap();
    this.session = new TestSockJsSession(sessionId, new StubSockJsServiceConfig(), this.wsHandler, attributes);
    given(this.xhrHandler.getTransportType()).willReturn(TransportType.XHR);
    given(this.xhrHandler.createSession(sessionId, this.wsHandler, attributes)).willReturn(this.session);
    given(this.xhrSendHandler.getTransportType()).willReturn(TransportType.XHR_SEND);
    given(this.jsonpHandler.getTransportType()).willReturn(TransportType.JSONP);
    given(this.jsonpHandler.createSession(sessionId, this.wsHandler, attributes)).willReturn(this.session);
    given(this.jsonpSendHandler.getTransportType()).willReturn(TransportType.JSONP_SEND);
    given(this.wsTransportHandler.getTransportType()).willReturn(TransportType.WEBSOCKET);
    this.service = new TransportHandlingSockJsService(this.taskScheduler, this.xhrHandler, this.xhrSendHandler);
}
Also used : TestSockJsSession(org.springframework.web.socket.sockjs.transport.session.TestSockJsSession) TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) StubSockJsServiceConfig(org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig) Before(org.junit.Before)

Example 5 with TransportHandlingSockJsService

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

the class DefaultSockJsServiceTests method handleTransportRequestWebsocket.

@Test
public void handleTransportRequestWebsocket() throws Exception {
    TransportHandlingSockJsService wsService = new TransportHandlingSockJsService(this.taskScheduler, this.wsTransportHandler);
    String sockJsPath = "/websocket";
    setRequest("GET", sockJsPrefix + sockJsPath);
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertNotEquals(403, this.servletResponse.getStatus());
    resetRequestAndResponse();
    List<String> allowed = Collections.singletonList("http://mydomain1.com");
    OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
    wsService.setHandshakeInterceptors(Collections.singletonList(interceptor));
    setRequest("GET", sockJsPrefix + sockJsPath);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertNotEquals(403, this.servletResponse.getStatus());
    resetRequestAndResponse();
    setRequest("GET", sockJsPrefix + sockJsPath);
    this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
    wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
    assertEquals(403, this.servletResponse.getStatus());
}
Also used : TransportHandlingSockJsService(org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) Test(org.junit.Test)

Aggregations

TransportHandlingSockJsService (org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService)7 Test (org.junit.Test)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)2 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)2 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)2 SockJsService (org.springframework.web.socket.sockjs.SockJsService)2 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1 CsrfTokenHandshakeInterceptor (org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor)1 AntPathMatcher (org.springframework.util.AntPathMatcher)1 PathMatcher (org.springframework.util.PathMatcher)1 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)1 TransportHandler (org.springframework.web.socket.sockjs.transport.TransportHandler)1 TransportType (org.springframework.web.socket.sockjs.transport.TransportType)1 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)1 StubSockJsServiceConfig (org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig)1 TestSockJsSession (org.springframework.web.socket.sockjs.transport.session.TestSockJsSession)1