Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2014 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.messaging.simp.user;
 
 import java.util.Set;
 
 
A default implementation of UserDestinationResolver that relies on the UserSessionRegistry provided to the constructor to find the sessionIds associated with a user and then uses the sessionId to make the target destination unique.

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(s):
Rossen Stoyanchev
Since:
4.0
 
 
 	private static final Log logger = LogFactory.getLog(DefaultUserDestinationResolver.class);
 
 
 
 	private String destinationPrefix = "/user/";


Create an instance that will access user session id information through the provided registry.

Parameters:
userSessionRegistry the registry, never null
 
 	public DefaultUserDestinationResolver(UserSessionRegistry userSessionRegistry) {
 		Assert.notNull(userSessionRegistry"'userSessionRegistry' must not be null");
 		this. = userSessionRegistry;
 	}


The prefix used to identify user destinations. Any destinations that do not start with the given prefix are not be resolved.

The default value is "/user/".

Parameters:
prefix the prefix to use
 
 	public void setUserDestinationPrefix(String prefix) {
 		Assert.hasText(prefix"prefix must not be empty");
 		this. = prefix.endsWith("/") ? prefix : prefix + "/";
 	}

Return the prefix used to identify user destinations. Any destinations that do not start with the given prefix are not be resolved.

By default "/user/queue/".

 
 		return this.;
 	}


Return the configured UserSessionRegistry.
 
 		return this.;
	}
		SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
		if (info == null) {
			return null;
		}
		Set<StringtargetDestinations = new HashSet<String>();
		for (String sessionId : info.getSessionIds()) {
			targetDestinations.add(getTargetDestination(
					headers.getDestination(), info.getDestinationWithoutPrefix(), sessionIdinfo.getUser()));
		}
		return new UserDestinationResult(headers.getDestination(),
				targetDestinationsinfo.getSubscribeDestination(), info.getUser());
	}
		String destination = headers.getDestination();
		Principal principal = headers.getUser();
		SimpMessageType messageType = headers.getMessageType();
		String destinationWithoutPrefix;
		String subscribeDestination;
		String user;
		Set<StringsessionIds;
		if (..equals(messageType) || ..equals(messageType)) {
			if (!checkDestination(destinationthis.)) {
				return null;
			}
			if (principal == null) {
				.error("Ignoring message, no principal info available");
				return null;
			}
			if (headers.getSessionId() == null) {
				.error("Ignoring message, no session id available");
				return null;
			}
			destinationWithoutPrefix = destination.substring(this..length()-1);
			subscribeDestination = destination;
			user = principal.getName();
			sessionIds = Collections.singleton(headers.getSessionId());
		}
		else if (..equals(messageType)) {
			if (!checkDestination(destinationthis.)) {
				return null;
			}
			int startIndex = this..length();
			int endIndex = destination.indexOf('/'startIndex);
			Assert.isTrue(endIndex > 0, "Expected destination pattern \"/principal/{userId}/**\"");
			destinationWithoutPrefix = destination.substring(endIndex);
			subscribeDestination = this..substring(0, startIndex-1) + destinationWithoutPrefix;
			user = destination.substring(startIndexendIndex);
			user = StringUtils.replace(user"%2F""/");
			sessionIds = this..getSessionIds(user);
		}
		else {
				.trace("Ignoring " + messageType + " message");
			}
			return null;
		}
		return new DestinationInfo(destinationWithoutPrefixsubscribeDestinationusersessionIds);
	}
	protected boolean checkDestination(String destinationString requiredPrefix) {
		if (destination == null) {
			.trace("Ignoring message, no destination");
			return false;
		}
		if (!destination.startsWith(requiredPrefix)) {
				.trace("Ignoring message to " + destination + ", not a \"user\" destination");
			}
			return false;
		}
		return true;
	}

Return the target destination to use. Provided as input are the original source destination, as well as the same destination with the target prefix removed.

Parameters:
sourceDestination the source destination from the input message
sourceDestinationWithoutPrefix the source destination with the target prefix removed
sessionId an active user session id
user the user
Returns:
the target destination
	protected String getTargetDestination(String sourceDestination,
			String sourceDestinationWithoutPrefixString sessionIdString user) {
		return sourceDestinationWithoutPrefix + "-user" + sessionId;
	}
	private static class DestinationInfo {
		private final String subscribeDestination;
		private final String user;
		private final Set<StringsessionIds;
		public DestinationInfo(String destinationWithoutPrefixString subscribeDestinationString user,
				Set<StringsessionIds) {
			this. = user;
			this. = destinationWithoutPrefix;
			this. = subscribeDestination;
			this. = sessionIds;
		}
		}
		}
		public String getUser() {
			return this.;
		}
		public Set<StringgetSessionIds() {
			return this.;
		}
	}
New to GrepCode? Check out our FAQ X