|
15 | 15 | */ |
16 | 16 | package org.springframework.session.data.mongo; |
17 | 17 |
|
18 | | -import static org.springframework.session.data.mongo.MongoSessionUtils.*; |
19 | | - |
20 | | -import java.time.Duration; |
21 | | - |
22 | | -import org.bson.Document; |
23 | | -import org.slf4j.Logger; |
24 | | -import org.slf4j.LoggerFactory; |
25 | | -import reactor.core.publisher.Mono; |
26 | | -import org.springframework.beans.factory.InitializingBean; |
27 | | -import org.springframework.context.ApplicationEvent; |
28 | | -import org.springframework.context.ApplicationEventPublisher; |
29 | | -import org.springframework.context.ApplicationEventPublisherAware; |
30 | | -import org.springframework.data.mongodb.core.MongoOperations; |
31 | 18 | import org.springframework.data.mongodb.core.ReactiveMongoOperations; |
32 | | -import org.springframework.data.mongodb.core.index.IndexOperations; |
33 | 19 | import org.springframework.session.ReactiveSessionRepository; |
34 | | -import org.springframework.session.events.SessionCreatedEvent; |
35 | | -import org.springframework.session.events.SessionDeletedEvent; |
36 | | - |
37 | | -import com.mongodb.DBObject; |
38 | 20 |
|
39 | 21 | /** |
| 22 | + * This {@link ReactiveSessionRepository} implementation is kept to support migration to |
| 23 | + * {@link ReactiveMongoSessionRepository} in a backwards compatible manner. |
| 24 | + * |
40 | 25 | * @author Greg Turnquist |
| 26 | + * @deprecated since 2.2.0 in favor of {@link ReactiveMongoSessionRepository}. |
41 | 27 | */ |
42 | | -public class ReactiveMongoOperationsSessionRepository |
43 | | - implements ReactiveSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean { |
44 | | - |
45 | | - /** |
46 | | - * The default time period in seconds in which a session will expire. |
47 | | - */ |
48 | | - public static final int DEFAULT_INACTIVE_INTERVAL = 1800; |
49 | | - |
50 | | - /** |
51 | | - * The default collection name for storing session. |
52 | | - */ |
53 | | - public static final String DEFAULT_COLLECTION_NAME = "sessions"; |
54 | | - |
55 | | - private static final Logger logger = LoggerFactory.getLogger(ReactiveMongoOperationsSessionRepository.class); |
56 | | - |
57 | | - private final ReactiveMongoOperations mongoOperations; |
58 | | - |
59 | | - private Integer maxInactiveIntervalInSeconds = DEFAULT_INACTIVE_INTERVAL; |
60 | | - private String collectionName = DEFAULT_COLLECTION_NAME; |
61 | | - private AbstractMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter( |
62 | | - Duration.ofSeconds(this.maxInactiveIntervalInSeconds)); |
63 | | - private MongoOperations blockingMongoOperations; |
64 | | - private ApplicationEventPublisher eventPublisher; |
| 28 | +@Deprecated |
| 29 | +public class ReactiveMongoOperationsSessionRepository extends ReactiveMongoSessionRepository { |
65 | 30 |
|
66 | 31 | public ReactiveMongoOperationsSessionRepository(ReactiveMongoOperations mongoOperations) { |
67 | | - this.mongoOperations = mongoOperations; |
68 | | - } |
69 | | - |
70 | | - /** |
71 | | - * Creates a new {@link MongoSession} that is capable of being persisted by this {@link ReactiveSessionRepository}. |
72 | | - * <p> |
73 | | - * This allows optimizations and customizations in how the {@link MongoSession} is persisted. For example, the |
74 | | - * implementation returned might keep track of the changes ensuring that only the delta needs to be persisted on a |
75 | | - * save. |
76 | | - * </p> |
77 | | - * |
78 | | - * @return a new {@link MongoSession} that is capable of being persisted by this {@link ReactiveSessionRepository} |
79 | | - */ |
80 | | - @Override |
81 | | - public Mono<MongoSession> createSession() { |
82 | | - |
83 | | - return Mono.justOrEmpty(this.maxInactiveIntervalInSeconds) // |
84 | | - .map(MongoSession::new) // |
85 | | - .doOnNext(mongoSession -> publishEvent(new SessionCreatedEvent(this, mongoSession))) // |
86 | | - .switchIfEmpty(Mono.just(new MongoSession())); |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Ensures the {@link MongoSession} created by {@link ReactiveSessionRepository#createSession()} is saved. |
91 | | - * <p> |
92 | | - * Some implementations may choose to save as the {@link MongoSession} is updated by returning a {@link MongoSession} |
93 | | - * that immediately persists any changes. In this case, this method may not actually do anything. |
94 | | - * </p> |
95 | | - * |
96 | | - * @param session the {@link MongoSession} to save |
97 | | - */ |
98 | | - @Override |
99 | | - public Mono<Void> save(MongoSession session) { |
100 | | - |
101 | | - DBObject dbObject = convertToDBObject(this.mongoSessionConverter, session); |
102 | | - if (dbObject != null) { |
103 | | - return this.mongoOperations.save(dbObject, this.collectionName).then(); |
104 | | - } else { |
105 | | - return Mono.empty(); |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - /** |
110 | | - * Gets the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no |
111 | | - * {@link MongoSession} is found. |
112 | | - * |
113 | | - * @param id the {@link MongoSession#getId()} to lookup |
114 | | - * @return the {@link MongoSession} by the {@link MongoSession#getId()} or {@link Mono#empty()} if no |
115 | | - * {@link MongoSession} is found. |
116 | | - */ |
117 | | - @Override |
118 | | - public Mono<MongoSession> findById(String id) { |
119 | | - |
120 | | - return findSession(id) // |
121 | | - .map(document -> convertToSession(this.mongoSessionConverter, document)) // |
122 | | - .filter(mongoSession -> !mongoSession.isExpired()) // |
123 | | - .switchIfEmpty(Mono.defer(() -> this.deleteById(id).then(Mono.empty()))); |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Deletes the {@link MongoSession} with the given {@link MongoSession#getId()} or does nothing if the |
128 | | - * {@link MongoSession} is not found. |
129 | | - * |
130 | | - * @param id the {@link MongoSession#getId()} to delete |
131 | | - */ |
132 | | - @Override |
133 | | - public Mono<Void> deleteById(String id) { |
134 | | - |
135 | | - return findSession(id) // |
136 | | - .flatMap(document -> this.mongoOperations.remove(document, this.collectionName) // |
137 | | - .then(Mono.just(document))) // |
138 | | - .map(document -> convertToSession(this.mongoSessionConverter, document)) // |
139 | | - .doOnNext(mongoSession -> publishEvent(new SessionDeletedEvent(this, mongoSession))) // |
140 | | - .then(); |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * Do not use {@link org.springframework.data.mongodb.core.index.ReactiveIndexOperations} to ensure indexes exist. |
145 | | - * Instead, get a blocking {@link IndexOperations} and use that instead, if possible. |
146 | | - */ |
147 | | - @Override |
148 | | - public void afterPropertiesSet() { |
149 | | - |
150 | | - if (this.blockingMongoOperations != null) { |
151 | | - IndexOperations indexOperations = this.blockingMongoOperations.indexOps(this.collectionName); |
152 | | - this.mongoSessionConverter.ensureIndexes(indexOperations); |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - private Mono<Document> findSession(String id) { |
157 | | - return this.mongoOperations.findById(id, Document.class, this.collectionName); |
158 | | - } |
159 | | - |
160 | | - @Override |
161 | | - public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { |
162 | | - this.eventPublisher = eventPublisher; |
163 | | - } |
164 | | - |
165 | | - private void publishEvent(ApplicationEvent event) { |
166 | | - |
167 | | - try { |
168 | | - this.eventPublisher.publishEvent(event); |
169 | | - } catch (Throwable ex) { |
170 | | - logger.error("Error publishing " + event + ".", ex); |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - public Integer getMaxInactiveIntervalInSeconds() { |
175 | | - return this.maxInactiveIntervalInSeconds; |
176 | | - } |
177 | | - |
178 | | - public void setMaxInactiveIntervalInSeconds(final Integer maxInactiveIntervalInSeconds) { |
179 | | - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; |
180 | | - } |
181 | | - |
182 | | - public String getCollectionName() { |
183 | | - return this.collectionName; |
184 | | - } |
185 | | - |
186 | | - public void setCollectionName(final String collectionName) { |
187 | | - this.collectionName = collectionName; |
188 | | - } |
189 | | - |
190 | | - public void setMongoSessionConverter(final AbstractMongoSessionConverter mongoSessionConverter) { |
191 | | - this.mongoSessionConverter = mongoSessionConverter; |
192 | | - } |
193 | | - |
194 | | - public void setBlockingMongoOperations(final MongoOperations blockingMongoOperations) { |
195 | | - this.blockingMongoOperations = blockingMongoOperations; |
| 32 | + super(mongoOperations); |
196 | 33 | } |
197 | 34 | } |
0 commit comments