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