org.springframework.web.socket.server.HandshakeInterceptor Java Examples

The following examples show how to use org.springframework.web.socket.server.HandshakeInterceptor. 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: HandshakeInterceptorChain.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
		Map<String, Object> attributes) throws Exception {

	for (int i = 0; i < this.interceptors.size(); i++) {
		HandshakeInterceptor interceptor = this.interceptors.get(i);
		if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
			if (logger.isDebugEnabled()) {
				logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
			}
			applyAfterHandshake(request, response, null);
			return false;
		}
		this.interceptorIndex = i;
	}
	return true;
}
 
Example #2
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("https://mydomain1.com")
			.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);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("https://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #3
Source File: HandshakeInterceptorChain.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
		Map<String, Object> attributes) throws Exception {

	for (int i = 0; i < this.interceptors.size(); i++) {
		HandshakeInterceptor interceptor = this.interceptors.get(i);
		if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
			if (logger.isDebugEnabled()) {
				logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
			}
			applyAfterHandshake(request, response, null);
			return false;
		}
		this.interceptorIndex = i;
	}
	return true;
}
 
Example #4
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo")
			.addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com")
			.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);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #5
Source File: AbstractWebSocketHandlerRegistration.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.sockJsServiceRegistration = new SockJsServiceRegistration();
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.sockJsServiceRegistration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
		this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.sockJsServiceRegistration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
	}
	return this.sockJsServiceRegistration;
}
 
Example #6
Source File: AbstractWebSocketHandlerRegistration.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.sockJsServiceRegistration = new SockJsServiceRegistration();
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.sockJsServiceRegistration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
		this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.sockJsServiceRegistration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
	}
	return this.sockJsServiceRegistration;
}
 
Example #7
Source File: WebMvcStompWebSocketEndpointRegistration.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.registration = new StompSockJsServiceRegistration(this.sockJsTaskScheduler);
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.registration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
		this.registration.setTransportHandlerOverrides(transportHandler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.registration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
	}
	return this.registration;
}
 
Example #8
Source File: WebMvcStompWebSocketEndpointRegistration.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.registration = new SockJsServiceRegistration();
	this.registration.setTaskScheduler(this.sockJsTaskScheduler);
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.registration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler handler = new WebSocketTransportHandler(this.handshakeHandler);
		this.registration.setTransportHandlerOverrides(handler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.registration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
	}
	return this.registration;
}
 
Example #9
Source File: AbstractWebSocketHandlerRegistration.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.sockJsServiceRegistration = new SockJsServiceRegistration(this.sockJsTaskScheduler);
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.sockJsServiceRegistration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
		this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.sockJsServiceRegistration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()]));
	}
	return this.sockJsServiceRegistration;
}
 
Example #10
Source File: WebMvcStompWebSocketEndpointRegistration.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public SockJsServiceRegistration withSockJS() {
	this.registration = new SockJsServiceRegistration();
	this.registration.setTaskScheduler(this.sockJsTaskScheduler);
	HandshakeInterceptor[] interceptors = getInterceptors();
	if (interceptors.length > 0) {
		this.registration.setInterceptors(interceptors);
	}
	if (this.handshakeHandler != null) {
		WebSocketTransportHandler handler = new WebSocketTransportHandler(this.handshakeHandler);
		this.registration.setTransportHandlerOverrides(handler);
	}
	if (!this.allowedOrigins.isEmpty()) {
		this.registration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins));
	}
	return this.registration;
}
 
Example #11
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsPassedToSockJsRegistration() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor)
			.setAllowedOrigins("http://mydomain1.com").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);
	assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
	List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
	assertEquals(interceptor, interceptors.get(0));
	assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}
 
Example #12
Source File: HandshakeInterceptorChain.java    From java-technology-stack with MIT License 6 votes vote down vote up
public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
		Map<String, Object> attributes) throws Exception {

	for (int i = 0; i < this.interceptors.size(); i++) {
		HandshakeInterceptor interceptor = this.interceptors.get(i);
		if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
			if (logger.isDebugEnabled()) {
				logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
			}
			applyAfterHandshake(request, response, null);
			return false;
		}
		this.interceptorIndex = i;
	}
	return true;
}
 
Example #13
Source File: WebMvcStompWebSocketEndpointRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public StompWebSocketEndpointRegistration addInterceptors(HandshakeInterceptor... interceptors) {
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}
 
Example #14
Source File: ServletWebSocketHandlerRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
		WebSocketHandler wsHandler, HandshakeHandler handshakeHandler,
		HandshakeInterceptor[] interceptors, String path) {

	WebSocketHttpRequestHandler httpHandler = new WebSocketHttpRequestHandler(wsHandler, handshakeHandler);
	if (!ObjectUtils.isEmpty(interceptors)) {
		httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
	}
	mappings.add(httpHandler, path);
}
 
Example #15
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 #16
Source File: WebSocketHttpRequestHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Configure one or more WebSocket handshake request interceptors.
 */
