Skip to content

Align MongoDB session repositories with other implementations #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.lang.reflect.Field;

Expand All @@ -25,9 +25,6 @@
import org.springframework.context.ApplicationContext;
import org.springframework.core.serializer.DefaultDeserializer;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.Assert;
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
import org.springframework.util.ReflectionUtils;

/**
Expand All @@ -49,8 +46,7 @@ void verifyContainerClassLoaderLoadedIntoConverter() {

Field mongoSessionConverterField = ReflectionUtils.findField(this.sessionRepository.getClass(),
"mongoSessionConverter");
ReflectionUtils.makeAccessible(
Assert.requireNonNull(mongoSessionConverterField, "mongoSessionConverter must not be null!"));
ReflectionUtils.makeAccessible(mongoSessionConverterField);
AbstractMongoSessionConverter sessionConverter = (AbstractMongoSessionConverter) ReflectionUtils
.getField(mongoSessionConverterField, this.sessionRepository);

Expand All @@ -70,7 +66,7 @@ void verifyContainerClassLoaderLoadedIntoConverter() {
private static Object extractField(Class<?> clazz, String fieldName, Object obj) {

Field field = ReflectionUtils.findField(clazz, fieldName);
ReflectionUtils.makeAccessible(Assert.requireNonNull(field, fieldName + " must not be null!"));
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, obj);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -37,8 +37,6 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.mongo.MongoSession;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.net.URI;

Expand All @@ -41,8 +41,6 @@
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
import org.springframework.session.data.mongo.config.annotation.web.reactive.EnableMongoWebSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.net.URI;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.util.Collections;
import java.util.Map;
Expand All @@ -25,9 +25,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.geo.GeoModule;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
import org.springframework.session.data.mongo.MongoSession;
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
import org.springframework.test.context.ContextConfiguration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.session.data.mongo.integration;
package org.springframework.session.data.mongo;

import java.time.Duration;
import java.util.Map;
Expand All @@ -23,9 +23,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
import org.springframework.session.data.mongo.MongoSession;
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
import org.springframework.test.context.ContextConfiguration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,8 @@
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.IndexResolver;
import org.springframework.session.PrincipalNameIndexResolver;
import org.springframework.session.Session;
import org.springframework.util.Assert;

/**
* Base class for serializing and deserializing session objects. To create custom
Expand All @@ -54,8 +56,7 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter

private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";

private IndexResolver<MongoSession> indexResolver = new DelegatingIndexResolver<>(
new PrincipalNameIndexResolver<>());
private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());

/**
* Returns query to be executed to return sessions based on a particular index.
Expand Down Expand Up @@ -122,8 +123,9 @@ else if (Document.class.isAssignableFrom(sourceType.getType())) {

protected abstract MongoSession convert(Document sessionWrapper);

public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
this.indexResolver = Assert.requireNonNull(indexResolver, "indexResolver must not be null!");
public void setIndexResolver(IndexResolver<Session> indexResolver) {
Assert.notNull(indexResolver, "indexResolver must not be null");
this.indexResolver = indexResolver;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;
Expand All @@ -38,6 +39,7 @@
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.util.Assert;

/**
* Session repository implementation which stores sessions in Mongo. Uses
Expand Down Expand Up @@ -97,9 +99,9 @@ public MongoSession createSession() {

@Override
public void save(MongoSession session) {
this.mongoOperations
.save(Assert.requireNonNull(MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session),
"convertToDBObject must not null!"), this.collectionName);
DBObject dbObject = MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session);
Assert.notNull(dbObject, "dbObject must not be null");
this.mongoOperations.save(dbObject, this.collectionName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @author Greg Turnquist
* @since 1.2
*/
public class MongoSession implements Session {
class MongoSession implements Session {

/**
* Mongo doesn't support {@literal dot} in field names. We replace it with a unicode
Expand Down Expand Up @@ -66,15 +66,15 @@ public class MongoSession implements Session {

private Map<String, Object> attrs = new HashMap<>();

public MongoSession() {
MongoSession() {
this(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
}

public MongoSession(long maxInactiveIntervalInSeconds) {
MongoSession(long maxInactiveIntervalInSeconds) {
this(UUID.randomUUID().toString(), maxInactiveIntervalInSeconds);
}

public MongoSession(String id, long maxInactiveIntervalInSeconds) {
MongoSession(String id, long maxInactiveIntervalInSeconds) {

this.id = id;
this.originalSessionId = id;
Expand Down Expand Up @@ -130,7 +130,7 @@ public Instant getCreationTime() {
return Instant.ofEpochMilli(this.createdMillis);
}

public void setCreationTime(long created) {
void setCreationTime(long created) {
this.createdMillis = created;
}

Expand Down Expand Up @@ -184,11 +184,11 @@ public String getId() {
return this.id;
}

public Date getExpireAt() {
Date getExpireAt() {
return this.expireAt;
}

public void setExpireAt(final Date expireAt) {
void setExpireAt(final Date expireAt) {
this.expireAt = expireAt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
*
* @author Greg Turnquist
*/
public final class MongoSessionUtils {
final class MongoSessionUtils {

private MongoSessionUtils() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.mongo.MongoSession;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;

Expand Down Expand Up @@ -68,7 +68,7 @@ public class MongoHttpSessionConfiguration implements BeanClassLoaderAware, Embe

private ClassLoader classLoader;

private IndexResolver<MongoSession> indexResolver;
private IndexResolver<Session> indexResolver;

@Bean
public MongoIndexedSessionRepository mongoSessionRepository(MongoOperations mongoOperations) {
Expand Down Expand Up @@ -156,7 +156,7 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
}

@Autowired(required = false)
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
public void setIndexResolver(IndexResolver<Session> indexResolver) {
this.indexResolver = indexResolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.server.SpringWebSessionConfiguration;
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
import org.springframework.session.data.mongo.MongoSession;
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
Expand Down Expand Up @@ -72,7 +72,7 @@ public class ReactiveMongoWebSessionConfiguration

private ClassLoader classLoader;

private IndexResolver<MongoSession> indexResolver;
private IndexResolver<Session> indexResolver;

@Bean
public ReactiveMongoSessionRepository reactiveMongoSessionRepository(ReactiveMongoOperations operations) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public void setSessionRepositoryCustomizers(
}

@Autowired(required = false)
public void setIndexResolver(IndexResolver<MongoSession> indexResolver) {
public void setIndexResolver(IndexResolver<Session> indexResolver) {
this.indexResolver = indexResolver;
}

Expand Down
Loading