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.messaging.simp; 018 019import java.util.Map; 020 021import org.springframework.lang.Nullable; 022import org.springframework.messaging.MessagingException; 023import org.springframework.messaging.core.MessagePostProcessor; 024import org.springframework.messaging.core.MessageSendingOperations; 025 026/** 027 * A specialization of {@link MessageSendingOperations} with methods for use with 028 * the Spring Framework support for Simple Messaging Protocols (like STOMP). 029 * 030 * <p>For more on user destinations see 031 * {@link org.springframework.messaging.simp.user.UserDestinationResolver 032 * UserDestinationResolver}. 033 * 034 * <p>Generally it is expected the user is the one authenticated with the 035 * WebSocket session (or by extension the user authenticated with the 036 * handshake request that started the session). However if the session is 037 * not authenticated, it is also possible to pass the session id (if known) 038 * in place of the user name. Keep in mind though that in that scenario, 039 * you must use one of the overloaded methods that accept headers making sure the 040 * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#setSessionId 041 * sessionId} header has been set accordingly. 042 * 043 * @author Rossen Stoyanchev 044 * @since 4.0 045 */ 046public interface SimpMessageSendingOperations extends MessageSendingOperations<String> { 047 048 /** 049 * Send a message to the given user. 050 * @param user the user that should receive the message. 051 * @param destination the destination to send the message to. 052 * @param payload the payload to send 053 */ 054 void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException; 055 056 /** 057 * Send a message to the given user. 058 * <p>By default headers are interpreted as native headers (e.g. STOMP) and 059 * are saved under a special key in the resulting Spring 060 * {@link org.springframework.messaging.Message Message}. In effect when the 061 * message leaves the application, the provided headers are included with it 062 * and delivered to the destination (e.g. the STOMP client or broker). 063 * <p>If the map already contains the key 064 * {@link org.springframework.messaging.support.NativeMessageHeaderAccessor#NATIVE_HEADERS "nativeHeaders"} 065 * or was prepared with 066 * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor SimpMessageHeaderAccessor} 067 * then the headers are used directly. A common expected case is providing a 068 * content type (to influence the message conversion) and native headers. 069 * This may be done as follows: 070 * <pre class="code"> 071 * SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); 072 * accessor.setContentType(MimeTypeUtils.TEXT_PLAIN); 073 * accessor.setNativeHeader("foo", "bar"); 074 * accessor.setLeaveMutable(true); 075 * MessageHeaders headers = accessor.getMessageHeaders(); 076 * messagingTemplate.convertAndSendToUser(user, destination, payload, headers); 077 * </pre> 078 * <p><strong>Note:</strong> if the {@code MessageHeaders} are mutable as in 079 * the above example, implementations of this interface should take notice and 080 * update the headers in the same instance (rather than copy or re-create it) 081 * and then set it immutable before sending the final message. 082 * @param user the user that should receive the message (must not be {@code null}) 083 * @param destination the destination to send the message to (must not be {@code null}) 084 * @param payload the payload to send (may be {@code null}) 085 * @param headers the message headers (may be {@code null}) 086 */ 087 void convertAndSendToUser(String user, String destination, Object payload, Map<String, Object> headers) 088 throws MessagingException; 089 090 /** 091 * Send a message to the given user. 092 * @param user the user that should receive the message (must not be {@code null}) 093 * @param destination the destination to send the message to (must not be {@code null}) 094 * @param payload the payload to send (may be {@code null}) 095 * @param postProcessor a postProcessor to post-process or modify the created message 096 */ 097 void convertAndSendToUser(String user, String destination, Object payload, MessagePostProcessor postProcessor) 098 throws MessagingException; 099 100 /** 101 * Send a message to the given user. 102 * <p>See {@link #convertAndSend(Object, Object, java.util.Map)} for important 103 * notes regarding the input headers. 104 * @param user the user that should receive the message 105 * @param destination the destination to send the message to 106 * @param payload the payload to send 107 * @param headers the message headers 108 * @param postProcessor a postProcessor to post-process or modify the created message 109 */ 110 void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers, 111 @Nullable MessagePostProcessor postProcessor) throws MessagingException; 112 113}