org.springframework.web.socket.server.support.DefaultHandshakeHandler Java Examples

The following examples show how to use org.springframework.web.socket.server.support.DefaultHandshakeHandler. 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #2
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
	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);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #3
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #4
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #5
Source File: DefaultSockJsService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static Set<TransportHandler> getDefaultTransportHandlers(Collection<TransportHandler> overrides) {
	Set<TransportHandler> result = new LinkedHashSet<TransportHandler>(8);
	result.add(new XhrPollingTransportHandler());
	result.add(new XhrReceivingTransportHandler());
	result.add(new XhrStreamingTransportHandler());
	result.add(new JsonpPollingTransportHandler());
	result.add(new JsonpReceivingTransportHandler());
	result.add(new EventSourceTransportHandler());
	result.add(new HtmlFileTransportHandler());
	try {
		result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler()));
	}
	catch (Exception ex) {
		Log logger = LogFactory.getLog(DefaultSockJsService.class);
		if (logger.isWarnEnabled()) {
			logger.warn("Failed to create a default WebSocketTransportHandler", ex);
		}
	}
	if (overrides != null) {
		result.addAll(overrides);
	}
	return result;
}
 
Example #6
Source File: DefaultSockJsService.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static Set<TransportHandler> getDefaultTransportHandlers(@Nullable Collection<TransportHandler> overrides) {
	Set<TransportHandler> result = new LinkedHashSet<>(8);
	result.add(new XhrPollingTransportHandler());
	result.add(new XhrReceivingTransportHandler());
	result.add(new XhrStreamingTransportHandler());
	result.add(new EventSourceTransportHandler());
	result.add(new HtmlFileTransportHandler());
	try {
		result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler()));
	}
	catch (Exception ex) {
		Log logger = LogFactory.getLog(DefaultSockJsService.class);
		if (logger.isWarnEnabled()) {
			logger.warn("Failed to create a default WebSocketTransportHandler", ex);
		}
	}
	if (overrides != null) {
		result.addAll(overrides);
	}
	return result;
}
 
Example #7
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #8
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
	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);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #9
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
	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);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #10
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerAndInterceptor() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);

	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());

	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
	assertEquals(2, requestHandler.getHandshakeInterceptors().size());
	assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
 
Example #11
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();

	this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
	this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);

	List<Mapping> mappings = this.registration.getMappings();
	assertEquals(1, mappings.size());

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo/**", mapping.path);
	assertNotNull(mapping.sockJsService);

	WebSocketTransportHandler transportHandler =
			(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
	assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
 
Example #12
Source File: DefaultSockJsService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static Set<TransportHandler> getDefaultTransportHandlers(@Nullable Collection<TransportHandler> overrides) {
	Set<TransportHandler> result = new LinkedHashSet<>(8);
	result.add(new XhrPollingTransportHandler());
	result.add(new XhrReceivingTransportHandler());
	result.add(new XhrStreamingTransportHandler());
	result.add(new EventSourceTransportHandler());
	result.add(new HtmlFileTransportHandler());
	try {
		result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler()));
	}
	catch (Exception ex) {
		Log logger = LogFactory.getLog(DefaultSockJsService.class);
		if (logger.isWarnEnabled()) {
			logger.warn("Failed to create a default WebSocketTransportHandler", ex);
		}
	}
	if (overrides != null) {
		result.addAll(overrides);
	}
	return result;
}
 
Example #13
Source File: WebSocketSendToUserConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/greeting").setHandshakeHandler(new DefaultHandshakeHandler() {

        //Get sessionId from request and set it in Map attributes
        public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                                         Map attributes) throws Exception {
            if (request instanceof ServletServerHttpRequest) {
                ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
                HttpSession session = servletRequest.getServletRequest().getSession();
                attributes.put("sessionId", session.getId());
            }
            return true;
        }}).withSockJS();
}
 
Example #14
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 #15
Source File: WebsocketConfig.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    StompWebSocketEndpointRegistration endpoint = registry.addEndpoint("/my-websocket");//添加一个/my-websocket端点,客户端就可以通过这个端点来进行连接;

    endpoint.setHandshakeHandler(new DefaultHandshakeHandler(requestUpgradeStrategy()) {
        //这里获取首次握手的身份
        @Override
        protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
            String token = request.getHeaders().getFirst("access_token");
            return () -> "账号-" + token;
        }
    });//这里放入一个握手的处理器,可以处理自定义握手期间的事情,重写父类方法即可 选用netty的协议升级策略

    endpoint.setAllowedOrigins("*").withSockJS();//setAllowedOrigins(*)设置跨域.  withSockJS(*)添加SockJS支持
}
 
Example #16
Source File: WebSocketStompClientIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void registerStompEndpoints(StompEndpointRegistry registry) {
	// Can't rely on classpath detection
	RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
	registry.addEndpoint("/stomp")
			.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
			.setAllowedOrigins("*");
}
 
Example #17
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 #18
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 #19
Source File: WebSocketStompClientIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void registerStompEndpoints(StompEndpointRegistry registry) {
	// Can't rely on classpath detection
	RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
	registry.addEndpoint("/stomp")
			.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
			.setAllowedOrigins("*");
}
 
Example #20
Source File: DefaultHandshakeHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	super.setup();

	MockitoAnnotations.initMocks(this);
	this.handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
}
 
Example #21
Source File: DefaultHandshakeHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	super.setup();

	MockitoAnnotations.initMocks(this);
	this.handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
}
 
Example #22
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 #23
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 #24
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 #25
Source File: WebSocketStompClientIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void registerStompEndpoints(StompEndpointRegistry registry) {
	// Can't rely on classpath detection
	RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
	registry.addEndpoint("/stomp")
			.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
			.setAllowedOrigins("*");
}
 
Example #26
Source File: DefaultHandshakeHandlerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
	MockitoAnnotations.initMocks(this);
	this.handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
}
 
Example #27
Source File: AbstractWebSocketHandlerRegistration.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HandshakeHandler getOrCreateHandshakeHandler() {
	return (this.handshakeHandler != null ? this.handshakeHandler : new DefaultHandshakeHandler());
}
 
Example #28
Source File: AbstractWebSocketIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
public DefaultHandshakeHandler handshakeHandler() {
	return new DefaultHandshakeHandler(requestUpgradeStrategy());
}
 
Example #29
Source File: AbstractSockJsIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
	registry.addHandler(new EchoHandler(), "/echo").setHandshakeHandler(handshakeHandler).withSockJS();
	registry.addHandler(testServerHandler(), "/test").setHandshakeHandler(handshakeHandler).withSockJS();
}
 
Example #30
Source File: AbstractSockJsIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
	HandshakeHandler handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
	registry.addHandler(new EchoHandler(), "/echo").setHandshakeHandler(handshakeHandler).withSockJS();
	registry.addHandler(testServerHandler(), "/test").setHandshakeHandler(handshakeHandler).withSockJS();
}