From aab4ebc51b2033ff169e5237b6753474d630e6a2 Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Thu, 7 Nov 2019 11:42:12 +0100
Subject: [PATCH 1/6] Add support for creating session with externally
specified id
(cherry picked from commit 4eecb733b7f8146a6a13222f31278e51bda2a86a)
---
.../CreateWithIdSessionRepository.java | 43 +++++++++++++++++++
.../redis/RedisIndexedSessionRepository.java | 16 ++++++-
.../HazelcastIndexedSessionRepository.java | 22 +++++++---
.../jdbc/JdbcIndexedSessionRepository.java | 16 ++++++-
4 files changed, 88 insertions(+), 9 deletions(-)
create mode 100644 spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
diff --git a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
new file mode 100644
index 000000000..355839a41
--- /dev/null
+++ b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2014-2019 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
+ *
+ * https://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.session;
+
+/**
+ * Extends a basic {@link SessionRepository} to allow creating session with specific id
+ *
+ * @param the type of Session being managed by this
+ * {@link CreateWithIdSessionRepository}
+ * @author Jakub Maciej
+ */
+public interface CreateWithIdSessionRepository extends SessionRepository {
+
+ /**
+ * Creates a new {@link Session} that is capable of being persisted by this
+ * {@link SessionRepository}.
+ *
+ *
+ * This allows optimizations and customizations in how the {@link Session} is
+ * persisted. For example, the implementation returned might keep track of the changes
+ * ensuring that only the delta needs to be persisted on a save.
+ *
+ * @return a new {@link Session} that is capable of being persisted by this
+ * {@link SessionRepository}
+ * @param id a desired session id
+ */
+ S createSession(String id);
+
+}
diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
index 46442683e..82b145d95 100644
--- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
+++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
@@ -38,6 +38,7 @@
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
@@ -244,10 +245,12 @@
*
* @author Rob Winch
* @author Vedran Pavic
+ * @author Jakub Maciej
* @since 2.2.0
*/
public class RedisIndexedSessionRepository
- implements FindByIndexNameSessionRepository, MessageListener {
+ implements FindByIndexNameSessionRepository,
+ CreateWithIdSessionRepository, MessageListener {
private static final Log logger = LogFactory.getLog(RedisIndexedSessionRepository.class);
@@ -498,6 +501,17 @@ public RedisSession createSession() {
return session;
}
+ @Override
+ public RedisSession createSession(final String id) {
+ MapSession cached = new MapSession(id);
+ if (this.defaultMaxInactiveInterval != null) {
+ cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
+ }
+ RedisSession session = new RedisSession(cached, true);
+ session.flushImmediateIfNecessary();
+ return session;
+ }
+
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] messageChannel = message.getChannel();
diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
index 4de118c64..32f007d88 100644
--- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
+++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
@@ -41,6 +41,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
@@ -108,10 +109,12 @@
* @author Tommy Ludwig
* @author Mark Anderson
* @author Aleksandar Stojsavljevic
+ * @author Jakub Maciej
* @since 2.2.0
*/
public class HazelcastIndexedSessionRepository
implements FindByIndexNameSessionRepository,
+ CreateWithIdSessionRepository,
EntryAddedListener, EntryEvictedListener,
EntryRemovedListener {
@@ -243,6 +246,17 @@ public HazelcastSession createSession() {
return session;
}
+ @Override
+ public HazelcastSession createSession(final String id) {
+ MapSession cached = new MapSession(id);
+ if (this.defaultMaxInactiveInterval != null) {
+ cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
+ }
+ HazelcastSession session = new HazelcastSession(cached, true);
+ session.flushImmediateIfNecessary();
+ return session;
+ }
+
@Override
public void save(HazelcastSession session) {
if (session.isNew) {
@@ -338,15 +352,9 @@ public void entryRemoved(EntryEvent event) {
this.eventPublisher.publishEvent(new SessionDeletedEvent(this, session));
}
}
-
- /**
- * A custom implementation of {@link Session} that uses a {@link MapSession} as the
- * basis for its mapping. It keeps track if changes have been made since last save.
- *
- * @author Aleksandar Stojsavljevic
- */
final class HazelcastSession implements Session {
+
private final MapSession delegate;
private boolean isNew;
diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
index ea6f2efd0..f4281bd60 100644
--- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
+++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
@@ -47,6 +47,7 @@
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
@@ -125,10 +126,12 @@
*
* @author Vedran Pavic
* @author Craig Andrews
+ * @author Jakub Maciej
* @since 2.2.0
*/
public class JdbcIndexedSessionRepository
- implements FindByIndexNameSessionRepository {
+ implements FindByIndexNameSessionRepository,
+ CreateWithIdSessionRepository {
/**
* The default name of database table used by Spring Session to store sessions.
@@ -404,6 +407,17 @@ public JdbcSession createSession() {
return session;
}
+ @Override
+ public JdbcSession createSession(final String id) {
+ MapSession delegate = new MapSession(id);
+ if (this.defaultMaxInactiveInterval != null) {
+ delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
+ }
+ JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true);
+ session.flushIfRequired();
+ return session;
+ }
+
@Override
public void save(final JdbcSession session) {
session.save();
From d4b51357d4dfc23c75e0f7b3df28cea1ffd55df7 Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Tue, 12 Nov 2019 10:46:08 +0100
Subject: [PATCH 2/6] Make the code more dry-friendly
---
.../session/CreateWithIdSessionRepository.java | 2 +-
.../data/redis/RedisIndexedSessionRepository.java | 13 +++++--------
.../HazelcastIndexedSessionRepository.java | 13 +++++--------
.../session/jdbc/JdbcIndexedSessionRepository.java | 13 +++++--------
4 files changed, 16 insertions(+), 25 deletions(-)
diff --git a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
index 355839a41..92b956ba9 100644
--- a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
+++ b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
@@ -36,7 +36,7 @@ public interface CreateWithIdSessionRepository extends Sessio
*
* @return a new {@link Session} that is capable of being persisted by this
* {@link SessionRepository}
- * @param id a desired session id
+ * @param id a desired session id, if id is null - it will be generated by underlying implementation
*/
S createSession(String id);
diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
index 82b145d95..0e28c49c5 100644
--- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
+++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
@@ -19,6 +19,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
+import java.util.Optional;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -492,18 +493,14 @@ public void deleteById(String sessionId) {
@Override
public RedisSession createSession() {
- MapSession cached = new MapSession();
- if (this.defaultMaxInactiveInterval != null) {
- cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
- }
- RedisSession session = new RedisSession(cached, true);
- session.flushImmediateIfNecessary();
- return session;
+ return createSession(null);
}
@Override
public RedisSession createSession(final String id) {
- MapSession cached = new MapSession(id);
+ MapSession cached = Optional.ofNullable(id)
+ .map(MapSession::new)
+ .orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
index 32f007d88..7c2b31988 100644
--- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
+++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
@@ -20,6 +20,7 @@
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
+import java.util.Optional;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -237,18 +238,14 @@ public void setSaveMode(SaveMode saveMode) {
@Override
public HazelcastSession createSession() {
- MapSession cached = new MapSession();
- if (this.defaultMaxInactiveInterval != null) {
- cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
- }
- HazelcastSession session = new HazelcastSession(cached, true);
- session.flushImmediateIfNecessary();
- return session;
+ return createSession(null);
}
@Override
public HazelcastSession createSession(final String id) {
- MapSession cached = new MapSession(id);
+ MapSession cached = Optional.ofNullable(id)
+ .map(MapSession::new)
+ .orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
index f4281bd60..b221ab327 100644
--- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
+++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
@@ -23,6 +23,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Optional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -398,18 +399,14 @@ public void setSaveMode(SaveMode saveMode) {
@Override
public JdbcSession createSession() {
- MapSession delegate = new MapSession();
- if (this.defaultMaxInactiveInterval != null) {
- delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
- }
- JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true);
- session.flushIfRequired();
- return session;
+ return createSession(null);
}
@Override
public JdbcSession createSession(final String id) {
- MapSession delegate = new MapSession(id);
+ MapSession delegate = Optional.ofNullable(id)
+ .map(MapSession::new)
+ .orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
From 96f0e4add486c51b1221116ff765f3acddef381b Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Tue, 12 Nov 2019 10:48:52 +0100
Subject: [PATCH 3/6] Fix formatting to spring default
---
.../session/CreateWithIdSessionRepository.java | 3 ++-
.../session/data/redis/RedisIndexedSessionRepository.java | 6 ++----
.../hazelcast/HazelcastIndexedSessionRepository.java | 8 +++-----
.../session/jdbc/JdbcIndexedSessionRepository.java | 6 ++----
4 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
index 92b956ba9..a77f2ac65 100644
--- a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
+++ b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
@@ -36,7 +36,8 @@ public interface CreateWithIdSessionRepository extends Sessio
*
* @return a new {@link Session} that is capable of being persisted by this
* {@link SessionRepository}
- * @param id a desired session id, if id is null - it will be generated by underlying implementation
+ * @param id a desired session id, if id is null - it will be generated by underlying
+ * implementation
*/
S createSession(String id);
diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
index 0e28c49c5..57243e276 100644
--- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
+++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
@@ -251,7 +251,7 @@
*/
public class RedisIndexedSessionRepository
implements FindByIndexNameSessionRepository,
- CreateWithIdSessionRepository, MessageListener {
+ CreateWithIdSessionRepository, MessageListener {
private static final Log logger = LogFactory.getLog(RedisIndexedSessionRepository.class);
@@ -498,9 +498,7 @@ public RedisSession createSession() {
@Override
public RedisSession createSession(final String id) {
- MapSession cached = Optional.ofNullable(id)
- .map(MapSession::new)
- .orElse(new MapSession());
+ MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
index 7c2b31988..06a2541a5 100644
--- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
+++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
@@ -115,7 +115,7 @@
*/
public class HazelcastIndexedSessionRepository
implements FindByIndexNameSessionRepository,
- CreateWithIdSessionRepository,
+ CreateWithIdSessionRepository,
EntryAddedListener, EntryEvictedListener,
EntryRemovedListener {
@@ -243,9 +243,7 @@ public HazelcastSession createSession() {
@Override
public HazelcastSession createSession(final String id) {
- MapSession cached = Optional.ofNullable(id)
- .map(MapSession::new)
- .orElse(new MapSession());
+ MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
@@ -349,8 +347,8 @@ public void entryRemoved(EntryEvent event) {
this.eventPublisher.publishEvent(new SessionDeletedEvent(this, session));
}
}
- final class HazelcastSession implements Session {
+ final class HazelcastSession implements Session {
private final MapSession delegate;
diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
index b221ab327..23cf18f59 100644
--- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
+++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
@@ -132,7 +132,7 @@
*/
public class JdbcIndexedSessionRepository
implements FindByIndexNameSessionRepository,
- CreateWithIdSessionRepository {
+ CreateWithIdSessionRepository {
/**
* The default name of database table used by Spring Session to store sessions.
@@ -404,9 +404,7 @@ public JdbcSession createSession() {
@Override
public JdbcSession createSession(final String id) {
- MapSession delegate = Optional.ofNullable(id)
- .map(MapSession::new)
- .orElse(new MapSession());
+ MapSession delegate = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
From 63bb756fc2df84cca1b6d221dafe8d3d14d9170a Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Tue, 12 Nov 2019 11:12:53 +0100
Subject: [PATCH 4/6] Fix checkstyle warnings
---
.../session/CreateWithIdSessionRepository.java | 10 ++++++----
.../data/redis/RedisIndexedSessionRepository.java | 4 ++--
.../hazelcast/HazelcastIndexedSessionRepository.java | 4 ++--
.../session/jdbc/JdbcIndexedSessionRepository.java | 4 ++--
4 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
index a77f2ac65..995ee07c0 100644
--- a/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
+++ b/spring-session-core/src/main/java/org/springframework/session/CreateWithIdSessionRepository.java
@@ -16,8 +16,10 @@
package org.springframework.session;
+import org.springframework.lang.Nullable;
+
/**
- * Extends a basic {@link SessionRepository} to allow creating session with specific id
+ * Extends a basic {@link SessionRepository} to allow creating session with specific id.
*
* @param the type of Session being managed by this
* {@link CreateWithIdSessionRepository}
@@ -34,11 +36,11 @@ public interface CreateWithIdSessionRepository extends Sessio
* persisted. For example, the implementation returned might keep track of the changes
* ensuring that only the delta needs to be persisted on a save.
*
- * @return a new {@link Session} that is capable of being persisted by this
- * {@link SessionRepository}
* @param id a desired session id, if id is null - it will be generated by underlying
* implementation
+ * @return a new {@link Session} that is capable of being persisted by this
+ * {@link SessionRepository}
*/
- S createSession(String id);
+ S createSession(@Nullable String id);
}
diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
index 57243e276..b01ef2429 100644
--- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
+++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
@@ -19,9 +19,9 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
-import java.util.Optional;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.apache.commons.logging.Log;
@@ -37,9 +37,9 @@
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
-import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
index 06a2541a5..8d3fad62c 100644
--- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
+++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
@@ -20,9 +20,9 @@
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
-import java.util.Optional;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -40,9 +40,9 @@
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
-import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
index 23cf18f59..41b7eeec1 100644
--- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
+++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java
@@ -23,10 +23,10 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Optional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
@@ -46,9 +46,9 @@
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
+import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
-import org.springframework.session.CreateWithIdSessionRepository;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
From ba1ba8d5aca5d5409c5c5249569f9bff2e18e287 Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Tue, 12 Nov 2019 11:40:51 +0100
Subject: [PATCH 5/6] Missing tests for 'specific id' use-case
---
.../RedisIndexedSessionRepositoryTests.java | 13 ++++++++++++
...azelcastIndexedSessionRepositoryTests.java | 21 +++++++++++++++++++
.../JdbcIndexedSessionRepositoryTests.java | 17 +++++++++++++++
3 files changed, 51 insertions(+)
diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java
index 658c8a456..3c1f555b3 100644
--- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java
+++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java
@@ -162,6 +162,19 @@ void createSessionCustomMaxInactiveInterval() {
assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval));
}
+ @Test
+ void createSessionWithSpecificId() {
+ final String desiredSessionId = "desiredId";
+ Session session = this.redisRepository.createSession(desiredSessionId);
+ assertThat(session.getId()).isEqualTo(desiredSessionId);
+ }
+
+ @Test
+ void createSessionWithIdEqualNull() {
+ Session session = this.redisRepository.createSession(null);
+ assertThat(session.getId()).isNotNull();
+ }
+
@Test
void saveNewSession() {
RedisSession session = this.redisRepository.createSession();
diff --git a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java
index f648bfe58..f7463dc8e 100644
--- a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java
+++ b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepositoryTests.java
@@ -115,6 +115,27 @@ void createSessionCustomMaxInactiveInterval() {
verifyZeroInteractions(this.sessions);
}
+ @Test
+ void createSessionWithSpecificId() {
+ verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
+
+ final String desiredSessionId = "desiredId";
+ HazelcastSession session = this.repository.createSession(desiredSessionId);
+
+ assertThat(session.getId()).isEqualTo(desiredSessionId);
+ verifyZeroInteractions(this.sessions);
+ }
+
+ @Test
+ void createSessionWithIdEqualNull() {
+ verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
+
+ HazelcastSession session = this.repository.createSession(null);
+
+ assertThat(session.getId()).isNotNull();
+ verifyZeroInteractions(this.sessions);
+ }
+
@Test
void saveNewFlushModeOnSave() {
verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());
diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java
index aed37e535..da3268699 100644
--- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java
+++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcIndexedSessionRepositoryTests.java
@@ -267,6 +267,23 @@ void createSessionImmediateFlushMode() {
verifyNoMoreInteractions(this.jdbcOperations);
}
+ @Test
+ void createSessionWithSpecificId() {
+ final String desiredSessionId = "desiredId";
+ JdbcSession session = this.repository.createSession(desiredSessionId);
+ assertThat(session.isNew()).isTrue();
+ assertThat(session.getId()).isEqualTo(desiredSessionId);
+ verifyNoMoreInteractions(this.jdbcOperations);
+ }
+
+ @Test
+ void createSessionWithIdEqualNull() {
+ JdbcSession session = this.repository.createSession(null);
+ assertThat(session.isNew()).isTrue();
+ assertThat(session.getId()).isNotNull();
+ verifyNoMoreInteractions(this.jdbcOperations);
+ }
+
@Test
void saveNewWithoutAttributes() {
JdbcSession session = this.repository.createSession();
From 1999cc558d6a2b4a89959c037f0a5a5a32e58f44 Mon Sep 17 00:00:00 2001
From: Jakub Maciej
Date: Tue, 12 Nov 2019 13:00:35 +0100
Subject: [PATCH 6/6] Restore missing @author section
---
.../hazelcast/HazelcastIndexedSessionRepository.java | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
index 8d3fad62c..04bf80b73 100644
--- a/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
+++ b/spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java
@@ -348,6 +348,12 @@ public void entryRemoved(EntryEvent event) {
}
}
+ /**
+ * A custom implementation of {@link Session} that uses a {@link MapSession} as the
+ * basis for its mapping. It keeps track if changes have been made since last save.
+ *
+ * @author Aleksandar Stojsavljevic
+ */
final class HazelcastSession implements Session {
private final MapSession delegate;