001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.messaging;
018
019import java.security.Principal;
020
021import org.springframework.context.ApplicationEvent;
022import org.springframework.lang.Nullable;
023import org.springframework.messaging.Message;
024import org.springframework.util.Assert;
025
026/**
027 * A base class for events for a message received from a WebSocket client and
028 * parsed into a higher-level sub-protocol (e.g. STOMP).
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.1
032 */
033@SuppressWarnings("serial")
034public abstract class AbstractSubProtocolEvent extends ApplicationEvent {
035
036        private final Message<byte[]> message;
037
038        @Nullable
039        private final Principal user;
040
041
042        /**
043         * Create a new AbstractSubProtocolEvent.
044         * @param source the component that published the event (never {@code null})
045         * @param message the incoming message (never {@code null})
046         */
047        protected AbstractSubProtocolEvent(Object source, Message<byte[]> message) {
048                this(source, message, null);
049        }
050
051        /**
052         * Create a new AbstractSubProtocolEvent.
053         * @param source the component that published the event (never {@code null})
054         * @param message the incoming message (never {@code null})
055         */
056        protected AbstractSubProtocolEvent(Object source, Message<byte[]> message, @Nullable Principal user) {
057                super(source);
058                Assert.notNull(message, "Message must not be null");
059                this.message = message;
060                this.user = user;
061        }
062
063
064        /**
065         * Return the Message associated with the event. Here is an example of
066         * obtaining information about the session id or any headers in the
067         * message:
068         * <pre class="code">
069         * StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
070         * headers.getSessionId();
071         * headers.getSessionAttributes();
072         * headers.getPrincipal();
073         * </pre>
074         */
075        public Message<byte[]> getMessage() {
076                return this.message;
077        }
078
079        /**
080         * Return the user for the session associated with the event.
081         */
082        @Nullable
083        public Principal getUser() {
084                return this.user;
085        }
086
087        @Override
088        public String toString() {
089                return getClass().getSimpleName() + "[" + this.message + "]";
090        }
091
092}