Skip to content

Commit 4eecb73

Browse files
author
Jakub Maciej
committed
Add support for creating session with externally specified id
1 parent 6d2e51a commit 4eecb73

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session;
18+
19+
/**
20+
* Extends a basic {@link SessionRepository} to allow creating session with specific id
21+
*
22+
* @param <S> the type of Session being managed by this
23+
* {@link CreateWithIdSessionRepository}
24+
* @author Jakub Maciej
25+
*/
26+
public interface CreateWithIdSessionRepository<S extends Session> extends SessionRepository<S> {
27+
28+
/**
29+
* Creates a new {@link Session} that is capable of being persisted by this
30+
* {@link SessionRepository}.
31+
*
32+
* <p>
33+
* This allows optimizations and customizations in how the {@link Session} is
34+
* persisted. For example, the implementation returned might keep track of the changes
35+
* ensuring that only the delta needs to be persisted on a save.
36+
* </p>
37+
* @return a new {@link Session} that is capable of being persisted by this
38+
* {@link SessionRepository}
39+
* @param id a desired session id
40+
*/
41+
S createSession(String id);
42+
43+
}

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.redis.serializer.RedisSerializer;
3939
import org.springframework.session.DelegatingIndexResolver;
4040
import org.springframework.session.FindByIndexNameSessionRepository;
41+
import org.springframework.session.CreateWithIdSessionRepository;
4142
import org.springframework.session.FlushMode;
4243
import org.springframework.session.IndexResolver;
4344
import org.springframework.session.MapSession;
@@ -244,10 +245,12 @@
244245
*
245246
* @author Rob Winch
246247
* @author Vedran Pavic
248+
* @author Jakub Maciej
247249
* @since 2.2.0
248250
*/
249251
public class RedisIndexedSessionRepository
250-
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener {
252+
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>,
253+
CreateWithIdSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener {
251254

252255
private static final Log logger = LogFactory.getLog(RedisIndexedSessionRepository.class);
253256

@@ -498,6 +501,17 @@ public RedisSession createSession() {
498501
return session;
499502
}
500503

504+
@Override
505+
public RedisSession createSession(final String id) {
506+
MapSession cached = new MapSession(id);
507+
if (this.defaultMaxInactiveInterval != null) {
508+
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
509+
}
510+
RedisSession session = new RedisSession(cached, true);
511+
session.flushImmediateIfNecessary();
512+
return session;
513+
}
514+
501515
@Override
502516
public void onMessage(Message message, byte[] pattern) {
503517
byte[] messageChannel = message.getChannel();

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.context.ApplicationEventPublisher;
4242
import org.springframework.session.DelegatingIndexResolver;
4343
import org.springframework.session.FindByIndexNameSessionRepository;
44+
import org.springframework.session.CreateWithIdSessionRepository;
4445
import org.springframework.session.FlushMode;
4546
import org.springframework.session.IndexResolver;
4647
import org.springframework.session.MapSession;
@@ -108,10 +109,12 @@
108109
* @author Tommy Ludwig
109110
* @author Mark Anderson
110111
* @author Aleksandar Stojsavljevic
112+
* @author Jakub Maciej
111113
* @since 2.2.0
112114
*/
113115
public class HazelcastIndexedSessionRepository
114116
implements FindByIndexNameSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>,
117+
CreateWithIdSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>,
115118
EntryAddedListener<String, MapSession>, EntryEvictedListener<String, MapSession>,
116119
EntryRemovedListener<String, MapSession> {
117120

@@ -243,6 +246,17 @@ public HazelcastSession createSession() {
243246
return session;
244247
}
245248

249+
@Override
250+
public HazelcastSession createSession(final String id) {
251+
MapSession cached = new MapSession(id);
252+
if (this.defaultMaxInactiveInterval != null) {
253+
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
254+
}
255+
HazelcastSession session = new HazelcastSession(cached, true);
256+
session.flushImmediateIfNecessary();
257+
return session;
258+
}
259+
246260
@Override
247261
public void save(HazelcastSession session) {
248262
if (session.isNew) {
@@ -338,15 +352,9 @@ public void entryRemoved(EntryEvent<String, MapSession> event) {
338352
this.eventPublisher.publishEvent(new SessionDeletedEvent(this, session));
339353
}
340354
}
341-
342-
/**
343-
* A custom implementation of {@link Session} that uses a {@link MapSession} as the
344-
* basis for its mapping. It keeps track if changes have been made since last save.
345-
*
346-
* @author Aleksandar Stojsavljevic
347-
*/
348355
final class HazelcastSession implements Session {
349356

357+
350358
private final MapSession delegate;
351359

352360
private boolean isNew;

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.jdbc.support.lob.LobHandler;
4848
import org.springframework.session.DelegatingIndexResolver;
4949
import org.springframework.session.FindByIndexNameSessionRepository;
50+
import org.springframework.session.CreateWithIdSessionRepository;
5051
import org.springframework.session.FlushMode;
5152
import org.springframework.session.IndexResolver;
5253
import org.springframework.session.MapSession;
@@ -125,10 +126,12 @@
125126
*
126127
* @author Vedran Pavic
127128
* @author Craig Andrews
129+
* @author Jakub Maciej
128130
* @since 2.2.0
129131
*/
130132
public class JdbcIndexedSessionRepository
131-
implements FindByIndexNameSessionRepository<JdbcIndexedSessionRepository.JdbcSession> {
133+
implements FindByIndexNameSessionRepository<JdbcIndexedSessionRepository.JdbcSession>,
134+
CreateWithIdSessionRepository<JdbcIndexedSessionRepository.JdbcSession> {
132135

133136
/**
134137
* The default name of database table used by Spring Session to store sessions.
@@ -404,6 +407,17 @@ public JdbcSession createSession() {
404407
return session;
405408
}
406409

410+
@Override
411+
public JdbcSession createSession(final String id) {
412+
MapSession delegate = new MapSession(id);
413+
if (this.defaultMaxInactiveInterval != null) {
414+
delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
415+
}
416+
JdbcSession session = new JdbcSession(delegate, UUID.randomUUID().toString(), true);
417+
session.flushIfRequired();
418+
return session;
419+
}
420+
407421
@Override
408422
public void save(final JdbcSession session) {
409423
session.save();

0 commit comments

Comments
 (0)