diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java index 0c8bab682e..fd6c944505 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java @@ -271,6 +271,12 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { RootBeanDefinition stompHandlerDef = new RootBeanDefinition(StompSubProtocolHandler.class); registerBeanDef(stompHandlerDef, context, source); + Element errorHandlerElem = DomUtils.getChildElementByTagName(element, "stomp-error-handler"); + if (errorHandlerElem != null) { + RuntimeBeanReference errorHandlerRef = new RuntimeBeanReference(errorHandlerElem.getAttribute("ref")); + stompHandlerDef.getPropertyValues().add("errorHandler", errorHandlerRef); + } + ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addIndexedArgumentValue(0, inChannel); cavs.addIndexedArgumentValue(1, outChannel); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/StompEndpointRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/StompEndpointRegistry.java index b1c4deffce..f9adf1a30b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/StompEndpointRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/StompEndpointRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.web.socket.config.annotation; +import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler; + /** * A contract for registering STOMP over WebSocket endpoints. * @@ -29,4 +31,11 @@ public interface StompEndpointRegistry { */ StompWebSocketEndpointRegistration addEndpoint(String... paths); + /** + * Configure a handler for customizing or handling STOMP ERROR frames to clients. + * @param errorHandler the error handler + * @since 4.2 + */ + WebMvcStompEndpointRegistry setErrorHandler(StompSubProtocolErrorHandler errorHandler); + } \ No newline at end of file diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java index 8239c70566..66a4b58b32 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java @@ -30,6 +30,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.handler.WebSocketHandlerDecorator; +import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; import org.springframework.web.util.UrlPathHelper; @@ -134,6 +135,12 @@ public class WebMvcStompEndpointRegistry implements StompEndpointRegistry { return this.urlPathHelper; } + @Override + public WebMvcStompEndpointRegistry setErrorHandler(StompSubProtocolErrorHandler errorHandler) { + this.stompHandler.setErrorHandler(errorHandler); + return this; + } + /** * Return a handler mapping with the mapped ViewControllers; or {@code null} in case of no registrations. */ diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolErrorHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolErrorHandler.java index 9f0d28e27f..4f58c0d0c2 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolErrorHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolErrorHandler.java @@ -30,9 +30,9 @@ public interface SubProtocolErrorHandler

{ * opportunity to prepare the error message or to prevent one from being sent. * *

Note that the STOMP protocol requires a server to close the connection - * after sending an ERROR frame. To prevent that, a handler could return - * {@code null} and send a message through the broker instead, e.g. via a - * user destination targeting the user. + * after sending an ERROR frame. To prevent an ERROR frame from being sent, + * a handler could return {@code null} and send a notification message + * through the broker instead, e.g. via a user destination. * * @param clientMessage the client message related to the error, possibly * {@code null} if error occurred while parsing a WebSocket message diff --git a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.2.xsd b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.2.xsd index 90a535c5b6..b5eb97d258 100644 --- a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.2.xsd +++ b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.2.xsd @@ -727,6 +727,22 @@ + + + + + + + + + + + + diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java index 68729089e0..5c7c949104 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java @@ -16,6 +16,9 @@ package org.springframework.web.socket.config; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @@ -64,9 +67,6 @@ import org.springframework.web.socket.sockjs.transport.handler.XhrPollingTranspo import org.springframework.web.socket.sockjs.transport.handler.XhrReceivingTransportHandler; import org.springframework.web.socket.sockjs.transport.handler.XhrStreamingTransportHandler; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - /** * Test fixture for HandlersBeanDefinitionParser. * See test configuration files websocket-config-handlers-*.xml. diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java index 862e0ade48..1c7f876214 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java @@ -73,6 +73,7 @@ import org.springframework.web.socket.handler.TestWebSocketSession; import org.springframework.web.socket.handler.WebSocketHandlerDecorator; import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory; import org.springframework.web.socket.messaging.DefaultSimpUserRegistry; +import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; @@ -144,6 +145,8 @@ public class MessageBrokerBeanDefinitionParserTests { StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) handlerMap.get("v12.stomp"); assertNotNull(stompHandler); assertEquals(128 * 1024, stompHandler.getMessageSizeLimit()); + assertNotNull(stompHandler.getErrorHandler()); + assertEquals(TestStompErrorHandler.class, stompHandler.getErrorHandler().getClass()); assertNotNull(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher")); @@ -498,4 +501,8 @@ class TestWebSocketHandlerDecoratorFactory implements WebSocketHandlerDecoratorF } }; } +} + +class TestStompErrorHandler extends StompSubProtocolErrorHandler { + } \ No newline at end of file diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistryTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistryTests.java index 25fa934447..138f08df7c 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistryTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,16 +17,18 @@ package org.springframework.web.socket.config.annotation; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; import org.springframework.messaging.SubscribableChannel; import org.springframework.scheduling.TaskScheduler; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; +import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler; +import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; import org.springframework.web.util.UrlPathHelper; @@ -46,12 +48,12 @@ public class WebMvcStompEndpointRegistryTests { @Before public void setup() { - SubscribableChannel inChannel = Mockito.mock(SubscribableChannel.class); - SubscribableChannel outChannel = Mockito.mock(SubscribableChannel.class); + SubscribableChannel inChannel = mock(SubscribableChannel.class); + SubscribableChannel outChannel = mock(SubscribableChannel.class); this.webSocketHandler = new SubProtocolWebSocketHandler(inChannel, outChannel); WebSocketTransportRegistration transport = new WebSocketTransportRegistration(); - TaskScheduler scheduler = Mockito.mock(TaskScheduler.class); + TaskScheduler scheduler = mock(TaskScheduler.class); this.endpointRegistry = new WebMvcStompEndpointRegistry(this.webSocketHandler, transport, null, scheduler); } @@ -87,4 +89,15 @@ public class WebMvcStompEndpointRegistryTests { assertSame(pathHelper, hm.getUrlPathHelper()); } + @Test + public void errorHandler() throws Exception { + StompSubProtocolErrorHandler errorHandler = mock(StompSubProtocolErrorHandler.class); + this.endpointRegistry.setErrorHandler(errorHandler); + this.endpointRegistry.addEndpoint("/stompOverWebSocket"); + + Map protocolHandlers = this.webSocketHandler.getProtocolHandlerMap(); + StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) protocolHandlers.get("v12.stomp"); + assertSame(errorHandler, stompHandler.getErrorHandler()); + } + } diff --git a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-simple.xml b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-simple.xml index 82b3437ab4..ed9baf5c09 100644 --- a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-simple.xml +++ b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-simple.xml @@ -33,6 +33,8 @@ + + @@ -47,6 +49,7 @@ +