Polishing

This commit is contained in:
Juergen Hoeller
2018-02-25 21:44:34 +01:00
parent 39ddd0f349
commit d4af8c86b2
6 changed files with 40 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -67,7 +67,7 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
* @param userRegistry the registry, never {@code null}
*/
public DefaultUserDestinationResolver(SimpUserRegistry userRegistry) {
Assert.notNull(userRegistry, "'userRegistry' must not be null");
Assert.notNull(userRegistry, "SimpUserRegistry must not be null");
this.userRegistry = userRegistry;
}
@@ -86,8 +86,8 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
* @param prefix the prefix to use
*/
public void setUserDestinationPrefix(String prefix) {
Assert.hasText(prefix, "prefix must not be empty");
this.prefix = prefix.endsWith("/") ? prefix : prefix + "/";
Assert.hasText(prefix, "Prefix must not be empty");
this.prefix = (prefix.endsWith("/") ? prefix : prefix + "/");
}
/**
@@ -101,11 +101,11 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
* Use this property to indicate whether the leading slash from translated
* user destinations should be removed or not. This depends on the
* destination prefixes the message broker is configured with.
* <p>By default this is set to {@code false}, i.e. "do not change the
* target destination", although
* <p>By default this is set to {@code false}, i.e.
* "do not change the target destination", although
* {@link org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration
* AbstractMessageBrokerConfiguration} may change that to {@code true} if
* the configured destinations do not have a leading slash.
* AbstractMessageBrokerConfiguration} may change that to {@code true}
* if the configured destinations do not have a leading slash.
* @param remove whether to remove the leading slash
* @since 4.3.14
*/
@@ -115,6 +115,7 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
/**
* Whether to remove the leading slash from target destinations.
* @since 4.3.14
*/
public boolean isRemoveLeadingSlash() {
return this.removeLeadingSlash;
@@ -198,18 +199,18 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
String user = (principal != null ? principal.getName() : null);
Set<String> sessionIds = Collections.singleton(sessionId);
return new ParseResult(sourceDestination, actualDestination, sourceDestination,
sessionIds, user);
return new ParseResult(sourceDestination, actualDestination, sourceDestination, sessionIds, user);
}
private ParseResult parseMessage(MessageHeaders headers, String sourceDestination) {
private ParseResult parseMessage(MessageHeaders headers, String sourceDest) {
int prefixEnd = this.prefix.length();
int userEnd = sourceDestination.indexOf('/', prefixEnd);
int userEnd = sourceDest.indexOf('/', prefixEnd);
Assert.isTrue(userEnd > 0, "Expected destination pattern \"/user/{userId}/**\"");
String actualDestination = sourceDestination.substring(userEnd);
String subscribeDestination = this.prefix.substring(0, prefixEnd - 1) + actualDestination;
String userName = sourceDestination.substring(prefixEnd, userEnd);
String actualDest = sourceDest.substring(userEnd);
String subscribeDest = this.prefix.substring(0, prefixEnd - 1) + actualDest;
String userName = sourceDest.substring(prefixEnd, userEnd);
userName = StringUtils.replace(userName, "%2F", "/");
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
Set<String> sessionIds;
if (userName.equals(sessionId)) {
@@ -219,11 +220,11 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
else {
sessionIds = getSessionIdsByUser(userName, sessionId);
}
if (isRemoveLeadingSlash()) {
actualDestination = actualDestination.substring(1);
actualDest = actualDest.substring(1);
}
return new ParseResult(sourceDestination, actualDestination, subscribeDestination,
sessionIds, userName);
return new ParseResult(sourceDest, actualDest, subscribeDest, sessionIds, userName);
}
private Set<String> getSessionIdsByUser(String userName, String sessionId) {
@@ -288,7 +289,6 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
private final String user;
public ParseResult(String sourceDest, String actualDest, String subscribeDest,
Set<String> sessionIds, String user) {
@@ -299,7 +299,6 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
this.user = user;
}
public String getSourceDestination() {
return this.sourceDestination;
}