Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 98aae89

Browse files
authored
Polishing (#38)
1 parent cecb803 commit 98aae89

File tree

6 files changed

+59
-133
lines changed

6 files changed

+59
-133
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<hamcrest.version>1.3</hamcrest.version>
7575
<jackson.version>2.9.1</jackson.version>
7676
<junit.version>4.12</junit.version>
77+
<lombok.version>1.18.0</lombok.version>
7778
<mockito.version>2.18.3</mockito.version>
7879
<mongo.version>3.8.0</mongo.version>
7980
<mongo-reactivestreams.version>1.9.0</mongo-reactivestreams.version>
@@ -541,6 +542,12 @@
541542
<artifactId>spring-security-core</artifactId>
542543
</dependency>
543544

545+
<dependency>
546+
<groupId>org.projectlombok</groupId>
547+
<artifactId>lombok</artifactId>
548+
<version>${lombok.version}</version>
549+
</dependency>
550+
544551
<!-- Test dependencies -->
545552

546553
<dependency>

src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter
4747

4848
private static final Log LOG = LogFactory.getLog(AbstractMongoSessionConverter.class);
4949

50-
protected static final String EXPIRE_AT_FIELD_NAME = "expireAt";
50+
static final String EXPIRE_AT_FIELD_NAME = "expireAt";
5151

5252
private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
5353

src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ private static class MongoIdNamingStrategy extends PropertyNamingStrategy.Proper
145145

146146
@Override
147147
public String translate(String propertyName) {
148-
if (propertyName.equals("id")) {
149-
return "_id";
150-
} else if (propertyName.equals("_id")) {
151-
return "id";
152-
} else {
153-
return propertyName;
148+
149+
switch (propertyName) {
150+
case "id":
151+
return "_id";
152+
case "_id":
153+
return "id";
154+
default:
155+
return propertyName;
154156
}
155157
}
156158
}

src/main/java/org/springframework/session/data/mongo/MongoOperationsSessionRepository.java

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
2020

21+
import lombok.Setter;
22+
2123
import java.time.Duration;
2224
import java.util.Collections;
23-
import java.util.HashMap;
24-
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Optional;
27+
import java.util.stream.Collectors;
2728

2829
import org.bson.Document;
2930
import org.slf4j.Logger;
@@ -34,14 +35,11 @@
3435
import org.springframework.context.ApplicationEventPublisherAware;
3536
import org.springframework.data.mongodb.core.MongoOperations;
3637
import org.springframework.data.mongodb.core.index.IndexOperations;
37-
import org.springframework.data.mongodb.core.query.Query;
3838
import org.springframework.session.FindByIndexNameSessionRepository;
3939
import org.springframework.session.events.SessionCreatedEvent;
4040
import org.springframework.session.events.SessionDeletedEvent;
4141
import org.springframework.session.events.SessionExpiredEvent;
4242

43-
import com.mongodb.DBObject;
44-
4543
/**
4644
* Session repository implementation which stores sessions in Mongo. Uses
4745
* {@link AbstractMongoSessionConverter} to transform session objects from/to native Mongo
@@ -57,6 +55,8 @@
5755
public class MongoOperationsSessionRepository
5856
implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {
5957

58+
private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class);
59+
6060
/**
6161
* The default time period in seconds in which a session will expire.
6262
*/
@@ -67,13 +67,11 @@ public class MongoOperationsSessionRepository
6767
*/
6868
public static final String DEFAULT_COLLECTION_NAME = "sessions";
6969

70-
private static final Logger logger = LoggerFactory.getLogger(MongoOperationsSessionRepository.class);
71-
7270
private final MongoOperations mongoOperations;
7371

74-
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
75-
private String collectionName = DEFAULT_COLLECTION_NAME;
76-
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
72+
@Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
73+
@Setter private String collectionName = DEFAULT_COLLECTION_NAME;
74+
@Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
7775
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
7876
private ApplicationEventPublisher eventPublisher;
7977

@@ -91,15 +89,13 @@ public MongoSession createSession() {
9189
}
9290

9391
publishEvent(new SessionCreatedEvent(this, session));
94-
92+
9593
return session;
9694
}
9795

9896
@Override
9997
public void save(MongoSession session) {
100-
101-
DBObject sessionDbObject = convertToDBObject(this.mongoSessionConverter, session);
102-
this.mongoOperations.save(sessionDbObject, this.collectionName);
98+
this.mongoOperations.save(convertToDBObject(this.mongoSessionConverter, session), this.collectionName);
10399
}
104100

