Handle missing expiration

Original issue: #13
This commit is contained in:
Greg Turnquist
2017-10-18 14:01:07 -05:00
parent 966ca62e65
commit 00085d517b
5 changed files with 41 additions and 14 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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));
}

View File

@@ -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[]