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.user;
018
019import java.util.Set;
020
021import org.springframework.util.Assert;
022
023/**
024 * Contains the result from parsing a "user" destination from a source message
025 * and translating it to target destinations (one per active user session).
026 *
027 * @author Rossen Stoyanchev
028 * @since 4.0.2
029 * @see org.springframework.messaging.simp.user.UserDestinationResolver
030 */
031public class UserDestinationResult {
032
033        private final String sourceDestination;
034
035        private final Set<String> targetDestinations;
036
037        private final String subscribeDestination;
038
039        private final String user;
040
041
042        public UserDestinationResult(String sourceDestination, Set<String> targetDestinations,
043                        String subscribeDestination, String user) {
044
045                Assert.notNull(sourceDestination, "'sourceDestination' must not be null");
046                Assert.notNull(targetDestinations, "'targetDestinations' must not be null");
047                Assert.notNull(subscribeDestination, "'subscribeDestination' must not be null");
048
049                this.sourceDestination = sourceDestination;
050                this.targetDestinations = targetDestinations;
051                this.subscribeDestination = subscribeDestination;
052                this.user = user;
053        }
054
055
056        /**
057         * The "user" destination from the source message. This may look like
058         * "/user/queue/position-updates" when subscribing or
059         * "/user/{username}/queue/position-updates" when sending a message.
060         * @return the "user" destination, never {@code null}.
061         */
062        public String getSourceDestination() {
063                return this.sourceDestination;
064        }
065
066        /**
067         * The target destinations that the source destination was translated to,
068         * one per active user session, e.g. "/queue/position-updates-useri9oqdfzo".
069         * @return the target destinations, never {@code null} but possibly an empty
070         * set if there are no active sessions for the user.
071         */
072        public Set<String> getTargetDestinations() {
073                return this.targetDestinations;
074        }
075
076        /**
077         * The user destination in the form expected when a client subscribes, e.g.
078         * "/user/queue/position-updates".
079         * @return the subscribe form of the "user" destination, never {@code null}.
080         */
081        public String getSubscribeDestination() {
082                return this.subscribeDestination;
083        }
084
085        /**
086         * The user for this user destination.
087         * @return the user name or {@code null} if we have a session id only such as
088         * when the user is not authenticated; in such cases it is possible to use
089         * sessionId in place of a user name thus removing the need for a user-to-session
090         * lookup via {@link SimpUserRegistry}.
091         */
092        public String getUser() {
093                return this.user;
094        }
095
096
097        @Override
098        public String toString() {
099                return "UserDestinationResult [source=" + this.sourceDestination + ", target=" + this.targetDestinations +
100                                ", subscribeDestination=" + this.subscribeDestination + ", user=" + this.user + "]";
101        }
102
103}