Rename SessionIdGenerationStrategy to SessionIdGenerator

Closes gh-2391
This commit is contained in:
Marcus Da Coregio
2023-08-03 13:44:42 -03:00
parent b2615c2010
commit 2d4233fcfd
38 changed files with 269 additions and 286 deletions

View File

@@ -74,8 +74,7 @@ public final class MapSession implements Session, Serializable {
*/
private Duration maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL;
private transient SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy
.getInstance();
private transient SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Creates a new instance with a secure randomly generated identifier.
@@ -85,14 +84,14 @@ public final class MapSession implements Session, Serializable {
}
/**
* Creates a new instance using the specified {@link SessionIdGenerationStrategy} to
* generate the session id.
* @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use.
* Creates a new instance using the specified {@link SessionIdGenerator} to generate
* the session id.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use.
* @since 3.2
*/
public MapSession(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
this(sessionIdGenerationStrategy.generate());
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public MapSession(SessionIdGenerator sessionIdGenerator) {
this(sessionIdGenerator.generate());
this.sessionIdGenerator = sessionIdGenerator;
}
/**
@@ -155,7 +154,7 @@ public final class MapSession implements Session, Serializable {
@Override
public String changeSessionId() {
String changedId = this.sessionIdGenerationStrategy.generate();
String changedId = this.sessionIdGenerator.generate();
setId(changedId);
return changedId;
}
@@ -247,13 +246,12 @@ public final class MapSession implements Session, Serializable {
}
/**
* Sets the {@link SessionIdGenerationStrategy} to use when generating a new session
* id.
* @param sessionIdGenerationStrategy the {@link SessionIdGenerationStrategy} to use.
* Sets the {@link SessionIdGenerator} to use when generating a new session id.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use.
* @since 3.2
*/
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
this.sessionIdGenerator = sessionIdGenerator;
}
private static final long serialVersionUID = 7160779239673823561L;

View File

@@ -43,7 +43,7 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
private final Map<String, Session> sessions;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Creates a new instance backed by the provided {@link java.util.Map}. This allows
@@ -74,7 +74,7 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
this.sessions.remove(session.getOriginalId());
}
MapSession saved = new MapSession(session);
saved.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy);
saved.setSessionIdGenerator(this.sessionIdGenerator);
this.sessions.put(session.getId(), saved);
}
@@ -89,7 +89,7 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
return null;
}
MapSession result = new MapSession(saved);
result.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy);
result.setSessionIdGenerator(this.sessionIdGenerator);
return result;
}
@@ -100,14 +100,14 @@ public class MapSessionRepository implements SessionRepository<MapSession> {
@Override
public MapSession createSession() {
MapSession result = new MapSession(this.sessionIdGenerationStrategy);
MapSession result = new MapSession(this.sessionIdGenerator);
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
return result;
}
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
Assert.notNull(sessionIdGenerationStrategy, "sessionIdGenerationStrategy cannot be null");
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}
}

View File

@@ -45,7 +45,7 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository<M
private final Map<String, Session> sessions;
private SessionIdGenerationStrategy sessionIdGenerationStrategy = UuidSessionIdGenerationStrategy.getInstance();
private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance();
/**
* Creates a new instance backed by the provided {@link Map}. This allows injecting a
@@ -86,7 +86,7 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository<M
return Mono.defer(() -> Mono.justOrEmpty(this.sessions.get(id))
.filter((session) -> !session.isExpired())
.map(MapSession::new)
.doOnNext((session) -> session.setSessionIdGenerationStrategy(this.sessionIdGenerationStrategy))
.doOnNext((session) -> session.setSessionIdGenerator(this.sessionIdGenerator))
.switchIfEmpty(deleteById(id).then(Mono.empty())));
// @formatter:on
}
@@ -99,21 +99,20 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository<M
@Override
public Mono<MapSession> createSession() {
return Mono.defer(() -> {
MapSession result = new MapSession(this.sessionIdGenerationStrategy);
MapSession result = new MapSession(this.sessionIdGenerator);
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
return Mono.just(result);
});
}
/**
* Sets the {@link SessionIdGenerationStrategy} to use.
* @param sessionIdGenerationStrategy the non-null {@link SessionIdGenerationStrategy}
* to use
* Sets the {@link SessionIdGenerator} to use.
* @param sessionIdGenerator the non-null {@link SessionIdGenerator} to use
* @since 3.2
*/
public void setSessionIdGenerationStrategy(SessionIdGenerationStrategy sessionIdGenerationStrategy) {
Assert.notNull(sessionIdGenerationStrategy, "sessionIdGenerationStrategy cannot be null");
this.sessionIdGenerationStrategy = sessionIdGenerationStrategy;
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}
}

