use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler in project spring-framework by spring-projects.
the class TestValidator method stompBrokerRelay.
@Test
public void stompBrokerRelay() {
loadBeanDefinitions("websocket-config-broker-relay.xml");
HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
assertThat(hm).isNotNull();
assertThat(hm).isInstanceOf(SimpleUrlHandlerMapping.class);
SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
assertThat(suhm.getUrlMap()).hasSize(1);
assertThat(suhm.getOrder()).isEqualTo(2);
HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo/**");
assertThat(httpRequestHandler).isNotNull();
assertThat(httpRequestHandler).isInstanceOf(SockJsHttpRequestHandler.class);
SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
WebSocketHandler wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
assertThat(wsHandler).isNotNull();
assertThat(wsHandler).isInstanceOf(SubProtocolWebSocketHandler.class);
assertThat(sockJsHttpRequestHandler.getSockJsService()).isNotNull();
UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
assertThat(userDestResolver).isNotNull();
assertThat(userDestResolver).isInstanceOf(DefaultUserDestinationResolver.class);
DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
assertThat(defaultUserDestResolver.getDestinationPrefix()).isEqualTo("/user/");
StompBrokerRelayMessageHandler messageBroker = this.appContext.getBean(StompBrokerRelayMessageHandler.class);
assertThat(messageBroker).isNotNull();
assertThat(messageBroker.getClientLogin()).isEqualTo("clientlogin");
assertThat(messageBroker.getClientPasscode()).isEqualTo("clientpass");
assertThat(messageBroker.getSystemLogin()).isEqualTo("syslogin");
assertThat(messageBroker.getSystemPasscode()).isEqualTo("syspass");
assertThat(messageBroker.getRelayHost()).isEqualTo("relayhost");
assertThat(messageBroker.getRelayPort()).isEqualTo(1234);
assertThat(messageBroker.getVirtualHost()).isEqualTo("spring.io");
assertThat(messageBroker.getSystemHeartbeatReceiveInterval()).isEqualTo(5000);
assertThat(messageBroker.getSystemHeartbeatSendInterval()).isEqualTo(5000);
assertThat(messageBroker.getDestinationPrefixes()).containsExactlyInAnyOrder("/topic", "/queue");
assertThat(messageBroker.isPreservePublishOrder()).isTrue();
List<Class<? extends MessageHandler>> subscriberTypes = Arrays.asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, StompBrokerRelayMessageHandler.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(StompBrokerRelayMessageHandler.class, UserDestinationMessageHandler.class);
testChannel("brokerChannel", subscriberTypes, 1);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class));
String destination = "/topic/unresolved-user-destination";
UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
assertThat(userDestHandler.getBroadcastDestination()).isEqualTo(destination);
assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userDestHandler);
destination = "/topic/simp-user-registry";
UserRegistryMessageHandler userRegistryHandler = this.appContext.getBean(UserRegistryMessageHandler.class);
assertThat(userRegistryHandler.getBroadcastDestination()).isEqualTo(destination);
assertThat(messageBroker.getSystemSubscriptions()).isNotNull();
assertThat(messageBroker.getSystemSubscriptions().get(destination)).isSameAs(userRegistryHandler);
SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
assertThat(userRegistry.getClass()).isEqualTo(MultiServerUserRegistry.class);
String name = "webSocketMessageBrokerStats";
WebSocketMessageBrokerStats stats = this.appContext.getBean(name, WebSocketMessageBrokerStats.class);
String actual = stats.toString();
String expected = "WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), " + "0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)], " + "stompSubProtocol\\[processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "stompBrokerRelay\\[0 sessions, relayhost:1234 \\(not available\\), " + "processed CONNECT\\(0\\)-CONNECTED\\(0\\)-DISCONNECT\\(0\\)], " + "inboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "outboundChannel\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d], " + "sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " + "completed tasks = \\d]";
assertThat(actual.matches(expected)).as("\nExpected: " + expected.replace("\\", "") + "\n Actual: " + actual).isTrue();
}
use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler in project spring-framework by spring-projects.
the class TestMessageCodec method sockJs.
@Test
public void sockJs() {
loadBeanDefinitions("websocket-config-handlers-sockjs.xml");
SimpleUrlHandlerMapping handlerMapping = this.appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(handlerMapping).isNotNull();
SockJsHttpRequestHandler testHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
assertThat(testHandler).isNotNull();
unwrapAndCheckDecoratedHandlerType(testHandler.getWebSocketHandler(), TestWebSocketHandler.class);
SockJsService testSockJsService = testHandler.getSockJsService();
SockJsHttpRequestHandler fooHandler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/foo/**");
assertThat(fooHandler).isNotNull();
unwrapAndCheckDecoratedHandlerType(fooHandler.getWebSocketHandler(), FooWebSocketHandler.class);
SockJsService sockJsService = fooHandler.getSockJsService();
assertThat(sockJsService).isNotNull();
assertThat(sockJsService).isSameAs(testSockJsService);
assertThat(sockJsService).isInstanceOf(DefaultSockJsService.class);
DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsService;
assertThat(defaultSockJsService.getTaskScheduler()).isInstanceOf(ThreadPoolTaskScheduler.class);
assertThat(defaultSockJsService.shouldSuppressCors()).isFalse();
Map<TransportType, TransportHandler> handlerMap = defaultSockJsService.getTransportHandlers();
assertThat(handlerMap.values()).extracting("class").containsExactlyInAnyOrder(XhrPollingTransportHandler.class, XhrReceivingTransportHandler.class, XhrStreamingTransportHandler.class, EventSourceTransportHandler.class, HtmlFileTransportHandler.class, WebSocketTransportHandler.class);
WebSocketTransportHandler handler = (WebSocketTransportHandler) handlerMap.get(TransportType.WEBSOCKET);
assertThat(handler.getHandshakeHandler().getClass()).isEqualTo(TestHandshakeHandler.class);
List<HandshakeInterceptor> interceptors = defaultSockJsService.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactlyInAnyOrder(FooTestInterceptor.class, BarTestInterceptor.class, OriginHandshakeInterceptor.class);
}
use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler in project spring-security by spring-projects.
the class AbstractSecurityWebSocketMessageBrokerConfigurer method afterSingletonsInstantiated.
@Override
public void afterSingletonsInstantiated() {
if (sameOriginDisabled()) {
return;
}
String beanName = "stompWebSocketHandlerMapping";
SimpleUrlHandlerMapping mapping = this.context.getBean(beanName, SimpleUrlHandlerMapping.class);
Map<String, Object> mappings = mapping.getHandlerMap();
for (Object object : mappings.values()) {
if (object instanceof SockJsHttpRequestHandler) {
setHandshakeInterceptors((SockJsHttpRequestHandler) object);
} else if (object instanceof WebSocketHttpRequestHandler) {
setHandshakeInterceptors((WebSocketHttpRequestHandler) object);
} else {
throw new IllegalStateException("Bean " + beanName + " is expected to contain mappings to either a " + "SockJsHttpRequestHandler or a WebSocketHttpRequestHandler but got " + object);
}
}
if (this.inboundRegistry.containsMapping() && !this.inboundRegistry.isSimpDestPathMatcherConfigured()) {
PathMatcher pathMatcher = getDefaultPathMatcher();
this.inboundRegistry.simpDestPathMatcher(pathMatcher);
}
}
use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler 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(".");
}
use of org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler in project spring-framework by spring-projects.
the class TestMessageCodec method sockJsAttributes.
@Test
public void sockJsAttributes() {
loadBeanDefinitions("websocket-config-handlers-sockjs-attributes.xml");
SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
assertThat(handlerMapping).isNotNull();
SockJsHttpRequestHandler handler = (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
assertThat(handler).isNotNull();
unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
SockJsService sockJsService = handler.getSockJsService();
assertThat(sockJsService).isNotNull();
assertThat(sockJsService).isInstanceOf(TransportHandlingSockJsService.class);
TransportHandlingSockJsService transportService = (TransportHandlingSockJsService) sockJsService;
assertThat(transportService.getTaskScheduler()).isInstanceOf(TestTaskScheduler.class);
assertThat(transportService.getTransportHandlers().values()).extracting("class").containsExactlyInAnyOrder(XhrPollingTransportHandler.class, XhrStreamingTransportHandler.class);
assertThat(transportService.getName()).isEqualTo("testSockJsService");
assertThat(transportService.isWebSocketEnabled()).isFalse();
assertThat(transportService.isSessionCookieNeeded()).isFalse();
assertThat(transportService.getStreamBytesLimit()).isEqualTo(2048);
assertThat(transportService.getDisconnectDelay()).isEqualTo(256);
assertThat(transportService.getHttpMessageCacheSize()).isEqualTo(1024);
assertThat(transportService.getHeartbeatTime()).isEqualTo(20);
assertThat(transportService.getSockJsClientLibraryUrl()).isEqualTo("/js/sockjs.min.js");
assertThat(transportService.getMessageCodec().getClass()).isEqualTo(TestMessageCodec.class);
List<HandshakeInterceptor> interceptors = transportService.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactly(OriginHandshakeInterceptor.class);
assertThat(transportService.shouldSuppressCors()).isTrue();
assertThat(transportService.getAllowedOrigins()).containsExactly("https://mydomain1.example", "https://mydomain2.example");
assertThat(transportService.getAllowedOriginPatterns()).containsExactly("https://*.mydomain.example");
}
Aggregations