105101
@Override
@@ -114,8 +110,10 @@ public MongoSession findById(String id) {
114110
MongoSession session = convertToSession(this.mongoSessionConverter, sessionWrapper);
115111

116112
if (session.isExpired()) {
113+
117114
publishEvent(new SessionExpiredEvent(this, session));
118115
deleteById(id);
116+
119117
return null;
120118
}
121119

@@ -134,26 +132,17 @@ public MongoSession findById(String id) {
134132
@Override
135133
public Map<String, MongoSession> findByIndexNameAndIndexValue(String indexName, String indexValue) {
136134

137-
HashMap<String, MongoSession> result = new HashMap<String, MongoSession>();
138-
139-
Query query = this.mongoSessionConverter.getQueryForIndex(indexName, indexValue);
140-
141-
if (query == null) {
142-
return Collections.emptyMap();
135+
return Optional.ofNullable(this.mongoSessionConverter.getQueryForIndex(indexName, indexValue))
136+
.map(query -> this.mongoOperations.find(query, Document.class, this.collectionName))
137+
.orElse(Collections.emptyList())
138+
.stream()
139+
.map(dbSession -> convertToSession(this.mongoSessionConverter, dbSession))
140+
.collect(Collectors.toMap(MongoSession::getId, mapSession -> mapSession));
143141
}
144142

145-
List<Document> mapSessions = this.mongoOperations.find(query, Document.class, this.collectionName);
146-
147-
for (Document dbSession : mapSessions) {
148-
MongoSession mapSession = convertToSession(this.mongoSessionConverter, dbSession);
149-
result.put(mapSession.getId(), mapSession);
150-
}
151-
152-
return result;
153-
}
154-
155143
@Override
156144
public void deleteById(String id) {
145+
157146
Optional.ofNullable(findSession(id))
158147
.ifPresent(document -> {
159148
publishEvent(new SessionDeletedEvent(this, convertToSession(this.mongoSessionConverter, document)));
@@ -168,22 +157,10 @@ public void afterPropertiesSet() {
168157
this.mongoSessionConverter.ensureIndexes(indexOperations);
169158
}
170159

171-
Document findSession(String id) {
160+
private Document findSession(String id) {
172161
return this.mongoOperations.findById(id, Document.class, this.collectionName);
173162
}
174163

175-
public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) {
176-
this.mongoSessionConverter = mongoSessionConverter;
177-
}
178-
179-
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
180-
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
181-
}
182-
183-
public void setCollectionName(String collectionName) {
184-
this.collectionName = collectionName;
185-
}
186-
187164
@Override
188165
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
189166
this.eventPublisher = eventPublisher;

src/main/java/org/springframework/session/data/mongo/MongoSession.java

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
package org.springframework.session.data.mongo;
1717

18+
import lombok.EqualsAndHashCode;
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
1822
import java.time.Duration;
1923
import java.time.Instant;
2024
import java.util.Date;
2125
import java.util.HashMap;
22-
import java.util.HashSet;
2326
import java.util.Map;
2427
import java.util.Set;
2528
import java.util.UUID;
29+
import java.util.stream.Collectors;
2630

2731
import org.springframework.session.Session;
2832

@@ -33,19 +37,20 @@
3337
* @author Greg Turnquist
3438
* @since 1.2
3539
*/
40+
@EqualsAndHashCode(of = {"id"})
3641
public class MongoSession implements Session {
3742

3843
/**
3944
* Mongo doesn't support {@literal dot} in field names. We replace it with a very rarely used character
4045
*/
4146
private static final char DOT_COVER_CHAR = '\uF607';
4247

43-
private String id;
48+
@Getter private String id;
4449
private long createdMillis = System.currentTimeMillis();
4550
private long accessedMillis;
4651
private long intervalSeconds;
47-
private Date expireAt;
48-
private Map<String, Object> attrs = new HashMap<String, Object>();
52+
@Getter @Setter private Date expireAt;
53+
private Map<String, Object> attrs = new HashMap<>();
4954

5055
public MongoSession() {
5156
this(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
@@ -62,35 +67,23 @@ public MongoSession(String id, long maxInactiveIntervalInSeconds) {
6267
setLastAccessedTime(Instant.ofEpochMilli(this.createdMillis));
6368
}
6469

65-
public String getId() {
66-
return this.id;
67-
}
68-
6970
public String changeSessionId() {
70-
String changedId = generateId();
71+
72+
String changedId = UUID.randomUUID().toString();
7173
this.id = changedId;
7274
return changedId;
7375
}
7476

75-
private String generateId() {
76-
return UUID.randomUUID().toString();
77-
}
78-
79-
8077
@Override
8178
public <T> T getAttribute(String attributeName) {
8279
return (T) this.attrs.get(coverDot(attributeName));
8380
}
8481

8582
public Set<String> getAttributeNames() {
8683

87-
HashSet<String> result = new HashSet<>();
88-
89-
for (String key : this.attrs.keySet()) {
90-
result.add(uncoverDot(key));
91-
}
92-
93-
return result;
84+
return this.attrs.keySet().stream()
85+
.map(MongoSession::uncoverDot)
86+
.collect(Collectors.toSet());
9487
}
9588

9689
public void setAttribute(String attributeName, Object attributeValue) {
@@ -136,39 +129,11 @@ public boolean isExpired() {
136129
return this.intervalSeconds >= 0 && new Date().after(this.expireAt);
137130
}
138131

139-
public Date getExpireAt() {
140-
return this.expireAt;
141-
}
142-
143-
public void setExpireAt(Date expireAt) {
144-
this.expireAt = expireAt;
145-
}
146-
147132
static String coverDot(String attributeName) {
148133
return attributeName.replace('.', DOT_COVER_CHAR);
149134
}
150135

151136
static String uncoverDot(String attributeName) {
152137
return attributeName.replace(DOT_COVER_CHAR, '.');
153138
}
154-
155-
@Override
156-
public boolean equals(Object o) {
157-
158-
if (this == o) {
159-
return true;
160-
}
161-
if (o == null || getClass() != o.getClass()) {
162-
return false;
163-
}
164-
165-
MongoSession that = (MongoSession) o;
166-
167-
return this.id.equals(that.id);
168-
}
169-
170-
@Override
171-
public int hashCode() {
172-
return this.id.hashCode();
173-
}
174139
}

src/main/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepository.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import static org.springframework.session.data.mongo.MongoSessionUtils.*;
1919

20+
import lombok.Getter;
21+
import lombok.Setter;
22+
2023
import java.time.Duration;
2124

2225
import org.bson.Document;
@@ -54,12 +57,12 @@ public class ReactiveMongoOperationsSessionRepository
5457

5558
private final ReactiveMongoOperations mongoOperations;
5659

57-
private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
58-
private String collectionName = DEFAULT_COLLECTION_NAME;
59-
private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
60+
@Getter @Setter private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL;
61+
@Getter @Setter private String collectionName = DEFAULT_COLLECTION_NAME;
62+
@Setter private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(
6063
Duration.ofSeconds(this.maxInactiveIntervalInSeconds));
6164

62-
private MongoOperations blockingMongoOperations;
65+
@Setter private MongoOperations blockingMongoOperations;
6366
private ApplicationEventPublisher eventPublisher;
6467

6568
public ReactiveMongoOperationsSessionRepository(ReactiveMongoOperations mongoOperations) {
@@ -110,11 +113,11 @@ public Mono<Void> save(MongoSession session) {
110113
}
111114

112115
/**
113-
* Gets the {@link MongoSession} by the {@link MongoSession#getId()} or null if no
116+
* Gets the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no
114117
* {@link MongoSession} is found.
115118
*
116119
* @param id the {@link MongoSession#getId()} to lookup
117-
* @return the {@link MongoSession} by the {@link MongoSession#getId()} or null if no
120+
* @return the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no
118121
* {@link MongoSession} is found.
119122
*/
120123
@Override
@@ -159,34 +162,6 @@ private Mono<Document> findSession(String id) {
159162
return this.mongoOperations.findById(id, Document.class, this.collectionName);
160163
}
161164

162-
public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) {
163-
this.mongoSessionConverter = mongoSessionConverter;
164-
}
165-
166-
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
167-
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
168-
}
169-
170-
public Integer getMaxInactiveIntervalInSeconds() {
171-
return maxInactiveIntervalInSeconds;
172-
}
173-
174-
public void setCollectionName(String collectionName) {
175-
this.collectionName = collectionName;
176-
}
177-
178-
public String getCollectionName() {
179-
return collectionName;
180-
}
181-
182-
public MongoOperations getBlockingMongoOperations() {
183-
return this.blockingMongoOperations;
184-
}
185-
186-
public void setBlockingMongoOperations(MongoOperations blockingMongoOperations) {
187-
this.blockingMongoOperations = blockingMongoOperations;
188-
}
189-
190165
@Override
191166
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
192167
this.eventPublisher = eventPublisher;

0 commit comments

Comments
 (0)