001/*
002 * Copyright 2002-2016 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.messaging.Message;
022import org.springframework.util.Assert;
023import org.springframework.web.socket.CloseStatus;
024
025/**
026 * Event raised when the session of a WebSocket client using a Simple Messaging
027 * Protocol (e.g. STOMP) as the WebSocket sub-protocol is closed.
028 *
029 * <p>Note that this event may be raised more than once for a single session and
030 * therefore event consumers should be idempotent and ignore a duplicate event.
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.0.3
034 */
035@SuppressWarnings("serial")
036public class SessionDisconnectEvent extends AbstractSubProtocolEvent {
037
038        private final String sessionId;
039
040        private final CloseStatus status;
041
042
043        /**
044         * Create a new SessionDisconnectEvent.
045         * @param source the component that published the event (never {@code null})
046         * @param message the message
047         * @param sessionId the disconnect message
048         * @param closeStatus the status object
049         */
050        public SessionDisconnectEvent(Object source, Message<byte[]> message, String sessionId,
051                        CloseStatus closeStatus) {
052
053                this(source, message, sessionId, closeStatus, null);
054        }
055
056        /**
057         * Create a new SessionDisconnectEvent.
058         * @param source the component that published the event (never {@code null})
059         * @param message the message
060         * @param sessionId the disconnect message
061         * @param closeStatus the status object
062         * @param user the current session user
063         */
064        public SessionDisconnectEvent(Object source, Message<byte[]> message, String sessionId,
065                        CloseStatus closeStatus, Principal user) {
066
067                super(source, message, user);
068                Assert.notNull(sessionId, "Session id must not be null");
069                this.sessionId = sessionId;
070                this.status = closeStatus;
071        }
072
073
074        /**
075         * Return the session id.
076         */
077        public String getSessionId() {
078                return this.sessionId;
079        }
080
081        /**
082         * Return the status with which the session was closed.
083         */
084        public CloseStatus getCloseStatus() {
085                return this.status;
086        }
087
088
089        @Override
090        public String toString() {
091                return "SessionDisconnectEvent[sessionId=" + this.sessionId + ", " +
092                                (this.status != null ? this.status.toString() : "closeStatus=null") + "]";
093        }
094
095}