Polish contribution

Closes gh-919
This commit is contained in:
Vedran Pavic
2017-11-10 22:55:14 +01:00
parent 5f23a41674
commit 36d349f328
8 changed files with 36 additions and 60 deletions

View File

@@ -43,9 +43,9 @@ public class ReactiveRedisOperationsSessionRepository implements
ReactiveSessionRepository<ReactiveRedisOperationsSessionRepository.RedisSession> {
/**
* The default prefix for each key and channel in Redis used by Spring Session.
* The default namespace for each key and channel in Redis used by Spring Session.
*/
static final String DEFAULT_SPRING_SESSION_REDIS_PREFIX = "spring:session:";
public static final String DEFAULT_NAMESPACE = "spring:session:";
/**
* The key in the Hash representing {@link Session#getCreationTime()}.
@@ -73,9 +73,9 @@ public class ReactiveRedisOperationsSessionRepository implements
private final ReactiveRedisOperations<String, Object> sessionRedisOperations;
/**
* The prefix for every key used by Spring Session in Redis.
* The namespace for every key used by Spring Session in Redis.
*/
private String keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX;
private String namespace = DEFAULT_NAMESPACE;
/**
* If non-null, this value is used to override the default value for
@@ -93,7 +93,7 @@ public class ReactiveRedisOperationsSessionRepository implements
public void setRedisKeyNamespace(String namespace) {
Assert.hasText(namespace, "namespace cannot be null or empty");
this.keyPrefix = namespace.trim() + ":";
this.namespace = namespace.trim() + ":";
}
/**
@@ -167,7 +167,7 @@ public class ReactiveRedisOperationsSessionRepository implements
}
private String getSessionKey(String sessionId) {
return this.keyPrefix + "sessions:" + sessionId;
return this.namespace + "sessions:" + sessionId;
}
/**

View File

@@ -253,9 +253,9 @@ public class RedisOperationsSessionRepository implements
static PrincipalNameResolver PRINCIPAL_NAME_RESOLVER = new PrincipalNameResolver();
/**
* The default prefix for each key and channel in Redis used by Spring Session.
* The default namespace for each key and channel in Redis used by Spring Session.
*/
static final String DEFAULT_SPRING_SESSION_REDIS_PREFIX = "spring:session:";
public static final String DEFAULT_NAMESPACE = "spring:session:";
/**
* The key in the Hash representing
@@ -285,9 +285,9 @@ public class RedisOperationsSessionRepository implements
static final String SESSION_ATTR_PREFIX = "sessionAttr:";
/**
* The prefix for every key used by Spring Session in Redis.
* The namespace for every key used by Spring Session in Redis.
*/
private String keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX;
private String namespace = DEFAULT_NAMESPACE;
private final RedisOperations<Object, Object> sessionRedisOperations;
@@ -589,7 +589,7 @@ public class RedisOperationsSessionRepository implements
public void setRedisKeyNamespace(String namespace) {
Assert.hasText(namespace, "namespace cannot be null or empty");
this.keyPrefix = namespace.trim() + ":";
this.namespace = namespace.trim() + ":";
}
/**
@@ -599,17 +599,17 @@ public class RedisOperationsSessionRepository implements
* @return the Hash key for this session by prefixing it appropriately.
*/
String getSessionKey(String sessionId) {
return this.keyPrefix + "sessions:" + sessionId;
return this.namespace + "sessions:" + sessionId;
}
String getPrincipalKey(String principalName) {
return this.keyPrefix + "index:"
return this.namespace + "index:"
+ FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME + ":"
+ principalName;
}
String getExpirationsKey(long expiration) {
return this.keyPrefix + "expirations:" + expiration;
return this.namespace + "expirations:" + expiration;
}
private String getExpiredKey(String sessionId) {
@@ -621,7 +621,7 @@ public class RedisOperationsSessionRepository implements
}
private String getExpiredKeyPrefix() {
return this.keyPrefix + "sessions:" + "expires:";
return this.namespace + "sessions:" + "expires:";
}
/**
@@ -631,7 +631,7 @@ public class RedisOperationsSessionRepository implements
* @return the prefix for the channel that SessionCreatedEvent are published to
*/
public String getSessionCreatedChannelPrefix() {
return this.keyPrefix + "event:created:";
return this.namespace + "event:created:";
}
/**

View File

@@ -28,6 +28,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
/**
* Add this annotation to an {@code @Configuration} class to expose the
@@ -72,9 +73,8 @@ public @interface EnableRedisHttpSession {
/**
* <p>
* Defines a unique namespace for keys. The value is used to isolate sessions by
* changing the prefix from "spring:session:" to
* "spring:session:&lt;redisNamespace&gt;:". The default is "" such that all Redis
* keys begin with "spring:session".
* changing the prefix from default {@code spring:session:} to
* {@code <redisNamespace>:}.
* </p>
*
* <p>
@@ -85,7 +85,7 @@ public @interface EnableRedisHttpSession {
*
* @return the unique namespace for keys
*/
String redisNamespace() default "";
String redisNamespace() default RedisOperationsSessionRepository.DEFAULT_NAMESPACE;
/**
* <p>

View File

@@ -26,6 +26,7 @@ import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository;
import org.springframework.session.data.redis.RedisFlushMode;
/**
@@ -65,9 +66,8 @@ public @interface EnableRedisWebSession {
/**
* <p>
* Defines a unique namespace for keys. The value is used to isolate sessions by
* changing the prefix from {@code spring:session:} to
* {@code spring:session:<redisNamespace>:}. The default is "" such that all Redis
* keys begin with {@code spring:session:}.
* changing the prefix from default {@code spring:session:} to
* {@code <redisNamespace>:}.
* </p>
*
* <p>
@@ -78,7 +78,7 @@ public @interface EnableRedisWebSession {
*
* @return the unique namespace for keys
*/
String redisNamespace() default "";
String redisNamespace() default ReactiveRedisOperationsSessionRepository.DEFAULT_NAMESPACE;
/**
* <p>

View File

@@ -85,7 +85,8 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
public void customRedisKeyNamespace() {
this.repository.setRedisKeyNamespace("test");
assertThat(ReflectionTestUtils.getField(this.repository, "keyPrefix")).isEqualTo("test:");
assertThat(ReflectionTestUtils.getField(this.repository, "namespace"))
.isEqualTo("test:");
}
@Test

View File

@@ -16,7 +16,6 @@
package org.springframework.session.data.redis;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
@@ -710,26 +709,8 @@ public class RedisOperationsSessionRepositoryTests {
this.redisRepository.setRedisFlushMode(null);
}
@Test
public void testDefaultRedisNamespace() {
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
session.setMaxInactiveInterval(Duration.ZERO);
given(this.redisOperations.boundHashOps(anyString()))
.willReturn(this.boundHashOperations);
given(this.redisOperations.boundSetOps(anyString()))
.willReturn(this.boundSetOperations);
this.redisRepository.save(session);
String id = session.getId();
verify(this.redisOperations, atLeastOnce())
.delete(getKey("expires:" + id));
verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id));
}
@Test
public void testRedisNamespaceChange() {
public void changeRedisNamespace() {
String namespace = "foo:bar";
this.redisRepository.setRedisKeyNamespace(namespace);
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
@@ -743,29 +724,23 @@ public class RedisOperationsSessionRepositoryTests {
String id = session.getId();
verify(this.redisOperations, atLeastOnce())
.delete(getKeyWithinNamespace(namespace, "expires:" + id));
verify(this.redisOperations, never()).boundValueOps(getKeyWithinNamespace(namespace, "expires:" + id));
.delete(namespace + ":sessions:expires:" + id);
verify(this.redisOperations, never())
.boundValueOps(namespace + ":sessions:expires:" + id);
}
@Test(expected = IllegalArgumentException.class)
public void testLaunchExceptionOnNullNamespace() {
public void setRedisKeyNamespaceNullNamespace() {
this.redisRepository.setRedisKeyNamespace(null);
}
@Test(expected = IllegalArgumentException.class)
public void testLaunchExceptionOnEmptyNamespace() {
this.redisRepository.setRedisKeyNamespace("");
public void setRedisKeyNamespaceEmptyNamespace() {
this.redisRepository.setRedisKeyNamespace(" ");
}
private String getKey(String id) {
return getKeyWithinNamespace("spring:session", id);
}
private String getKeyWithinNamespace(String prefix, String id) {
if (prefix.endsWith(":")) {
prefix = prefix.substring(0, prefix.length());
}
return MessageFormat.format("{0}:sessions:{1}", prefix, id);
return "spring:session:sessions:" + id;
}
private Map map(Object... objects) {

View File

@@ -81,7 +81,7 @@ public class RedisWebSessionConfigurationTests {
ReactiveRedisOperationsSessionRepository repository = this.context
.getBean(ReactiveRedisOperationsSessionRepository.class);
assertThat(repository).isNotNull();
assertThat(ReflectionTestUtils.getField(repository, "keyPrefix"))
assertThat(ReflectionTestUtils.getField(repository, "namespace"))
.isEqualTo(REDIS_NAMESPACE + ":");
}