Delombok Spring Session for MongoDB.

This commit is contained in:
Greg Turnquist
2019-08-02 15:16:11 -05:00
parent 579868249d
commit d601e270fc
4 changed files with 80 additions and 49 deletions

View File

@@ -74,7 +74,6 @@
<jackson-bom.version>2.9.9.20190727</jackson-bom.version>
<jsr305.version>3.0.2</jsr305.version>
<junit-bom.version>5.5.1</junit-bom.version>
<lombok.version>1.18.6</lombok.version>
<mockito.version>2.25.1</mockito.version>
<mongo.version>3.11.0-rc0</mongo.version>
<mongo-reactivestreams.version>1.12.0-rc0</mongo-reactivestreams.version>
@@ -518,13 +517,6 @@
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
@@ -635,6 +627,7 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

View File

@@ -13,13 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.mongo;
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
import lombok.Setter;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
@@ -52,7 +49,6 @@ import org.springframework.session.events.SessionExpiredEvent;
*/
public class MongoOperationsSessionRepository
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
/**
* The default time period in seconds in which a session will expire.
*/
@@ -63,10 +59,9 @@ public class MongoOperationsSessionRepository
public static final String DEFAULT_COLLECTION_NAME = "sessions";
private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class);
private final MongoOperations mongoOperations;
@Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
@Setter private String collectionName = DEFAULT_COLLECTION_NAME;
@Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
private String collectionName = DEFAULT_COLLECTION_NAME;
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
private ApplicationEventPublisher eventPublisher;
@@ -76,15 +71,11 @@ public class MongoOperationsSessionRepository
@Override
public MongoSession createSession() {
MongoSession session = new MongoSession();
if (this.maxInactiveIntervalInSeconds != null) {
session.setMaxInactiveInterval(Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
}
publishEvent(new SessionCreatedEvent(this, session));
return session;
}
@@ -107,10 +98,8 @@ public class MongoOperationsSessionRepository
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
if (session != null && session.isExpired()) {
publishEvent(new SessionExpiredEvent(this, session));
deleteById(id);
return null;
}
@@ -129,8 +118,7 @@ public class MongoOperationsSessionRepository
return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue))
.map(query -> this.mongoOperations.find(query, Document.class, this.collectionName))
.orElse(Collections.emptyList()) //
.stream() //
.orElse(Collections.emptyList()).stream()
.map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession))
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
}
@@ -139,19 +127,16 @@ public class MongoOperationsSessionRepository
public void deleteById(String id) {
Optional.ofNullable(findSession(id)).ifPresent(document -> {
MongoSession session = convertToSession(this.mongoSessionConverter, document);
if (session != null) {
publishEvent(new SessionDeletedEvent(this, session));
}
this.mongoOperations.remove(document, this.collectionName);
});
}
@Override
public void afterPropertiesSet() {
IndexOperations indexOperations = this.mongoOperations.indexOps(this.collectionName);
this.mongoSessionConverter.ensureIndexes(indexOperations);
}
@@ -167,7 +152,6 @@ public class MongoOperationsSessionRepository
}
private void publishEvent(ApplicationEvent event) {
try {
this.eventPublisher.publishEvent(event);
} catch (Throwable ex) {
@@ -175,4 +159,15 @@ public class MongoOperationsSessionRepository
}
}
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
}
public void setCollectionName(final String collectionName) {
this.collectionName = collectionName;
}
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
this.mongoSessionConverter = mongoSessionConverter;
}
}

View File

@@ -15,15 +15,12 @@
*/
package org.springframework.session.data.mongo;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -38,19 +35,18 @@ import org.springframework.session.Session;
* @author Greg Turnquist
* @since 1.2
*/
@EqualsAndHashCode(of = { "id" })
public class MongoSession implements Session {
/**
* Mongo doesn't support {@literal dot} in field names. We replace it with a very rarely used character
*/
private static final char DOT_COVER_CHAR = '\uF607';
private static final char DOT_COVER_CHAR = '';
@Getter private String id;
private String id;
private long createdMillis = System.currentTimeMillis();
private long accessedMillis;
private long intervalSeconds;
@Getter @Setter private Date expireAt;
private Date expireAt;
private Map<String, Object> attrs = new HashMap<>();
public MongoSession() {
@@ -90,7 +86,6 @@ public class MongoSession implements Session {
}
public Set<String> getAttributeNames() {
return this.attrs.keySet().stream().map(MongoSession::uncoverDot).collect(Collectors.toSet());
}
@@ -136,4 +131,32 @@ public class MongoSession implements Session {
public boolean isExpired() {
return this.intervalSeconds >= 0 && new Date().after(this.expireAt);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
MongoSession that = (MongoSession) o;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String getId() {
return this.id;
}
public Date getExpireAt() {
return this.expireAt;
}
public void setExpireAt(final Date expireAt) {
this.expireAt = expireAt;
}
}

View File

@@ -17,9 +17,6 @@ package org.springframework.session.data.mongo;
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
import lombok.Getter;
import lombok.Setter;
import java.time.Duration;
import org.bson.Document;
@@ -59,12 +56,11 @@ public class ReactiveMongoOperationsSessionRepository
private final ReactiveMongoOperations mongoOperations;
@Getter @Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
@Getter @Setter private String collectionName = DEFAULT_COLLECTION_NAME;
@Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
private String collectionName = DEFAULT_COLLECTION_NAME;
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
@Setter private MongoOperations blockingMongoOperations;
private MongoOperations blockingMongoOperations;
private ApplicationEventPublisher eventPublisher;
public ReactiveMongoOperationsSessionRepository(ReactiveMongoOperations mongoOperations) {
@@ -136,9 +132,10 @@ public class ReactiveMongoOperationsSessionRepository
@Override
public Mono<Void> deleteById(String id) {
return findSession(id)
.flatMap(document -> this.mongoOperations.remove(document, this.collectionName).then(Mono.just(document)))
.map(document -> convertToSession(this.mongoSessionConverter, document))
return findSession(id) //
.flatMap(document -> this.mongoOperations.remove(document, this.collectionName) //
.then(Mono.just(document))) //
.map(document -> convertToSession(this.mongoSessionConverter, document)) //
.doOnSuccess(mongoSession -> publishEvent(new SessionDeletedEvent(this, mongoSession))) //
.then();
}
@@ -151,7 +148,6 @@ public class ReactiveMongoOperationsSessionRepository
public void afterPropertiesSet() {
if (this.blockingMongoOperations != null) {
IndexOperations indexOperations = this.blockingMongoOperations.indexOps(this.collectionName);
this.mongoSessionConverter.ensureIndexes(indexOperations);
}
@@ -174,4 +170,28 @@ public class ReactiveMongoOperationsSessionRepository
logger.error("Error publishing " + event + ".", ex);
}
}
public Integer getMaxInactiveIntervalInSeconds() {
return this.maxInactiveIntervalInSeconds;
}
public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
}
public String getCollectionName() {
return this.collectionName;
}
public void setCollectionName(final String collectionName) {
this.collectionName = collectionName;
}
public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) {
this.mongoSessionConverter = mongoSessionConverter;
}
public void setBlockingMongoOperations(final MongoOperations blockingMongoOperations) {
this.blockingMongoOperations = blockingMongoOperations;
}
}