View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.messaging;
18  
19  import org.springframework.messaging.Message;
20  import org.springframework.util.Assert;
21  import org.springframework.web.socket.CloseStatus;
22  
23  /**
24   * Event raised when the session of a WebSocket client using a Simple Messaging
25   * Protocol (e.g. STOMP) as the WebSocket sub-protocol is closed.
26   *
27   * <p>Note that this event may be raised more than once for a single session and
28   * therefore event consumers should be idempotent and ignore a duplicate event.
29   *
30   * @author Rossen Stoyanchev
31   * @since 4.0.3
32   */
33  @SuppressWarnings("serial")
34  public class SessionDisconnectEvent extends AbstractSubProtocolEvent {
35  
36  	private final String sessionId;
37  
38  	private final CloseStatus status;
39  
40  
41  	/**
42  	 * Create a new SessionDisconnectEvent.
43  	 * @param source the component that published the event (never {@code null})
44  	 * @param message the message
45  	 * @param sessionId the disconnect message
46  	 * @param closeStatus the status object
47  	 */
48  	public SessionDisconnectEvent(Object source, Message<byte[]> message, String sessionId, CloseStatus closeStatus) {
49  		super(source, message);
50  		Assert.notNull(sessionId, "'sessionId' must not be null");
51  		this.sessionId = sessionId;
52  		this.status = closeStatus;
53  	}
54  
55  
56  	/**
57  	 * Return the session id.
58  	 */
59  	public String getSessionId() {
60  		return this.sessionId;
61  	}
62  
63  	/**
64  	 * Return the status with which the session was closed.
65  	 */
66  	public CloseStatus getCloseStatus() {
67  		return this.status;
68  	}
69  
70  	@Override
71  	public String toString() {
72  		return "SessionDisconnectEvent[sessionId=" + this.sessionId + ", " +
73  				(this.status != null ? this.status.toString() : "closeStatus=null") + "]";
74  	}
75  
76  }