From 25decee1a4873275de039d3cad6affd64edb41d6 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 31 Jul 2014 17:47:16 -0400 Subject: [PATCH] Polish --- src/asciidoc/index.adoc | 80 +++++++++++------------------------------ 1 file changed, 20 insertions(+), 60 deletions(-) diff --git a/src/asciidoc/index.adoc b/src/asciidoc/index.adoc index be7524d8a1..1bf3921a9d 100644 --- a/src/asciidoc/index.adoc +++ b/src/asciidoc/index.adoc @@ -38645,11 +38645,10 @@ for purging inactive destinations. [[websocket-stomp-appplication-context-events]] -==== Broker events and message interception +==== Listening To ApplicationContext Events and Intercepting Messages -The STOMP messaging support publishes the `ApplicationContext` events listed below. -To subscribe to receive one or more of these events a Spring managed component can -implement Spring's `ApplicationListener` interface. The events are: +Several `ApplicationContext` events (listed below) are published and can be +received by implementing Spring's `ApplicationListener` interface. * `BrokerAvailabilityEvent` -- indicates when the broker becomes available/unavailable. While the "simple" broker becomes available immediately on startup and remains so while @@ -38686,82 +38685,43 @@ will typically notice the broker is not responding within 10 seconds. Clients ne implement their own reconnect logic. ==== -An application can also intercept all incoming or outgoing messages by creating a -`ChannelInterceptor` and registering it on the inbound or outbound channel. See bellow -an example that filters messages based on the message destination and user: +Furthermore, an application can directly intercept every incoming and outgoing message by +registering a `ChannelInterceptor` on the respective message channel. For example +to intercept inbound messages: [source,java,indent=0] [subs="verbatim,quotes"] ---- -public class AccessControlChannelInterceptor extends ChannelInterceptorAdapter { + @Configuration + @EnableWebSocketMessageBroker + public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override - public Message preSend(Message message, MessageChannel channel) { - Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); - String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); - if(isAllowed(destination, user)) { - return message; - } - throw new AccessControlException("Message to destination " + destination - + " not allowed for user " + user.getName()); - } - - private boolean isAllowed(String destination, Principal user) { - // ... + public void configureClientInboundChannel(ChannelRegistration registration) { + registration.setInterceptors(new MyChannelInterceptor()); } } ---- -In Java config: +A custom `ChannelInterceptor` can extend the empty method base class +`ChannelInterceptorAdapter` and use `StompHeaderAccessor` or `SimpMessageHeaderAccessor` +to access information about the message. [source,java,indent=0] [subs="verbatim,quotes"] ---- - @Configuration - @EnableWebSocketMessageBroker - public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer { - - // ... + public class MyChannelInterceptor extends ChannelInterceptorAdapter { @Override - public void configureClientInboundChannel(ChannelRegistration registration) { - registration.setInterceptors(myInterceptor()); - } - - @Bean - public AccessControlChannelInterceptor myInterceptor() { - return new AccessControlChannelInterceptor(); + public Message preSend(Message message, MessageChannel channel) { + StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); + StompCommand command = accessor.getStompCommand(); + // ... + return message; } - } ---- -Or in XML config: - -[source,xml,indent=0] -[subs="verbatim,quotes,attributes"] ----- - - - - - - - - - - - - - ----- [[websocket-stomp-websocket-scope]]