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