Set expireAt properly to support TTL indexing.

This commit is contained in:
Michael Ruf
2019-02-25 09:25:34 +01:00
committed by Greg Turnquist
parent 841fcb9cf4
commit 82cf365ba7
2 changed files with 46 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.session.data.mongo;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import org.apache.commons.logging.Log;
@@ -46,6 +47,7 @@ import com.mongodb.util.JSON;
*
* @author Jakub Kubrynski
* @author Greg Turnquist
* @author Michael Ruf
* @since 1.2
*/
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -54,6 +56,7 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
private static final String ATTRS_FIELD_NAME = "attrs.";
private static final String PRINCIPAL_FIELD_NAME = "principal";
private static final String EXPIRE_AT_FIELD_NAME = "expireAt";
private final ObjectMapper objectMapper;
@@ -108,6 +111,7 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
try {
DBObject dbSession = (DBObject) JSON.parse(this.objectMapper.writeValueAsString(source));
dbSession.put(EXPIRE_AT_FIELD_NAME, source.getExpireAt());
dbSession.put(PRINCIPAL_FIELD_NAME, extractPrincipal(source));
return dbSession;
} catch (JsonProcessingException e) {
@@ -119,10 +123,14 @@ public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter
@Nullable
protected MongoSession convert(Document source) {
Date expireAt = source.getDate(EXPIRE_AT_FIELD_NAME);
source.remove(EXPIRE_AT_FIELD_NAME);
String json = source.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
try {
return this.objectMapper.readValue(json, MongoSession.class);
MongoSession mongoSession = this.objectMapper.readValue(json, MongoSession.class);
mongoSession.setExpireAt(expireAt);
return mongoSession;
} catch (IOException e) {
LOG.error("Error during Mongo Session deserialization", e);
return null;

View File

@@ -18,7 +18,11 @@ package org.springframework.session.data.mongo;
import static org.assertj.core.api.AssertionsForClassTypes.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.ReflectionUtils;
@@ -85,4 +89,37 @@ public class JacksonMongoSessionConverterTest extends AbstractMongoSessionConver
new JacksonMongoSessionConverter((ObjectMapper) null);
}
@Test
public void shouldSaveExpireAtAsDate() {
//given
MongoSession session = new MongoSession();
//when
DBObject convert = this.mongoSessionConverter.convert(session);
//then
assertThat(convert.get("expireAt")).isInstanceOf(Date.class);
assertThat(convert.get("expireAt")).isEqualTo(session.getExpireAt());
}
@Test
public void shouldLoadExpireAtFromDocument() {
// given
Date now = new Date();
HashMap data = new HashMap();
data.put("expireAt", now);
data.put("@class", MongoSession.class.getName());
data.put("_id", new ObjectId().toString());
Document document = new Document(data);
// when
MongoSession convertedSession = this.mongoSessionConverter.convert(document);
// then
assertThat(convertedSession.getExpireAt()).isEqualTo(now);
}
}