JdkMongoSessionConverter supports custom ClassLoader

Fixes gh-431
This commit is contained in:
Jakub Kubrynski
2016-03-16 16:11:17 +01:00
committed by Rob Winch
parent 7a82915a98
commit 9014ac9060
3 changed files with 27 additions and 15 deletions

View File

@@ -337,6 +337,8 @@ You can explicitly register `JdkMongoSessionConverter` by defining it as a Bean.
include::{docs-test-dir}docs/http/MongoJdkSessionConfiguration.java[tags=config]
----
There is also a constructor taking `Serializer` and `Deserializer` objects, allowing you to pass custom implementations, which is especially important when you want to use non-default classloader.
==== Using custom converters
You can create your own session converter by extending `AbstractMongoSessionConverter` class.

View File

@@ -16,14 +16,17 @@
package org.springframework.session.data.mongo;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import com.fasterxml.jackson.databind.Module;
import com.mongodb.MongoClient;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.geo.GeoModule;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
@@ -63,7 +66,7 @@ public class MongoRepositoryJacksonITests extends AbstractMongoRepositoryITests
@Bean
public AbstractMongoSessionConverter mongoSessionConverter() {
return new JacksonMongoSessionConverter();
return new JacksonMongoSessionConverter(Collections.<Module>singletonList(new GeoModule()));
}
}

View File

@@ -18,8 +18,6 @@ package org.springframework.session.data.mongo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -29,6 +27,10 @@ import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.serializer.DefaultDeserializer;
import org.springframework.core.serializer.DefaultSerializer;
import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.session.FindByIndexNameSessionRepository;
@@ -48,10 +50,22 @@ public class JdkMongoSessionConverter extends AbstractMongoSessionConverter {
private static final String CREATION_TIME = "created";
private static final String LAST_ACCESSED_TIME = "accessed";
private static final String MAX_INTERVAL = "interval";
private static final String ATTRIBUTES = "attr";
static final String ATTRIBUTES = "attr";
private static final String PRINCIPAL_FIELD_NAME = "principal";
private final Serializer serializer;
private final Deserializer deserializer;
public JdkMongoSessionConverter() {
this(new DefaultSerializer(), new DefaultDeserializer());
}
public JdkMongoSessionConverter(Serializer serializer, Deserializer deserializer) {
this.serializer = serializer;
this.deserializer = deserializer;
}
@Override
public Query getQueryForIndex(String indexName, Object indexValue) {
if (FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
@@ -86,16 +100,15 @@ public class JdkMongoSessionConverter extends AbstractMongoSessionConverter {
return session;
}
@SuppressWarnings("unchecked")
private byte[] serializeAttributes(Session session) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(out);
Map<String, Object> attributes = new HashMap<String, Object>();
for (String attrName : session.getAttributeNames()) {
attributes.put(attrName, session.getAttribute(attrName));
}
outputStream.writeObject(attributes);
outputStream.flush();
this.serializer.serialize(attributes, out);
return out.toByteArray();
}
catch (IOException e) {
@@ -109,22 +122,16 @@ public class JdkMongoSessionConverter extends AbstractMongoSessionConverter {
try {
ByteArrayInputStream in = new ByteArrayInputStream(
(byte[]) sessionWrapper.get(ATTRIBUTES));
ObjectInputStream objectInputStream = new ObjectInputStream(in);
Map<String, Object> attributes = (Map<String, Object>) objectInputStream
.readObject();
Map<String, Object> attributes =
(Map<String, Object>) this.deserializer.deserialize(in);
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
session.setAttribute(entry.getKey(), entry.getValue());
}
objectInputStream.close();
}
catch (IOException e) {
LOG.error("Exception during session deserialization", e);
throw new IllegalStateException("Cannot deserialize session", e);
}
catch (ClassNotFoundException e) {
LOG.error("Exception during session deserialization", e);
throw new IllegalStateException("Cannot deserialize session", e);
}
}
}