@@ -57,17 +57,20 @@ public class JdkMongoSessionConverter extends AbstractMongoSessionConverter {
|
||||
private final Converter<Object, byte[]> serializer;
|
||||
private final Converter<byte[], Object> deserializer;
|
||||
|
||||
public JdkMongoSessionConverter() {
|
||||
this(new SerializingConverter(), new DeserializingConverter());
|
||||
private Duration maxInactiveInterval;
|
||||
|
||||
public JdkMongoSessionConverter(Duration maxInactiveInterval) {
|
||||
this(new SerializingConverter(), new DeserializingConverter(), maxInactiveInterval);
|
||||
}
|
||||
|
||||
public JdkMongoSessionConverter(Converter<Object, byte[]> serializer,
|
||||
Converter<byte[], Object> deserializer) {
|
||||
|
||||
Converter<byte[], Object> deserializer, Duration maxInactiveInterval) {
|
||||
Assert.notNull(serializer, "serializer cannot be null");
|
||||
Assert.notNull(deserializer, "deserializer cannot be null");
|
||||
Assert.notNull(maxInactiveInterval, "maxInactiveInterval cannot be null");
|
||||
this.serializer = serializer;
|
||||
this.deserializer = deserializer;
|
||||
this.maxInactiveInterval = maxInactiveInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,7 +103,7 @@ public class JdkMongoSessionConverter extends AbstractMongoSessionConverter {
|
||||
@Override
|
||||
protected MongoSession convert(Document sessionWrapper) {
|
||||
|
||||
Object maxInterval = sessionWrapper.get(MAX_INTERVAL);
|
||||
Object maxInterval = sessionWrapper.getOrDefault(MAX_INTERVAL, this.maxInactiveInterval);
|
||||
|
||||
Duration maxIntervalDuration = (maxInterval instanceof Duration)
|
||||
? (Duration) maxInterval
|
||||
|
||||
@@ -62,9 +62,10 @@ public class MongoOperationsSessionRepository
|
||||
|
||||
private final MongoOperations mongoOperations;
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter();
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
|
||||
public MongoOperationsSessionRepository(MongoOperations mongoOperations) {
|
||||
this.mongoOperations = mongoOperations;
|
||||
|
||||
@@ -17,6 +17,8 @@ package org.springframework.session.data.mongo;
|
||||
|
||||
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.bson.Document;
|
||||
@@ -43,9 +45,10 @@ public class ReactiveMongoOperationsSessionRepository implements ReactorSessionR
|
||||
|
||||
private final ReactiveMongoOperations mongoOperations;
|
||||
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter();
|
||||
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
|
||||
private String collectionName = DEFAULT_COLLECTION_NAME;
|
||||
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
|
||||
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
|
||||
|
||||
private MongoOperations blockingMongoOperations;
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@ package org.springframework.session.data.mongo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.serializer.support.DeserializingConverter;
|
||||
import org.springframework.core.serializer.support.SerializingConverter;
|
||||
@@ -35,16 +37,17 @@ import com.mongodb.DBObject;
|
||||
*/
|
||||
public class JdkMongoSessionConverterTest {
|
||||
|
||||
JdkMongoSessionConverter sut = new JdkMongoSessionConverter();
|
||||
Duration inactiveInterval = Duration.ofMinutes(30);
|
||||
JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(inactiveInterval);
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullSerializer() {
|
||||
new JdkMongoSessionConverter(null, new DeserializingConverter());
|
||||
new JdkMongoSessionConverter(null, new DeserializingConverter(), inactiveInterval);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullDeserializer() {
|
||||
new JdkMongoSessionConverter(new SerializingConverter(), null);
|
||||
new JdkMongoSessionConverter(new SerializingConverter(), null, inactiveInterval);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,14 +100,30 @@ public class JdkMongoSessionConverterTest {
|
||||
assertThat(dbObject.get("principal")).isEqualTo(principalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sessionWrapperWithNoMaxIntervalShouldFallbackToDefaultValues() {
|
||||
|
||||
// given
|
||||
MongoSession toSerialize = new MongoSession();
|
||||
DBObject dbObject = convertToDBObject(toSerialize);
|
||||
Document document = new Document(dbObject.toMap());
|
||||
document.remove("interval");
|
||||
|
||||
// when
|
||||
MongoSession convertedSession = this.mongoSessionConverter.convert(document);
|
||||
|
||||
// then
|
||||
assertThat(convertedSession.getMaxInactiveInterval()).isEqualTo(Duration.ofMinutes(30));
|
||||
}
|
||||
|
||||
MongoSession convertToSession(DBObject session) {
|
||||
return (MongoSession) this.sut.convert(session,
|
||||
return (MongoSession) this.mongoSessionConverter.convert(session,
|
||||
TypeDescriptor.valueOf(DBObject.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class));
|
||||
}
|
||||
|
||||
DBObject convertToDBObject(MongoSession session) {
|
||||
return (DBObject) this.sut.convert(session,
|
||||
return (DBObject) this.mongoSessionConverter.convert(session,
|
||||
TypeDescriptor.valueOf(MongoSession.class),
|
||||
TypeDescriptor.valueOf(DBObject.class));
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.session.data.mongo.integration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -85,7 +86,7 @@ public class MongoRepositoryJdkSerializationITest extends AbstractMongoRepositor
|
||||
|
||||
@Bean
|
||||
public AbstractMongoSessionConverter mongoSessionConverter() {
|
||||
return new JdkMongoSessionConverter();
|
||||
return new JdkMongoSessionConverter(Duration.ofMinutes(30));
|
||||
}
|
||||
}
|
||||
// end::sample[]
|
||||
|
||||
Reference in New Issue
Block a user