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

The following examples show how to use org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor. 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 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 #2
Source File: WebSocketConfig.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    final String[] allowedOriginArray = getAllowedOriginArray(configProperties.getWebSocketAllowedOrigins());

    for (PinpointWebSocketHandler handler : handlerRepository.getWebSocketHandlerRepository()) {
        String path = handler.getRequestMapping() + WEBSOCKET_SUFFIX;

        WebSocketHandler webSocketHandler = webSocketHandlerDecoratorFactory.decorate(handler);
        final WebSocketHandlerRegistration webSocketHandlerRegistration = registry.addHandler(webSocketHandler, path);

        webSocketHandlerRegistration.addInterceptors(new HttpSessionHandshakeInterceptor());
        webSocketHandlerRegistration.addInterceptors(new WebSocketSessionContextPrepareHandshakeInterceptor());
        if (Objects.nonNull(customHandshakeInterceptor)) {
            webSocketHandlerRegistration.addInterceptors(customHandshakeInterceptor);
        }
        webSocketHandlerRegistration.setAllowedOrigins(allowedOriginArray);
    }
}
 
Example #3
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 #4
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 #5
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 #6
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

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

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #7
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

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

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #8
Source File: WebSocketHandlerRegistrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

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

	Mapping mapping = mappings.get(0);
	assertEquals(handler, mapping.webSocketHandler);
	assertEquals("/foo", mapping.path);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #9
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #10
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 #11
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 #12
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 #13
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #14
Source File: WebSocketHandlerRegistrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("https://mydomain1.com");

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #15
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 #16
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 #17
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptorsWithAllowedOrigins() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #18
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void interceptors() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #19
Source File: WebSocketHandlerRegistrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void emptyAllowedOrigin() {
	WebSocketHandler handler = new TextWebSocketHandler();
	HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();

	this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();

	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.interceptors);
	assertEquals(2, mapping.interceptors.length);
	assertEquals(interceptor, mapping.interceptors[0]);
	assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
 
Example #20
Source File: TraderUMainConfiguration.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(createNodeMgmtHandler(), NodeMgmtService.URI_WS_NODEMGMT)
    .setAllowedOrigins("*")
    .addInterceptors(new HttpSessionHandshakeInterceptor());
    //.setHandshakeHandler(createJettyHandshakeHandler());
}
 
Example #21
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 #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 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 #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: 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 #26
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 #27
Source File: SessionCacheWebSocketHandler.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    log.debug("after WebSocket connection established, http session id => {}, spaceKey => {}",
            session.getAttributes().get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME),
            session.getAttributes().get("spaceKey"));

    webSocketSessionStore.addSession(session.getId(), session);
    super.afterConnectionEstablished(session);
}
 
Example #28
Source File: WebSocketConfig.java    From seppb with MIT License 4 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(messageWebSocketHandler, "/myHandler")
            .addHandler(deploymentBuildWebSocketHandler, "/deployment-build")
            .addInterceptors(new HttpSessionHandshakeInterceptor()).setAllowedOrigins("*");
}
 
Example #29
Source File: SpringWebsocketsApplication.java    From training with MIT License 4 votes vote down vote up
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
	registry.addHandler(chatSocket, "/chat/*")
			.addInterceptors(new HttpSessionHandshakeInterceptor());
}