001/* 002 * Copyright 2002-2014 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.messaging.Message; 023import org.springframework.util.Assert; 024 025/** 026 * A base class for events for a message received from a WebSocket client and 027 * parsed into a higher-level sub-protocol (e.g. STOMP). 028 * 029 * @author Rossen Stoyanchev 030 * @since 4.1 031 */ 032@SuppressWarnings("serial") 033public abstract class AbstractSubProtocolEvent extends ApplicationEvent { 034 035 private final Message<byte[]> message; 036 037 private final Principal user; 038 039 040 /** 041 * Create a new AbstractSubProtocolEvent. 042 * @param source the component that published the event (never {@code null}) 043 * @param message the incoming message 044 */ 045 protected AbstractSubProtocolEvent(Object source, Message<byte[]> message) { 046 super(source); 047 Assert.notNull(message, "Message must not be null"); 048 this.message = message; 049 this.user = null; 050 } 051 052 /** 053 * Create a new AbstractSubProtocolEvent. 054 * @param source the component that published the event (never {@code null}) 055 * @param message the incoming message 056 */ 057 protected AbstractSubProtocolEvent(Object source, Message<byte[]> message, Principal user) { 058 super(source); 059 Assert.notNull(message, "Message must not be null"); 060 this.message = message; 061 this.user = user; 062 } 063 064 065 /** 066 * Return the Message associated with the event. Here is an example of 067 * obtaining information about the session id or any headers in the 068 * message: 069 * <pre class="code"> 070 * StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); 071 * headers.getSessionId(); 072 * headers.getSessionAttributes(); 073 * headers.getPrincipal(); 074 * </pre> 075 */ 076 public Message<byte[]> getMessage() { 077 return this.message; 078 } 079 080 /** 081 * Return the user for the session associated with the event. 082 */ 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}