JdbcOperationsSessionRepository uses ConversionService
Issue gh-364
This commit is contained in:
@@ -31,8 +31,9 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.core.serializer.support.DeserializingConverter;
|
||||
import org.springframework.core.serializer.support.SerializingConverter;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
@@ -139,9 +140,7 @@ public class JdbcOperationsSessionRepository
|
||||
*/
|
||||
private Integer defaultMaxInactiveInterval;
|
||||
|
||||
private Converter<Object, byte[]> serializingConverter = new SerializingConverter();
|
||||
|
||||
private Converter<byte[], Object> deserializingConverter = new DeserializingConverter();
|
||||
private ConversionService conversionService;
|
||||
|
||||
private LobHandler lobHandler = new DefaultLobHandler();
|
||||
|
||||
@@ -162,6 +161,8 @@ public class JdbcOperationsSessionRepository
|
||||
public JdbcOperationsSessionRepository(JdbcOperations jdbcOperations) {
|
||||
Assert.notNull(jdbcOperations, "JdbcOperations must not be null");
|
||||
this.jdbcOperations = jdbcOperations;
|
||||
|
||||
this.conversionService = createDefaultConversionService();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,14 +189,12 @@ public class JdbcOperationsSessionRepository
|
||||
this.lobHandler = lobHandler;
|
||||
}
|
||||
|
||||
public void setSerializingConverter(Converter<Object, byte[]> serializingConverter) {
|
||||
Assert.notNull(serializingConverter, "SerializingConverter must not be null");
|
||||
this.serializingConverter = serializingConverter;
|
||||
}
|
||||
|
||||
public void setDeserializingConverter(Converter<byte[], Object> deserializingConverter) {
|
||||
Assert.notNull(deserializingConverter, "DeserializingConverter must not be null");
|
||||
this.deserializingConverter = deserializingConverter;
|
||||
/**
|
||||
* @param conversionService the converter to set
|
||||
*/
|
||||
public void setConversionService(ConversionService conversionService) {
|
||||
Assert.notNull(conversionService, "conversionService must not be null");
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -328,7 +327,7 @@ public class JdbcOperationsSessionRepository
|
||||
}
|
||||
|
||||
private byte[] serialize(ExpiringSession session) {
|
||||
return this.serializingConverter.convert(session);
|
||||
return (byte[]) this.conversionService.convert(session, TypeDescriptor.valueOf(ExpiringSession.class), TypeDescriptor.valueOf(byte[].class));
|
||||
}
|
||||
|
||||
private static long roundDownMinute(long timeInMs) {
|
||||
@@ -339,6 +338,14 @@ public class JdbcOperationsSessionRepository
|
||||
return date.getTimeInMillis();
|
||||
}
|
||||
|
||||
private static GenericConversionService createDefaultConversionService() {
|
||||
GenericConversionService converter = new GenericConversionService();
|
||||
converter.addConverter(ExpiringSession.class, byte[].class, new SerializingConverter());
|
||||
converter.addConverter(byte[].class, ExpiringSession.class, new DeserializingConverter());
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
final class JdbcSession implements ExpiringSession {
|
||||
|
||||
private final ExpiringSession delegate;
|
||||
@@ -467,8 +474,8 @@ public class JdbcOperationsSessionRepository
|
||||
|
||||
@Override
|
||||
public ExpiringSession mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return (ExpiringSession) JdbcOperationsSessionRepository.this.deserializingConverter.convert(
|
||||
JdbcOperationsSessionRepository.this.lobHandler.getBlobAsBytes(rs, "SESSION_BYTES"));
|
||||
return (ExpiringSession) JdbcOperationsSessionRepository.this.conversionService.convert(
|
||||
JdbcOperationsSessionRepository.this.lobHandler.getBlobAsBytes(rs, "SESSION_BYTES"), TypeDescriptor.valueOf(byte[].class), TypeDescriptor.valueOf(ExpiringSession.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.session.jdbc.config.annotation.web.http;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -26,7 +25,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportAware;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -58,9 +57,11 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
|
||||
|
||||
private LobHandler lobHandler;
|
||||
|
||||
private Converter<Object, byte[]> serializingConverter;
|
||||
@Autowired(required = false)
|
||||
@Qualifier("conversionService")
|
||||
private ConversionService conversionService;
|
||||
|
||||
private Converter<byte[], Object> deserializingConverter;
|
||||
private ConversionService springSessionConversionService;
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate springSessionJdbcOperations(DataSource dataSource) {
|
||||
@@ -80,11 +81,10 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
|
||||
if (this.lobHandler != null) {
|
||||
sessionRepository.setLobHandler(this.lobHandler);
|
||||
}
|
||||
if (this.serializingConverter != null) {
|
||||
sessionRepository.setSerializingConverter(this.serializingConverter);
|
||||
}
|
||||
if (this.deserializingConverter != null) {
|
||||
sessionRepository.setDeserializingConverter(this.deserializingConverter);
|
||||
if (this.springSessionConversionService != null) {
|
||||
sessionRepository.setConversionService(this.springSessionConversionService);
|
||||
} else if(conversionService != null) {
|
||||
sessionRepository.setConversionService(this.conversionService);
|
||||
}
|
||||
return sessionRepository;
|
||||
}
|
||||
@@ -96,15 +96,9 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
@Qualifier("springSessionSerializingConverter")
|
||||
public void setSerializingConverter(Converter<Object, byte[]> serializingConverter) {
|
||||
this.serializingConverter = serializingConverter;
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
@Qualifier("springSessionDeserializingConverter")
|
||||
public void setDeserializingConverter(Converter<byte[], Object> deserializingConverter) {
|
||||
this.deserializingConverter = deserializingConverter;
|
||||
@Qualifier("springSessionConversionService")
|
||||
public void setSpringSessionConversionService(ConversionService conversionService) {
|
||||
this.springSessionConversionService = conversionService;
|
||||
}
|
||||
|
||||
private String getTableName() {
|
||||
|
||||
@@ -129,19 +129,11 @@ public class JdbcOperationsSessionRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSerializingConverterNull() {
|
||||
public void setConversionServiceNull() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("SerializingConverter must not be null");
|
||||
this.thrown.expectMessage("conversionService must not be null");
|
||||
|
||||
this.repository.setSerializingConverter(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDeserializingConverterNull() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("DeserializingConverter must not be null");
|
||||
|
||||
this.repository.setDeserializingConverter(null);
|
||||
this.repository.setConversionService(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
|
||||
@@ -123,33 +124,15 @@ public class JdbcHttpSessionConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customSerializingConverterConfiguration() {
|
||||
registerAndRefresh(CustomSerializingConverterConfiguration.class);
|
||||
|
||||
JdbcOperationsSessionRepository repository = this.context.getBean(JdbcOperationsSessionRepository.class);
|
||||
Converter<Object, byte[]> serializingConverter =
|
||||
(Converter<Object, byte[]>) this.context.getBean("springSessionSerializingConverter");
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(serializingConverter).isNotNull();
|
||||
Converter<Object, byte[]> sc =
|
||||
(Converter<Object, byte[]>) ReflectionTestUtils.getField(repository, "serializingConverter");
|
||||
assertThat(sc).isEqualTo(serializingConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customDeserializingConverterConfiguration() {
|
||||
public void customConversionServiceConfiguration() {
|
||||
registerAndRefresh(CustomDeserializingConverterConfiguration.class);
|
||||
|
||||
JdbcOperationsSessionRepository repository = this.context.getBean(JdbcOperationsSessionRepository.class);
|
||||
Converter<byte[], Object> deserializingConverter =
|
||||
(Converter<byte[], Object>) this.context.getBean("springSessionDeserializingConverter");
|
||||
ConversionService conversionService = this.context.getBean("springSessionConversionService", ConversionService.class);
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(deserializingConverter).isNotNull();
|
||||
Converter<byte[], Object> dc =
|
||||
(Converter<byte[], Object>) ReflectionTestUtils.getField(repository, "deserializingConverter");
|
||||
assertThat(dc).isEqualTo(deserializingConverter);
|
||||
assertThat(conversionService).isNotNull();
|
||||
Object repositoryConversionService = ReflectionTestUtils.getField(repository, "conversionService");
|
||||
assertThat(repositoryConversionService).isEqualTo(conversionService);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
@@ -214,9 +197,8 @@ public class JdbcHttpSessionConfigurationTests {
|
||||
static class CustomDeserializingConverterConfiguration extends BaseConfiguration {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("unchecked")
|
||||
public Converter<byte[], Object> springSessionDeserializingConverter() {
|
||||
return mock(Converter.class);
|
||||
public ConversionService springSessionConversionService() {
|
||||
return mock(ConversionService.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user