Browse Source

Polish

pull/449/head
Rossen Stoyanchev 12 years ago
parent
commit
2cafe9d73a
  1. 26
      spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java
  2. 29
      spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java

26
spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java

@ -30,11 +30,9 @@ import java.util.Set; @@ -30,11 +30,9 @@ import java.util.Set;
/**
* A default implementation of {@link UserDestinationResolver}.
*
* <p>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)}.
* <p>
* 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 { @@ -111,26 +109,26 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
String targetUser;
String targetDestination;
Set<String> sessionIds;
Set<String> 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 { @@ -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 { @@ -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) {

29
spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResolver.java

@ -24,12 +24,17 @@ import java.util.Set; @@ -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.
*
* <p>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".
* <p>
* 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".
* <p>
* 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; @@ -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<String> resolveDestination(Message<?> message);

Loading…
Cancel
Save