From 47ef45d15248d26331ed669b32136fec8bfd0cdb Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 2 Dec 2013 15:25:09 -0500 Subject: [PATCH] Polish websocket xml namespace Issue: SPR-11063 --- .../DefaultUserDestinationResolver.java | 7 +++ .../MessageBrokerBeanDefinitionParser.java | 48 +++++++++++-------- .../config/xml/spring-websocket-4.0.xsd | 2 +- ...essageBrokerBeanDefinitionParserTests.java | 17 ++++++- .../HandlersBeanDefinitionParserTests.java | 3 +- .../xml/websocket-config-broker-simple.xml | 2 +- 6 files changed, 54 insertions(+), 25 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/handler/DefaultUserDestinationResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/handler/DefaultUserDestinationResolver.java index 7205643b22..ee76da4326 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/handler/DefaultUserDestinationResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/handler/DefaultUserDestinationResolver.java @@ -90,6 +90,13 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { return this.subscriptionDestinationPrefix; } + /** + * Return the configured {@link UserSessionRegistry}. + */ + public UserSessionRegistry getUserSessionRegistry() { + return this.userSessionRegistry; + } + @Override public Set resolveDestination(Message message) { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParser.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParser.java index 02f129b2ca..1f8f994472 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParser.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParser.java @@ -41,6 +41,7 @@ import org.springframework.messaging.support.converter.DefaultContentTypeResolve import org.springframework.messaging.support.converter.MappingJackson2MessageConverter; import org.springframework.messaging.support.converter.StringMessageConverter; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.MimeTypeUtils; import org.springframework.util.StringUtils; @@ -111,41 +112,45 @@ public class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { handlerMappingDef.getPropertyValues().add("order", order); handlerMappingDef.getPropertyValues().add("urlMap", urlMap); - String channelName = "clientInboundChannel"; + String beanName = "clientInboundChannel"; Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel"); - RuntimeBeanReference clientInChannel = getMessageChannel(channelName, channelElem, parserCxt, source); + RuntimeBeanReference clientInChannel = getMessageChannel(beanName, channelElem, parserCxt, source); - channelName = "clientOutboundChannel"; + beanName = "clientOutboundChannel"; channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel"); - RuntimeBeanReference clientOutChannel = getMessageChannel(channelName, channelElem, parserCxt, source); + RuntimeBeanReference clientOutChannel = getMessageChannel(beanName, channelElem, parserCxt, source); - RootBeanDefinition userSessionRegistryDef = new RootBeanDefinition(DefaultUserSessionRegistry.class); - String userSessionRegistryName = registerBeanDef(userSessionRegistryDef, parserCxt, source); - RuntimeBeanReference userSessionRegistry = new RuntimeBeanReference(userSessionRegistryName); + RootBeanDefinition beanDef = new RootBeanDefinition(DefaultUserSessionRegistry.class); + beanName = registerBeanDef(beanDef, parserCxt, source); + RuntimeBeanReference userSessionRegistry = new RuntimeBeanReference(beanName); - RuntimeBeanReference subProtocolWebSocketHandler = registerSubProtocolWebSocketHandler( + RuntimeBeanReference subProtocolWsHandler = registerSubProtocolWebSocketHandler( clientInChannel, clientOutChannel, userSessionRegistry, parserCxt, source); - List stompEndpointElements = DomUtils.getChildElementsByTagName(element, "stomp-endpoint"); - for(Element stompEndpointElement : stompEndpointElements) { + for(Element stompEndpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) { - RuntimeBeanReference requestHandler = registerHttpRequestHandler( - stompEndpointElement, subProtocolWebSocketHandler, parserCxt, source); + RuntimeBeanReference httpRequestHandler = registerHttpRequestHandler( + stompEndpointElem, subProtocolWsHandler, parserCxt, source); - List paths = Arrays.asList(stompEndpointElement.getAttribute("path").split(",")); + String pathAttribute = stompEndpointElem.getAttribute("path"); + Assert.state(StringUtils.hasText(pathAttribute), "Invalid (no path mapping)"); + + List paths = Arrays.asList(pathAttribute.split(",")); for(String path : paths) { - if (DomUtils.getChildElementByTagName(stompEndpointElement, "sockjs") != null) { + path = path.trim(); + Assert.state(StringUtils.hasText(path), "Invalid path attribute: " + pathAttribute); + if (DomUtils.getChildElementByTagName(stompEndpointElem, "sockjs") != null) { path = path.endsWith("/") ? path + "**" : path + "/**"; } - urlMap.put(path, requestHandler); + urlMap.put(path, httpRequestHandler); } } registerBeanDef(handlerMappingDef, parserCxt, source); - channelName = "brokerChannel"; + beanName = "brokerChannel"; channelElem = DomUtils.getChildElementByTagName(element, "broker-channel"); - RuntimeBeanReference brokerChannel = getMessageChannel(channelName, channelElem, parserCxt, source); + RuntimeBeanReference brokerChannel = getMessageChannel(beanName, channelElem, parserCxt, source); registerMessageBroker(element, clientInChannel, clientOutChannel, brokerChannel, parserCxt, source); RuntimeBeanReference messageConverter = registerBrokerMessageConverter(parserCxt, source); @@ -156,7 +161,7 @@ public class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { messageConverter, messagingTemplate, parserCxt, source); RuntimeBeanReference userDestinationResolver = registerUserDestinationResolver(element, - userSessionRegistryDef, parserCxt, source); + userSessionRegistry, parserCxt, source); registerUserDestinationMessageHandler(clientInChannel, clientOutChannel, brokerChannel, userDestinationResolver, parserCxt, source); @@ -320,7 +325,8 @@ public class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { mpvs.add("virtualHost",virtualHost); } - RootBeanDefinition messageBrokerDef = new RootBeanDefinition(StompBrokerRelayMessageHandler.class, cavs, mpvs); + Class handlerType = StompBrokerRelayMessageHandler.class; + RootBeanDefinition messageBrokerDef = new RootBeanDefinition(handlerType, cavs, mpvs); registerBeanDef(messageBrokerDef, parserCxt, source); } @@ -386,10 +392,10 @@ public class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { } private RuntimeBeanReference registerUserDestinationResolver(Element messageBrokerElement, - BeanDefinition userSessionRegistryDef, ParserContext parserCxt, Object source) { + RuntimeBeanReference userSessionRegistry, ParserContext parserCxt, Object source) { ConstructorArgumentValues cavs = new ConstructorArgumentValues(); - cavs.addIndexedArgumentValue(0, userSessionRegistryDef); + cavs.addIndexedArgumentValue(0, userSessionRegistry); RootBeanDefinition userDestinationResolverDef = new RootBeanDefinition(DefaultUserDestinationResolver.class, cavs, null); String prefix = messageBrokerElement.getAttribute("user-destination-prefix"); diff --git a/spring-websocket/src/main/resources/org/springframework/web/socket/server/config/xml/spring-websocket-4.0.xsd b/spring-websocket/src/main/resources/org/springframework/web/socket/server/config/xml/spring-websocket-4.0.xsd index c493a13e96..2454cf8ea4 100644 --- a/spring-websocket/src/main/resources/org/springframework/web/socket/server/config/xml/spring-websocket-4.0.xsd +++ b/spring-websocket/src/main/resources/org/springframework/web/socket/server/config/xml/spring-websocket-4.0.xsd @@ -192,7 +192,7 @@ - + diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParserTests.java index 2119439f07..3965d7d944 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/config/xml/MessageBrokerBeanDefinitionParserTests.java @@ -35,6 +35,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler; import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler; @@ -47,7 +48,9 @@ import java.util.Map; import static org.junit.Assert.*; /** - * Test fixture for the configuration in websocket-config-broker*.xml test files. + * Test fixture for MessageBrokerBeanDefinitionParser. + * See test configuration files websocket-config-broker-*.xml. + * * @author Brian Clozel */ public class MessageBrokerBeanDefinitionParserTests { @@ -82,6 +85,10 @@ public class MessageBrokerBeanDefinitionParserTests { SubProtocolWebSocketHandler subProtocolWsHandler = (SubProtocolWebSocketHandler) wsHandler; assertEquals(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"), subProtocolWsHandler.getSubProtocols()); + StompSubProtocolHandler stompHandler = + (StompSubProtocolHandler) subProtocolWsHandler.getProtocolHandlerMap().get("v12.stomp"); + assertNotNull(stompHandler); + httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**"); assertNotNull(httpRequestHandler); assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class)); @@ -91,12 +98,20 @@ public class MessageBrokerBeanDefinitionParserTests { assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class)); assertNotNull(sockJsHttpRequestHandler.getSockJsService()); + UserSessionRegistry userSessionRegistry = this.appContext.getBean(UserSessionRegistry.class); + assertNotNull(userSessionRegistry); + UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class); assertNotNull(userDestResolver); assertThat(userDestResolver, Matchers.instanceOf(DefaultUserDestinationResolver.class)); DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver; assertEquals("/personal/", defaultUserDestResolver.getDestinationPrefix()); + assertSame(stompHandler.getUserSessionRegistry(), defaultUserDestResolver.getUserSessionRegistry()); + + UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class); + assertNotNull(userDestHandler); + List> subscriberTypes = Arrays.>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/server/config/xml/HandlersBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/server/config/xml/HandlersBeanDefinitionParserTests.java index cbdd88d3fe..d3b56f96c1 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/server/config/xml/HandlersBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/server/config/xml/HandlersBeanDefinitionParserTests.java @@ -64,7 +64,7 @@ import static org.junit.Assert.assertTrue; /** * Test fixture for HandlersBeanDefinitionParser. - * See test configuration files websocket-config-handlers*.xml. + * See test configuration files websocket-config-handlers-*.xml. * * @author Brian Clozel */ @@ -227,6 +227,7 @@ public class HandlersBeanDefinitionParserTests { } class TestWebSocketHandler implements WebSocketHandler { + @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception {} diff --git a/spring-websocket/src/test/resources/org/springframework/web/socket/messaging/config/xml/websocket-config-broker-simple.xml b/spring-websocket/src/test/resources/org/springframework/web/socket/messaging/config/xml/websocket-config-broker-simple.xml index 4447bb341d..387d3343b3 100644 --- a/spring-websocket/src/test/resources/org/springframework/web/socket/messaging/config/xml/websocket-config-broker-simple.xml +++ b/spring-websocket/src/test/resources/org/springframework/web/socket/messaging/config/xml/websocket-config-broker-simple.xml @@ -21,7 +21,7 @@ http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"> - +