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

The following examples show how to use org.springframework.web.socket.server.support.WebSocketHttpRequestHandler. 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 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 #2
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 #3
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 #4
Source File: HandlersBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void addMapping(Element element, ManagedMap<String, Object> urlMap, ParserContext context) {
	String pathAttribute = element.getAttribute("path");
	List<String> mappings = Arrays.asList(StringUtils.tokenizeToStringArray(pathAttribute, ","));
	RuntimeBeanReference handlerReference = new RuntimeBeanReference(element.getAttribute("handler"));

	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addIndexedArgumentValue(0, handlerReference);
	if (this.handshakeHandlerReference != null) {
		cavs.addIndexedArgumentValue(1, this.handshakeHandlerReference);
	}
	RootBeanDefinition requestHandlerDef = new RootBeanDefinition(WebSocketHttpRequestHandler.class, cavs, null);
	requestHandlerDef.setSource(context.extractSource(element));
	requestHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	requestHandlerDef.getPropertyValues().add("handshakeInterceptors", this.interceptorsList);
	String requestHandlerName = context.getReaderContext().registerWithGeneratedName(requestHandlerDef);
	RuntimeBeanReference requestHandlerRef = new RuntimeBeanReference(requestHandlerName);

	for (String mapping : mappings) {
		urlMap.put(mapping, requestHandlerRef);
	}
}
 
Example #5
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 #6
Source File: HandlersBeanDefinitionParser.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void addMapping(Element element, ManagedMap<String, Object> urlMap, ParserContext context) {
	String pathAttribute = element.getAttribute("path");
	String[] mappings = StringUtils.tokenizeToStringArray(pathAttribute, ",");
	RuntimeBeanReference handlerReference = new RuntimeBeanReference(element.getAttribute("handler"));

	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, handlerReference);
	cargs.addIndexedArgumentValue(1, this.handshakeHandlerReference);
	RootBeanDefinition requestHandlerDef = new RootBeanDefinition(WebSocketHttpRequestHandler.class, cargs, null);
	requestHandlerDef.setSource(context.extractSource(element));
	requestHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	requestHandlerDef.getPropertyValues().add("handshakeInterceptors", this.interceptorsList);
	String requestHandlerName = context.getReaderContext().registerWithGeneratedName(requestHandlerDef);
	RuntimeBeanReference requestHandlerRef = new RuntimeBeanReference(requestHandlerName);

	for (String mapping : mappings) {
		urlMap.put(mapping, requestHandlerRef);
	}
}
 
Example #7
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 #8
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 #9
Source File: HandlersBeanDefinitionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void addMapping(Element element, ManagedMap<String, Object> urlMap, ParserContext context) {
	String pathAttribute = element.getAttribute("path");
	String[] mappings = StringUtils.tokenizeToStringArray(pathAttribute, ",");
	RuntimeBeanReference handlerReference = new RuntimeBeanReference(element.getAttribute("handler"));

	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, handlerReference);
	cargs.addIndexedArgumentValue(1, this.handshakeHandlerReference);
	RootBeanDefinition requestHandlerDef = new RootBeanDefinition(WebSocketHttpRequestHandler.class, cargs, null);
	requestHandlerDef.setSource(context.extractSource(element));
	requestHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	requestHandlerDef.getPropertyValues().add("handshakeInterceptors", this.interceptorsList);
	String requestHandlerName = context.getReaderContext().registerWithGeneratedName(requestHandlerDef);
	RuntimeBeanReference requestHandlerRef = new RuntimeBeanReference(requestHandlerName);

	for (String mapping : mappings) {
		urlMap.put(mapping, requestHandlerRef);
	}
}
 
Example #10
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 #11
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void webSocketHandlerDecorator() throws Exception {
	ApplicationContext config = createConfig(WebSocketHandlerDecoratorConfig.class);
	WebSocketHandler handler = config.getBean(SubProtocolWebSocketHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) config.getBean("stompWebSocketHandlerMapping");
	WebSocketHttpRequestHandler httpHandler = (WebSocketHttpRequestHandler) mapping.getHandlerMap().get("/test");
	handler = httpHandler.getWebSocketHandler();

	WebSocketSession session = new TestWebSocketSession("id");
	handler.afterConnectionEstablished(session);
	assertEquals(true, session.getAttributes().get("decorated"));
}
 
Example #12
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void minimalRegistration() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

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

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertNotNull(((WebSocketHttpRequestHandler) entry.getKey()).getWebSocketHandler());
	assertEquals(1, ((WebSocketHttpRequestHandler) entry.getKey()).getHandshakeInterceptors().size());
	assertEquals(Arrays.asList("/foo"), entry.getValue());
}
 
Example #13
Source File: ServletWebSocketHandlerRegistration.java    From spring-analysis-note 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 #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: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void minimalRegistration() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

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

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertNotNull(((WebSocketHttpRequestHandler) entry.getKey()).getWebSocketHandler());
	assertEquals(1, ((WebSocketHttpRequestHandler) entry.getKey()).getHandshakeInterceptors().size());
	assertEquals(Arrays.asList("/foo"), entry.getValue());
}
 
Example #16
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void webSocketHandlerDecorator() throws Exception {
	ApplicationContext config = createConfig(WebSocketHandlerDecoratorConfig.class);
	WebSocketHandler handler = config.getBean(SubProtocolWebSocketHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) config.getBean("stompWebSocketHandlerMapping");
	WebSocketHttpRequestHandler httpHandler = (WebSocketHttpRequestHandler) mapping.getHandlerMap().get("/test");
	handler = httpHandler.getWebSocketHandler();

	WebSocketSession session = new TestWebSocketSession("id");
	handler.afterConnectionEstablished(session);
	assertEquals(true, session.getAttributes().get("decorated"));
}
 
Example #17
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void minimalRegistration() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

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

	Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
	assertNotNull(((WebSocketHttpRequestHandler) entry.getKey()).getWebSocketHandler());
	assertEquals(1, ((WebSocketHttpRequestHandler) entry.getKey()).getHandshakeInterceptors().size());
	assertEquals(Arrays.asList("/foo"), entry.getValue());
}
 
Example #18
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void allowedOrigins() {
	WebMvcStompWebSocketEndpointRegistration registration =
			new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.setAllowedOrigins();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertEquals(1, requestHandler.getHandshakeInterceptors().size());
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(0).getClass());
}
 
Example #19
Source File: WebMvcStompWebSocketEndpointRegistrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void sameOrigin() {
	WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);

	registration.setAllowedOrigins();

	MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
	assertEquals(1, mappings.size());
	WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler)mappings.entrySet().iterator().next().getKey();
	assertNotNull(requestHandler.getWebSocketHandler());
	assertEquals(1, requestHandler.getHandshakeInterceptors().size());
	assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(0).getClass());
}
 
Example #20
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void webSocketHandlerDecorator() throws Exception {
	ApplicationContext config = createConfig(WebSocketHandlerDecoratorConfig.class);
	WebSocketHandler handler = config.getBean(SubProtocolWebSocketHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) config.getBean("stompWebSocketHandlerMapping");
	WebSocketHttpRequestHandler httpHandler = (WebSocketHttpRequestHandler) mapping.getHandlerMap().get("/test");
	handler = httpHandler.getWebSocketHandler();

	WebSocketSession session = new TestWebSocketSession("id");
	handler.afterConnectionEstablished(session);
	assertEquals(true, session.getAttributes().get("decorated"));
}