From d601e270fc4099e2167dd9b72f1054e6b293e76b Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Fri, 2 Aug 2019 15:16:11 -0500 Subject: [PATCH] Delombok Spring Session for MongoDB. --- pom.xml | 9 +--- .../MongoOperationsSessionRepository.java | 35 +++++++-------- .../session/data/mongo/MongoSession.java | 41 +++++++++++++---- ...ctiveMongoOperationsSessionRepository.java | 44 ++++++++++++++----- 4 files changed, 80 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index 068ec16..408ba6b 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,6 @@ 2.9.9.20190727 3.0.2 5.5.1 - 1.18.6 2.25.1 3.11.0-rc0 1.12.0-rc0 @@ -518,13 +517,6 @@ spring-security-core - - org.projectlombok - lombok - ${lombok.version} - provided - - @@ -635,6 +627,7 @@ + org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java b/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java index 9e595ca..16f4952 100644 --- a/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java +++ b/src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java @@ -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, 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; + } } diff --git a/src/main/java/org/springframework/session/data/mongo/MongoSession.java b/src/main/java/org/springframework/session/data/mongo/MongoSession.java index 4a9f69d..9f2d35b 100644 --- a/src/main/java/org/springframework/session/data/mongo/MongoSession.java +++ b/src/main/java/org/springframework/session/data/mongo/MongoSession.java @@ -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 attrs = new HashMap<>(); public MongoSession() { @@ -90,7 +86,6 @@ public class MongoSession implements Session { } public Set 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; + } } diff --git a/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java b/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java index 48cbb0b..b38c282 100644 --- a/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java +++ b/src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java @@ -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 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; + } }