From 2cafe9d73afe81ff15152ce9185a50a14b9558c0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 21 Jan 2014 10:33:22 -0500 Subject: [PATCH] Polish --- .../user/DefaultUserDestinationResolver.java | 26 ++++++++--------- .../simp/user/UserDestinationResolver.java | 29 ++++++++++++------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java index 305db97bab7..75f4146bb0e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java @@ -30,11 +30,9 @@ import java.util.Set; /** * A default implementation of {@link UserDestinationResolver}. - * - *

Resolves messages sent to destination patterns "/user/{user-name}/**" as well as - * subscriptions to destinations "/user/queue/**" where the "/user/" prefix used to - * recognize such destinations is customizable via - * {@link #setUserDestinationPrefix(String)}. + *

+ * Uses the {@link org.springframework.messaging.simp.user.UserSessionRegistry} + * provided to the constructor to find the sessionIds associated with a user. * * @author Rossen Stoyanchev * @since 4.0 @@ -111,26 +109,26 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { String targetUser; String targetDestination; - Set sessionIds; + Set targetSessionIds; - Principal user = headers.getUser(); + Principal principal = headers.getUser(); SimpMessageType messageType = headers.getMessageType(); if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) { if (!checkDestination(destination, this.destinationPrefix)) { return null; } - if (user == null) { - logger.error("Ignoring message, no user info available"); + if (principal == null) { + logger.error("Ignoring message, no principal info available"); return null; } if (headers.getSessionId() == null) { logger.error("Ignoring message, no session id available"); return null; } - targetUser = user.getName(); + targetUser = principal.getName(); targetDestination = destination.substring(this.destinationPrefix.length()-1); - sessionIds = Collections.singleton(headers.getSessionId()); + targetSessionIds = Collections.singleton(headers.getSessionId()); } else if (SimpMessageType.MESSAGE.equals(messageType)) { if (!checkDestination(destination, this.destinationPrefix)) { @@ -138,10 +136,10 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { } int startIndex = this.destinationPrefix.length(); int endIndex = destination.indexOf('/', startIndex); - Assert.isTrue(endIndex > 0, "Expected destination pattern \"/user/{userId}/**\""); + Assert.isTrue(endIndex > 0, "Expected destination pattern \"/principal/{userId}/**\""); targetUser = destination.substring(startIndex, endIndex); targetDestination = destination.substring(endIndex); - sessionIds = this.userSessionRegistry.getSessionIds(targetUser); + targetSessionIds = this.userSessionRegistry.getSessionIds(targetUser); } else { if (logger.isTraceEnabled()) { @@ -150,7 +148,7 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { return null; } - return new UserDestinationInfo(targetUser, targetDestination, sessionIds); + return new UserDestinationInfo(targetUser, targetDestination, targetSessionIds); } protected boolean checkDestination(String destination, String requiredPrefix) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java index 66843251fba..f722c434c0a 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java @@ -24,12 +24,17 @@ import java.util.Set; * A strategy for resolving unique, user destinations per session. User destinations * provide a user with the ability to subscribe to a queue unique to their session * as well others with the ability to send messages to those queues. - * - *

For example when a user attempts to subscribe to "/user/queue/position-updates", - * the destination may be resolved to "/queue/position-updates-useri9oqdfzo" yielding a - * unique queue name that does not collide with any other user attempting to do the same. - * Subsequently when messages are sent to "/user/{username}/queue/position-updates", - * the destination is translated to "/queue/position-updates-useri9oqdfzo". + *

+ * When a user attempts to subscribe to "/user/queue/position-updates", the + * "/user" prefix is removed and a unique suffix added, resulting in something + * like "/queue/position-updates-useri9oqdfzo" where the suffix is based on the + * user's session and ensures it does not collide with any other users attempting + * to subscribe to "/user/queue/position-updates". + *

+ * When a message is sent to a user with a destination such as + * "/user/{username}/queue/position-updates", the "/user/{username}" prefix is + * removed and the suffix added, resulting in something like + * "/queue/position-updates-useri9oqdfzo". * * @author Rossen Stoyanchev * @since 4.0 @@ -39,12 +44,14 @@ import java.util.Set; public interface UserDestinationResolver { /** - * Resolve the destination of the message to one or more user/session-specific target - * destinations. If the user has multiple sessions, the method may return more than - * one target destinations. + * Resolve the destination of the message to a set of actual target destinations + * to use. If the message is SUBSCRIBE/UNSUBSCRIBE, the returned set will contain + * only target destination. If the message represents data being sent to a user, + * the returned set may contain multiple target destinations, one for each active + * session of the target user. + * * @param message the message to resolve - * @return the resolved unique user destinations or an empty Set if the message - * destination is not recognized as a user destination + * @return the resolved unique user destination(s) or an empty Set */ Set resolveDestination(Message message);