public void setHandshakeInterceptors(List<HandshakeInterceptor> interceptors) {
	this.interceptors.clear();
	if (interceptors != null) {
		this.interceptors.addAll(interceptors);
	}
}
 
Example #17
Source File: HandshakeInterceptorChain.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void applyAfterHandshake(ServerHttpRequest request, ServerHttpResponse response, Exception failure) {
	for (int i = this.interceptorIndex; i >= 0; i--) {
		HandshakeInterceptor interceptor = this.interceptors.get(i);
		try {
			interceptor.afterHandshake(request, response, this.wsHandler, failure);
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn(interceptor + " threw exception in afterHandshake: " + ex);
			}
		}
	}
}
 
Example #18
Source File: TransportHandlingSockJsService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Configure one or more WebSocket handshake request interceptors.
 */
public void setHandshakeInterceptors(List<HandshakeInterceptor> interceptors) {
	this.interceptors.clear();
	if (interceptors != null) {
		this.interceptors.addAll(interceptors);
	}
}
 
Example #19
Source File: SockJsServiceRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public SockJsServiceRegistration setInterceptors(HandshakeInterceptor... interceptors) {
	this.interceptors.clear();
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}
 
Example #20
Source File: AbstractWebSocketHandlerRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors) {
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}
 
Example #21
Source File: HandshakeInterceptorChainTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	i1 = mock(HandshakeInterceptor.class);
	i2 = mock(HandshakeInterceptor.class);
	i3 = mock(HandshakeInterceptor.class);
	interceptors = Arrays.asList(i1, i2, i3);
	wsHandler = mock(WebSocketHandler.class);
	attributes = new HashMap<String, Object>();
}
 
Example #22
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public Mapping(WebSocketHandler h, String path, HandshakeHandler hh, HandshakeInterceptor[] interceptors) {
	this.webSocketHandler = h;
	this.path = path;
	this.handshakeHandler = hh;
	this.interceptors = interceptors;
	this.sockJsService = null;
}
 
Example #23
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 #24
Source File: ServletWebSocketHandlerRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
		WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler,
		HandshakeInterceptor[] interceptors, String path) {

	WebSocketHttpRequestHandler httpHandler =
			new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);

	if (!ObjectUtils.isEmpty(interceptors)) {
		httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
	}
	mappings.add(httpHandler, path);
}
 
Example #25
Source File: JsonRpcConfiguration.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private void publishWebSocketEndpoint(WebSocketHandlerRegistry wsHandlerRegistry,
    JsonRpcHandler<?> handler, List<String> paths) {

  ProtocolManager protocolManager = (ProtocolManager) ctx.getBean("protocolManager", handler);

  JsonRpcWebSocketHandler wsHandler = new JsonRpcWebSocketHandler(protocolManager);

  protocolManager.setPingWachdog(handler.isPingWatchdog());

  for (String path : paths) {

    WebSocketHandlerRegistration registration = wsHandlerRegistry.addHandler(wsHandler, path);

    List<String> origins = handler.allowedOrigins();
    registration.setAllowedOrigins(origins.toArray(new String[origins.size()]));

    if (handler.isSockJSEnabled()) {
      registration.withSockJS().setSessionCookieNeeded(false);
    }
  
    if (handler.getLabel() != null) {
      wsHandler.setLabel(handler.getLabel());
    }
    
    HandshakeInterceptor[] interceptors = new HandshakeInterceptor[handler.interceptors().size()];
    int i = 0;
    for (Object obj : handler.interceptors()) {
  	  if (obj instanceof HandshakeInterceptor) {
   	  interceptors[i] = (HandshakeInterceptor) obj;
   	  i++;
  	  }
    }
    registration.addInterceptors(interceptors);
    
  }
}
 
Example #26
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
public Mapping(WebSocketHandler h, String path, HandshakeHandler hh, HandshakeInterceptor[] interceptors) {
	this.webSocketHandler = h;
	this.path = path;
	this.handshakeHandler = hh;
	this.interceptors = interceptors;
	this.sockJsService = null;
}
 
Example #27
Source File: WebMvcStompWebSocketEndpointRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public StompWebSocketEndpointRegistration addInterceptors(HandshakeInterceptor... interceptors) {
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}
 
Example #28
Source File: HandshakeInterceptorChainTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	super.setup();

	i1 = mock(HandshakeInterceptor.class);
	i2 = mock(HandshakeInterceptor.class);
	i3 = mock(HandshakeInterceptor.class);
	interceptors = Arrays.asList(i1, i2, i3);
	wsHandler = mock(WebSocketHandler.class);
	attributes = new HashMap<>();
}
 
Example #29
Source File: AbstractWebSocketHandlerRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors) {
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}
 
Example #30
Source File: SockJsServiceRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
public SockJsServiceRegistration setInterceptors(HandshakeInterceptor... interceptors) {
	this.interceptors.clear();
	if (!ObjectUtils.isEmpty(interceptors)) {
		this.interceptors.addAll(Arrays.asList(interceptors));
	}
	return this;
}