View File

@@ -24,7 +24,7 @@ import org.springframework.lang.NonNull;
* @author Marcus da Coregio
* @since 3.2
*/
public interface SessionIdGenerationStrategy {
public interface SessionIdGenerator {
@NonNull
String generate();

View File

@@ -21,17 +21,16 @@ import java.util.UUID;
import org.springframework.lang.NonNull;
/**
* A {@link SessionIdGenerationStrategy} that generates a random UUID to be used as the
* session id.
* A {@link SessionIdGenerator} that generates a random UUID to be used as the session id.
*
* @author Marcus da Coregio
* @since 3.2
*/
public final class UuidSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
public final class UuidSessionIdGenerator implements SessionIdGenerator {
private static final UuidSessionIdGenerationStrategy INSTANCE = new UuidSessionIdGenerationStrategy();
private static final UuidSessionIdGenerator INSTANCE = new UuidSessionIdGenerator();
private UuidSessionIdGenerationStrategy() {
private UuidSessionIdGenerator() {
}
@Override
@@ -41,10 +40,10 @@ public final class UuidSessionIdGenerationStrategy implements SessionIdGeneratio
}
/**
* Returns the singleton instance of {@link UuidSessionIdGenerationStrategy}.
* @return the singleton instance of {@link UuidSessionIdGenerationStrategy}
* Returns the singleton instance of {@link UuidSessionIdGenerator}.
* @return the singleton instance of {@link UuidSessionIdGenerator}
*/
public static UuidSessionIdGenerationStrategy getInstance() {
public static UuidSessionIdGenerator getInstance() {
return INSTANCE;
}

View File

@@ -45,7 +45,7 @@ class MapSessionTests {
@Test
void constructorWhenSessionIdGenerationStrategyThenUsesStrategy() {
MapSession session = new MapSession(new FixedSessionIdGenerationStrategy("my-id"));
MapSession session = new MapSession(new FixedSessionIdGenerator("my-id"));
assertThat(session.getId()).isEqualTo("my-id");
}
@@ -159,18 +159,18 @@ class MapSessionTests {
@Test
void changeSessionIdWhenSessionIdStrategyThenUsesStrategy() {
MapSession session = new MapSession(new IncrementalSessionIdGenerationStrategy());
MapSession session = new MapSession(new IncrementalSessionIdGenerator());
String idBeforeChange = session.getId();
String idAfterChange = session.changeSessionId();
assertThat(idBeforeChange).isEqualTo("1");
assertThat(idAfterChange).isEqualTo("2");
}
static class FixedSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
static class FixedSessionIdGenerator implements SessionIdGenerator {
private final String id;
FixedSessionIdGenerationStrategy(String id) {
FixedSessionIdGenerator(String id) {
this.id = id;
}
@@ -181,7 +181,7 @@ class MapSessionTests {
}
static class IncrementalSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
static class IncrementalSessionIdGenerator implements SessionIdGenerator {
private int counter = 1;

View File

@@ -154,7 +154,7 @@ class ReactiveMapSessionRepositoryTests {
@Test
void createSessionWhenSessionIdGenerationStrategyThenUses() {
this.repository.setSessionIdGenerationStrategy(() -> "test");
this.repository.setSessionIdGenerator(() -> "test");
MapSession session = this.repository.createSession().block();
assertThat(session.getId()).isEqualTo("test");
assertThat(session.changeSessionId()).isEqualTo("test");
@@ -162,13 +162,13 @@ class ReactiveMapSessionRepositoryTests {
@Test
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerationStrategy(null))
.withMessage("sessionIdGenerationStrategy cannot be null");
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerator(null))
.withMessage("sessionIdGenerator cannot be null");
}
@Test
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
this.repository.setSessionIdGenerationStrategy(() -> "test");
this.repository.setSessionIdGenerator(() -> "test");
MapSession session = this.repository.createSession().block();
this.repository.save(session).block();