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