org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService Java Examples

The following examples show how to use org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultSockJsServiceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #2
Source File: DefaultSockJsServiceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #3
Source File: DefaultSockJsServiceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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("https://mydomain1.com");
	OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
	wsService.setHandshakeInterceptors(Collections.singletonList(interceptor));
	setRequest("GET", sockJsPrefix + sockJsPath);
	this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://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, "https://mydomain2.com");
	wsService.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
	assertEquals(403, this.servletResponse.getStatus());
}
 
Example #4
Source File: DefaultSockJsServiceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: DefaultSockJsServiceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example #6
Source File: DefaultSockJsServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #7
Source File: HandlersBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #8
Source File: DefaultSockJsServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #9
Source File: SockJsServiceRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private TransportHandlingSockJsService createSockJsService() {
	if (!this.transportHandlers.isEmpty()) {
		Assert.state(this.transportHandlerOverrides.isEmpty(),
				"Specify either TransportHandlers or TransportHandler overrides, not both");
		return new TransportHandlingSockJsService(this.taskScheduler, this.transportHandlers);
	}
	else {
		return new DefaultSockJsService(this.taskScheduler, this.transportHandlerOverrides);
	}
}
 
Example #10
Source File: SockJsServiceRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #11
Source File: HandlersBeanDefinitionParserTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}
 
Example #12
Source File: SockJsServiceRegistration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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.suppressCors != null) {
		service.setSuppressCors(this.suppressCors);
	}
	service.setAllowedOrigins(this.allowedOrigins);

	if (this.messageCodec != null) {
		service.setMessageCodec(this.messageCodec);
	}
	return service;
}
 
Example #13
Source File: DefaultSockJsServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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.wsTransportHandler.getTransportType()).willReturn(TransportType.WEBSOCKET);

	this.service = new TransportHandlingSockJsService(this.taskScheduler, this.xhrHandler, this.xhrSendHandler);
}
 
Example #14
Source File: SockJsServiceRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
private TransportHandlingSockJsService createSockJsService() {
	Assert.state(this.scheduler != null, "No TaskScheduler available");
	Assert.state(this.transportHandlers.isEmpty() || this.transportHandlerOverrides.isEmpty(),
			"Specify either TransportHandlers or TransportHandler overrides, not both");
	return (!this.transportHandlers.isEmpty() ?
			new TransportHandlingSockJsService(this.scheduler, this.transportHandlers) :
			new DefaultSockJsService(this.scheduler, this.transportHandlerOverrides));
}
 
Example #15
Source File: SockJsServiceRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
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.suppressCors != null) {
		service.setSuppressCors(this.suppressCors);
	}
	service.setAllowedOrigins(this.allowedOrigins);

	if (this.messageCodec != null) {
		service.setMessageCodec(this.messageCodec);
	}
	return service;
}
 
Example #16
Source File: HandlersBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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("https://mydomain1.com"));
	assertTrue(transportService.getAllowedOrigins().contains("https://mydomain2.com"));
}
 
Example #17
Source File: DefaultSockJsServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example #18
Source File: DefaultSockJsServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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.wsTransportHandler.getTransportType()).willReturn(TransportType.WEBSOCKET);

	this.service = new TransportHandlingSockJsService(this.taskScheduler, this.xhrHandler, this.xhrSendHandler);
}
 
Example #19
Source File: SockJsServiceRegistration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private TransportHandlingSockJsService createSockJsService() {
	Assert.state(this.scheduler != null, "No TaskScheduler available");
	Assert.state(this.transportHandlers.isEmpty() || this.transportHandlerOverrides.isEmpty(),
			"Specify either TransportHandlers or TransportHandler overrides, not both");
	return (!this.transportHandlers.isEmpty() ?
			new TransportHandlingSockJsService(this.scheduler, this.transportHandlers) :
			new DefaultSockJsService(this.scheduler, this.transportHandlerOverrides));
}