use of org.springframework.web.socket.messaging.SubProtocolHandler in project spring-integration by spring-projects.
the class SubProtocolHandlerRegistryTests method testSingleHandler.
@Test
public void testSingleHandler() {
SubProtocolHandler testProtocolHandler = spy(new StompSubProtocolHandler());
when(testProtocolHandler.getSupportedProtocols()).thenReturn(Collections.singletonList("foo"));
SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(Collections.<SubProtocolHandler>singletonList(testProtocolHandler));
WebSocketSession session = mock(WebSocketSession.class);
when(session.getAcceptedProtocol()).thenReturn("foo", (String) null);
SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertSame(protocolHandler, testProtocolHandler);
protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertSame(protocolHandler, testProtocolHandler);
}
use of org.springframework.web.socket.messaging.SubProtocolHandler in project spring-integration by spring-projects.
the class SubProtocolHandlerRegistryTests method testProtocolHandlers.
@Test
public void testProtocolHandlers() {
SubProtocolHandler defaultProtocolHandler = mock(SubProtocolHandler.class);
SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(Collections.<SubProtocolHandler>singletonList(new StompSubProtocolHandler()), defaultProtocolHandler);
WebSocketSession session = mock(WebSocketSession.class);
when(session.getAcceptedProtocol()).thenReturn("v10.stomp", (String) null);
SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertThat(protocolHandler, instanceOf(StompSubProtocolHandler.class));
protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertSame(protocolHandler, defaultProtocolHandler);
assertEquals(subProtocolHandlerRegistry.getSubProtocols(), new StompSubProtocolHandler().getSupportedProtocols());
}
use of org.springframework.web.socket.messaging.SubProtocolHandler in project spring-integration by spring-projects.
the class SubProtocolHandlerRegistryTests method testHandlerSelection.
@Test
public void testHandlerSelection() {
SubProtocolHandler testProtocolHandler = new StompSubProtocolHandler();
SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(testProtocolHandler);
WebSocketSession session = mock(WebSocketSession.class);
when(session.getAcceptedProtocol()).thenReturn("foo", "", null);
try {
subProtocolHandlerRegistry.findProtocolHandler(session);
fail("IllegalStateException expected");
} catch (Exception e) {
assertThat(e, instanceOf(IllegalStateException.class));
assertThat(e.getMessage(), containsString("No handler for sub-protocol 'foo'"));
}
SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertSame(protocolHandler, testProtocolHandler);
protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session);
assertNotNull(protocolHandler);
assertSame(protocolHandler, testProtocolHandler);
}
use of org.springframework.web.socket.messaging.SubProtocolHandler in project spring-integration by spring-projects.
the class SubProtocolHandlerRegistry method findProtocolHandler.
/**
* Resolves the {@link SubProtocolHandler} for the given {@code session} using
* its {@link WebSocketSession#getAcceptedProtocol() accepted sub-protocol}.
* @param session The session to resolve the sub-protocol handler for
* @return The sub-protocol handler
* @throws IllegalStateException if a protocol handler cannot be resolved
*/
public SubProtocolHandler findProtocolHandler(WebSocketSession session) {
SubProtocolHandler handler;
String protocol = session.getAcceptedProtocol();
if (StringUtils.hasText(protocol)) {
handler = this.protocolHandlers.get(protocol);
Assert.state(handler != null, "No handler for sub-protocol '" + protocol + "', handlers = " + this.protocolHandlers);
} else {
handler = this.defaultProtocolHandler;
Assert.state(handler != null, "No sub-protocol was requested and a default sub-protocol handler was not configured");
}
return handler;
}
use of org.springframework.web.socket.messaging.SubProtocolHandler in project spring-framework by spring-projects.
the class TestValidator method simpleBroker.
@Test
public void simpleBroker() throws Exception {
loadBeanDefinitions("websocket-config-broker-simple.xml");
HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
assertThat(hm).isInstanceOf(SimpleUrlHandlerMapping.class);
SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
assertThat(suhm.getUrlMap()).hasSize(4);
HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo");
assertThat(httpRequestHandler).isNotNull();
assertThat(httpRequestHandler).isInstanceOf(WebSocketHttpRequestHandler.class);
WebSocketHttpRequestHandler wsHttpRequestHandler = (WebSocketHttpRequestHandler) httpRequestHandler;
HandshakeHandler handshakeHandler = wsHttpRequestHandler.getHandshakeHandler();
assertThat(handshakeHandler).isNotNull();
assertThat(handshakeHandler instanceof TestHandshakeHandler).isTrue();
List<HandshakeInterceptor> interceptors = wsHttpRequestHandler.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
WebSocketSession session = new TestWebSocketSession("id");
wsHttpRequestHandler.getWebSocketHandler().afterConnectionEstablished(session);
assertThat(session.getAttributes().get("decorated")).asInstanceOf(BOOLEAN).isTrue();
WebSocketHandler wsHandler = wsHttpRequestHandler.getWebSocketHandler();
assertThat(wsHandler).isInstanceOf(ExceptionWebSocketHandlerDecorator.class);
wsHandler = ((ExceptionWebSocketHandlerDecorator) wsHandler).getDelegate();
assertThat(wsHandler).isInstanceOf(LoggingWebSocketHandlerDecorator.class);
wsHandler = ((LoggingWebSocketHandlerDecorator) wsHandler).getDelegate();
assertThat(wsHandler).isInstanceOf(TestWebSocketHandlerDecorator.class);
wsHandler = ((TestWebSocketHandlerDecorator) wsHandler).getDelegate();
assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
assertThat(this.appContext.getBean(MessageBrokerBeanDefinitionParser.WEB_SOCKET_HANDLER_BEAN_NAME)).isSameAs(wsHandler);
SubProtocolWebSocketHandler subProtocolWsHandler = (SubProtocolWebSocketHandler) wsHandler;
assertThat(subProtocolWsHandler.getSubProtocols()).isEqualTo(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
assertThat(subProtocolWsHandler.getSendTimeLimit()).isEqualTo(25 * 1000);
assertThat(subProtocolWsHandler.getSendBufferSizeLimit()).isEqualTo(1024 * 1024);
assertThat(subProtocolWsHandler.getTimeToFirstMessage()).isEqualTo(30 * 1000);
Map<String, SubProtocolHandler> handlerMap = subProtocolWsHandler.getProtocolHandlerMap();
StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) handlerMap.get("v12.stomp");
assertThat(stompHandler).isNotNull();
assertThat(stompHandler.getMessageSizeLimit()).isEqualTo(128 * 1024);
assertThat(stompHandler.getErrorHandler()).isNotNull();
assertThat(stompHandler.getErrorHandler().getClass()).isEqualTo(TestStompErrorHandler.class);
assertThat(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher")).isNotNull();
httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
assertThat(httpRequestHandler).isNotNull();
assertThat(httpRequestHandler).isInstanceOf(SockJsHttpRequestHandler.class);
SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
assertThat(wsHandler).isNotNull();
assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
assertThat(sockJsHttpRequestHandler.getSockJsService()).isNotNull();
assertThat(sockJsHttpRequestHandler.getSockJsService()).isInstanceOf(DefaultSockJsService.class);
DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsHttpRequestHandler.getSockJsService();
WebSocketTransportHandler wsTransportHandler = (WebSocketTransportHandler) defaultSockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
assertThat(wsTransportHandler.getHandshakeHandler()).isNotNull();
assertThat(wsTransportHandler.getHandshakeHandler()).isInstanceOf(TestHandshakeHandler.class);
assertThat(defaultSockJsService.shouldSuppressCors()).isFalse();
ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) defaultSockJsService.getTaskScheduler();
ScheduledThreadPoolExecutor executor = scheduler.getScheduledThreadPoolExecutor();
assertThat(executor.getCorePoolSize()).isEqualTo(Runtime.getRuntime().availableProcessors());
assertThat(executor.getRemoveOnCancelPolicy()).isTrue();
interceptors = defaultSockJsService.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
assertThat(defaultSockJsService.getAllowedOrigins()).containsExactly("https://mydomain3.com", "https://mydomain4.com");
assertThat(defaultSockJsService.getAllowedOriginPatterns()).containsExactly("https://*.mydomain.com");
SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
assertThat(userRegistry).isNotNull();
assertThat(userRegistry.getClass()).isEqualTo(DefaultSimpUserRegistry.class);
UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
assertThat(userDestResolver).isNotNull();
assertThat(userDestResolver).isInstanceOf(DefaultUserDestinationResolver.class);
DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
assertThat(defaultUserDestResolver.getDestinationPrefix()).isEqualTo("/personal/");
UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
assertThat(userDestHandler).isNotNull();
SimpleBrokerMessageHandler brokerMessageHandler = this.appContext.getBean(SimpleBrokerMessageHandler.class);
assertThat(brokerMessageHandler).isNotNull();
Collection<String> prefixes = brokerMessageHandler.getDestinationPrefixes();
assertThat(new ArrayList<>(prefixes)).isEqualTo(Arrays.asList("/topic", "/queue"));
DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry) brokerMessageHandler.getSubscriptionRegistry();
assertThat(registry.getSelectorHeaderName()).isEqualTo("my-selector");
assertThat(brokerMessageHandler.getTaskScheduler()).isNotNull();
assertThat(brokerMessageHandler.getHeartbeatValue()).isEqualTo(new long[] { 15000, 15000 });
assertThat(brokerMessageHandler.isPreservePublishOrder()).isTrue();
List<Class<? extends MessageHandler>> subscriberTypes = Arrays.asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class);
testChannel("clientInboundChannel", subscriberTypes, 2);
testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
testChannel("clientOutboundChannel", subscriberTypes, 2);
testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
subscriberTypes = Arrays.asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
testChannel("brokerChannel", subscriberTypes, 1);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class));
assertThat(this.appContext.getBean("webSocketScopeConfigurer", CustomScopeConfigurer.class)).isNotNull();
DirectFieldAccessor accessor = new DirectFieldAccessor(registry);
Object pathMatcher = accessor.getPropertyValue("pathMatcher");
String pathSeparator = (String) new DirectFieldAccessor(pathMatcher).getPropertyValue("pathSeparator");
assertThat(pathSeparator).isEqualTo(".");
}
Aggregations