Browse Source

Polishing contribution

See gh-24518
pull/24572/head
Rossen Stoyanchev 5 years ago
parent
commit
de608a08ed
  1. 21
      spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java

21
spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@ -24,7 +24,6 @@ import reactor.core.publisher.Flux; @@ -24,7 +24,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.util.Assert;
import org.springframework.web.context.request.async.WebAsyncManager;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
@ -41,6 +40,7 @@ public class DefaultWebSessionManager implements WebSessionManager { @@ -41,6 +40,7 @@ public class DefaultWebSessionManager implements WebSessionManager {
private static final Log logger = LogFactory.getLog(DefaultWebSessionManager.class);
private WebSessionIdResolver sessionIdResolver = new CookieWebSessionIdResolver();
private WebSessionStore sessionStore = new InMemoryWebSessionStore();
@ -84,15 +84,18 @@ public class DefaultWebSessionManager implements WebSessionManager { @@ -84,15 +84,18 @@ public class DefaultWebSessionManager implements WebSessionManager {
@Override
public Mono<WebSession> getSession(ServerWebExchange exchange) {
return Mono.defer(() -> retrieveSession(exchange)
.switchIfEmpty(Mono.defer(() -> {
if (logger.isDebugEnabled()) {
logger.debug("Did not find an existing WebSession, creating a new one.");
}
return this.sessionStore.createWebSession();
}))
.switchIfEmpty(createWebSession())
.doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session))));
}
private Mono<WebSession> createWebSession() {
Mono<WebSession> session = this.sessionStore.createWebSession();
if (logger.isDebugEnabled()) {
session = session.doOnNext(s -> logger.debug("Created new WebSession."));
}
return session;
}
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) {
return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange))
.concatMap(this.sessionStore::retrieveSession)
@ -106,7 +109,7 @@ public class DefaultWebSessionManager implements WebSessionManager { @@ -106,7 +109,7 @@ public class DefaultWebSessionManager implements WebSessionManager {
if (!ids.isEmpty()) {
// Expired on retrieve or while processing request, or invalidated..
if (logger.isDebugEnabled()) {
logger.debug("Expiring existing sessionIds while saving session.");
logger.debug("WebSession expired or has been invalidated");
}
this.sessionIdResolver.expireSession(exchange);
}

Loading…
Cancel
Save