Optimize DefaultUserDestinationResolver.resolveDestination()
Issue: SPR-15602
This commit is contained in:
		
							parent
							
								
									1412c2c577
								
							
						
					
					
						commit
						0ef1623643
					
				|  | @ -121,12 +121,12 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { | |||
| 
 | ||||
| 	@Override | ||||
| 	public UserDestinationResult resolveDestination(Message<?> message) { | ||||
| 		String sourceDestination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); | ||||
| 		ParseResult parseResult = parse(message); | ||||
| 		if (parseResult == null) { | ||||
| 			return null; | ||||
| 		} | ||||
| 		String user = parseResult.getUser(); | ||||
| 		String sourceDestination = parseResult.getSourceDestination(); | ||||
| 		Set<String> targetSet = new HashSet<>(); | ||||
| 		for (String sessionId : parseResult.getSessionIds()) { | ||||
| 			String actualDestination = parseResult.getActualDestination(); | ||||
|  | @ -142,40 +142,64 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { | |||
| 	@Nullable | ||||
| 	private ParseResult parse(Message<?> message) { | ||||
| 		MessageHeaders headers = message.getHeaders(); | ||||
| 		String destination = SimpMessageHeaderAccessor.getDestination(headers); | ||||
| 		if (destination == null || !checkDestination(destination, this.prefix)) { | ||||
| 		String sourceDestination = SimpMessageHeaderAccessor.getDestination(headers); | ||||
| 		if (sourceDestination == null || !checkDestination(sourceDestination, this.prefix)) { | ||||
| 			return null; | ||||
| 		} | ||||
| 		SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers); | ||||
| 		Principal principal = SimpMessageHeaderAccessor.getUser(headers); | ||||
| 		switch (messageType) { | ||||
| 			case SUBSCRIBE: | ||||
| 			case UNSUBSCRIBE: | ||||
| 				return parseSubscriptionMessage(message, headers, sourceDestination); | ||||
| 			case MESSAGE: | ||||
| 				return parseMessage(headers, sourceDestination); | ||||
| 			default: | ||||
| 				return null; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	private ParseResult parseSubscriptionMessage(Message<?> message, MessageHeaders headers, String sourceDestination) { | ||||
| 		String sessionId = SimpMessageHeaderAccessor.getSessionId(headers); | ||||
| 		if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) { | ||||
| 		if (sessionId == null) { | ||||
| 			logger.error("No session id. Ignoring " + message); | ||||
| 			return null; | ||||
| 		} | ||||
| 		int prefixEnd = this.prefix.length() - 1; | ||||
| 			String actualDestination = destination.substring(prefixEnd); | ||||
| 		String actualDestination = sourceDestination.substring(prefixEnd); | ||||
| 		if (!this.keepLeadingSlash) { | ||||
| 			actualDestination = actualDestination.substring(1); | ||||
| 		} | ||||
| 		Principal principal = SimpMessageHeaderAccessor.getUser(headers); | ||||
| 		String user = (principal != null ? principal.getName() : null); | ||||
| 			return new ParseResult(actualDestination, destination, Collections.singleton(sessionId), user); | ||||
| 		Set<String> sessionIds = Collections.singleton(sessionId); | ||||
| 		return new ParseResult(sourceDestination, actualDestination, sourceDestination, sessionIds, user); | ||||
| 	} | ||||
| 		else if (SimpMessageType.MESSAGE.equals(messageType)) { | ||||
| 
 | ||||
| 	private ParseResult parseMessage(MessageHeaders headers, String sourceDestination) { | ||||
| 		int prefixEnd = this.prefix.length(); | ||||
| 			int userEnd = destination.indexOf('/', prefixEnd); | ||||
| 		int userEnd = sourceDestination.indexOf('/', prefixEnd); | ||||
| 		Assert.isTrue(userEnd > 0, "Expected destination pattern \"/user/{userId}/**\""); | ||||
| 			String actualDestination = destination.substring(userEnd); | ||||
| 		String actualDestination = sourceDestination.substring(userEnd); | ||||
| 		String subscribeDestination = this.prefix.substring(0, prefixEnd - 1) + actualDestination; | ||||
| 			String userName = destination.substring(prefixEnd, userEnd); | ||||
| 		String userName = sourceDestination.substring(prefixEnd, userEnd); | ||||
| 		userName = StringUtils.replace(userName, "%2F", "/"); | ||||
| 		String sessionId = SimpMessageHeaderAccessor.getSessionId(headers); | ||||
| 		Set<String> sessionIds; | ||||
| 		if (userName.equals(sessionId)) { | ||||
| 			userName = null; | ||||
| 			sessionIds = Collections.singleton(sessionId); | ||||
| 		} | ||||
| 		else { | ||||
| 			sessionIds = getSessionIdsByUser(userName, sessionId); | ||||
| 		} | ||||
| 		if (!this.keepLeadingSlash) { | ||||
| 			actualDestination = actualDestination.substring(1); | ||||
| 		} | ||||
| 		return new ParseResult(sourceDestination, actualDestination, subscribeDestination, sessionIds, userName); | ||||
| 	} | ||||
| 
 | ||||
| 	private Set<String> getSessionIdsByUser(String userName, String sessionId) { | ||||
| 		Set<String> sessionIds; | ||||
| 		SimpUser user = this.userRegistry.getUser(userName); | ||||
| 		if (user != null) { | ||||
| 			if (user.getSession(sessionId) != null) { | ||||
|  | @ -192,15 +216,7 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { | |||
| 		else { | ||||
| 			sessionIds = Collections.emptySet(); | ||||
| 		} | ||||
| 			} | ||||
| 			if (!this.keepLeadingSlash) { | ||||
| 				actualDestination = actualDestination.substring(1); | ||||
| 			} | ||||
| 			return new ParseResult(actualDestination, subscribeDestination, sessionIds, userName); | ||||
| 		} | ||||
| 		else { | ||||
| 			return null; | ||||
| 		} | ||||
| 		return sessionIds; | ||||
| 	} | ||||
| 
 | ||||
| 	protected boolean checkDestination(String destination, String requiredPrefix) { | ||||
|  | @ -243,8 +259,11 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { | |||
| 
 | ||||
| 		private final String user; | ||||
| 
 | ||||
| 		private final String sourceDestination; | ||||
| 
 | ||||
| 		public ParseResult(String actualDest, String subscribeDest, Set<String> sessionIds, String user) { | ||||
| 		public ParseResult(String sourceDest, String actualDest, String subscribeDest, | ||||
| 				Set<String> sessionIds, String user) { | ||||
| 			this.sourceDestination = sourceDest; | ||||
| 			this.actualDestination = actualDest; | ||||
| 			this.subscribeDestination = subscribeDest; | ||||
| 			this.sessionIds = sessionIds; | ||||
|  | @ -267,6 +286,10 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver { | |||
| 		public String getUser() { | ||||
| 			return this.user; | ||||
| 		} | ||||
| 
 | ||||
| 		public String getSourceDestination() { | ||||
| 			return this.sourceDestination; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue