Skip to content

Session creation with pre-specified id. #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

import org.springframework.lang.Nullable;

/**
* Extends a basic {@link SessionRepository} to allow creating session with specific id.
*
* @param <S> the type of Session being managed by this
* {@link CreateWithIdSessionRepository}
* @author Jakub Maciej
*/
public interface CreateWithIdSessionRepository<S extends Session> extends SessionRepository<S> {

/**
* Creates a new {@link Session} that is capable of being persisted by this
* {@link SessionRepository}.
*
* <p>
* 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.
* </p>
* @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(@Nullable String id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand All @@ -36,6 +37,7 @@
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.FlushMode;
Expand Down Expand Up @@ -244,10 +246,12 @@
*
* @author Rob Winch
* @author Vedran Pavic
* @author Jakub Maciej
* @since 2.2.0
*/
public class RedisIndexedSessionRepository
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener {
implements FindByIndexNameSessionRepository<RedisIndexedSessionRepository.RedisSession>,
CreateWithIdSessionRepository<RedisIndexedSessionRepository.RedisSession>, MessageListener {

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

Expand Down Expand Up @@ -489,7 +493,12 @@ public void deleteById(String sessionId) {

@Override
public RedisSession createSession() {
MapSession cached = new MapSession();
return createSession(null);
}

@Override
public RedisSession createSession(final String id) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final is here redundant, it's not used in other methods.

MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use .orElseGet(MapSession::new) to avoid creating an unnecessary object if id is provided.

if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand All @@ -39,6 +40,7 @@
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.FlushMode;
Expand Down Expand Up @@ -108,10 +110,12 @@
* @author Tommy Ludwig
* @author Mark Anderson
* @author Aleksandar Stojsavljevic
* @author Jakub Maciej
* @since 2.2.0
*/
public class HazelcastIndexedSessionRepository
implements FindByIndexNameSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>,
CreateWithIdSessionRepository<HazelcastIndexedSessionRepository.HazelcastSession>,
EntryAddedListener<String, MapSession>, EntryEvictedListener<String, MapSession>,
EntryRemovedListener<String, MapSession> {

Expand Down Expand Up @@ -234,7 +238,12 @@ public void setSaveMode(SaveMode saveMode) {

@Override
public HazelcastSession createSession() {
MapSession cached = new MapSession();
return createSession(null);
}

@Override
public HazelcastSession createSession(final String id) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make the HazelcastSession public, so that can override

MapSession cached = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -45,6 +46,7 @@
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.FlushMode;
Expand Down Expand Up @@ -125,10 +127,12 @@
*
* @author Vedran Pavic
* @author Craig Andrews
* @author Jakub Maciej
* @since 2.2.0
*/
public class JdbcIndexedSessionRepository
implements FindByIndexNameSessionRepository<JdbcIndexedSessionRepository.JdbcSession> {
implements FindByIndexNameSessionRepository<JdbcIndexedSessionRepository.JdbcSession>,
CreateWithIdSessionRepository<JdbcIndexedSessionRepository.JdbcSession> {

/**
* The default name of database table used by Spring Session to store sessions.
Expand Down Expand Up @@ -395,7 +399,12 @@ public void setSaveMode(SaveMode saveMode) {

@Override
public JdbcSession createSession() {
MapSession delegate = new MapSession();
return createSession(null);
}

@Override
public JdbcSession createSession(final String id) {
MapSession delegate = Optional.ofNullable(id).map(MapSession::new).orElse(new MapSession());
if (this.defaultMaxInactiveInterval != null) {
delegate.setMaxInactiveInterval(Duration.ofSeconds(this.defaultMaxInactiveInterval));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down