Java Code Examples for org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler#getSockJsService()

The following examples show how to use org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler#getSockJsService() . 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: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-12283
public void disableCorsWithSockJsService() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.withSockJS().setSupressCors(true);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getSockJsService());
	DefaultSockJsService sockJsService = (DefaultSockJsService)requestHandler.getSockJsService();
	assertTrue(sockJsService.shouldSuppressCors());
}
 
Example 2
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
 
Example 3
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class,
			sockJsService.getHandshakeInterceptors().get(1).getClass());
	assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
 
Example 4
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 5
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-12283
public void disableCorsWithSockJsService() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.withSockJS().setSupressCors(true);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getSockJsService());
	DefaultSockJsService sockJsService = (DefaultSockJsService)requestHandler.getSockJsService();
	assertTrue(sockJsService.shouldSuppressCors());
}
 
Example 6
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
 
Example 7
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 = "http://mydomain.com";

	registration.setHandshakeHandler(handshakeHandler)
			.addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class,
			sockJsService.getHandshakeInterceptors().get(1).getClass());
	assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
 
Example 8
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 9
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test  // SPR-12283
public void disableCorsWithSockJsService() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.withSockJS().setSupressCors(true);

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getSockJsService());
	DefaultSockJsService sockJsService = (DefaultSockJsService)requestHandler.getSockJsService();
	assertTrue(sockJsService.shouldSuppressCors());
}
 
Example 10
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
 
Example 11
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 = "http://mydomain.com";

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertEquals(Arrays.asList("/foo/**"), entry.getValue());

	SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());

	DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
	assertNotNull(sockJsService);

	Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
	WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
	assertEquals(2, sockJsService.getHandshakeInterceptors().size());
	assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class,
			sockJsService.getHandshakeInterceptors().get(1).getClass());
	assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
 
Example 12
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 13
Source File: HandlersBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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> handlerMap = defaultSockJsService.getTransportHandlers();
	assertThat(handlerMap.values(),
			containsInAnyOrder(
					instanceOf(XhrPollingTransportHandler.class),
					instanceOf(XhrReceivingTransportHandler.class),
					instanceOf(XhrStreamingTransportHandler.class),
					instanceOf(EventSourceTransportHandler.class),
					instanceOf(HtmlFileTransportHandler.class),
					instanceOf(WebSocketTransportHandler.class)));

	WebSocketTransportHandler handler = (WebSocketTransportHandler) handlerMap.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)));
}
 
Example 14
Source File: HandlersBeanDefinitionParserTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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> handlerMap = defaultSockJsService.getTransportHandlers();
	assertThat(handlerMap.values(),
			containsInAnyOrder(
					instanceOf(XhrPollingTransportHandler.class),
					instanceOf(XhrReceivingTransportHandler.class),
					instanceOf(XhrStreamingTransportHandler.class),
					instanceOf(EventSourceTransportHandler.class),
					instanceOf(HtmlFileTransportHandler.class),
					instanceOf(WebSocketTransportHandler.class)));

	WebSocketTransportHandler handler = (WebSocketTransportHandler) handlerMap.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)));
}
 
Example 15
Source File: HandlersBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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)));
}