Skip to content

Commit

Permalink
JdkMongoSessionConverter supports custom ClassLoader
Browse files Browse the repository at this point in the history
Fixes gh-431
  • Loading branch information
jkubrynski authored and Rob Winch committed Mar 17, 2016
1 parent 7a82915 commit 9014ac9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +66,7 @@ public MongoOperations mongoOperations() throws UnknownHostException {

@Bean
public AbstractMongoSessionConverter mongoSessionConverter() {
return new JacksonMongoSessionConverter();
return new JacksonMongoSessionConverter(Collections.<Module>singletonList(new GeoModule()));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
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;
Expand All @@ -29,6 +27,10 @@
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;
Expand All @@ -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
Expand Down Expand Up @@ -86,16 +100,15 @@ protected MongoExpiringSession convert(DBObject sessionWrapper) {
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) {
Expand All @@ -109,22 +122,16 @@ private void deserializeAttributes(DBObject sessionWrapper, Session session) {
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);
}
}

}

0 comments on commit 9014ac9

Please sign in to comment.