From c7d54c8b525ff8f6dbaf667745010a3247b2e272 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 6 Sep 2017 11:01:22 -0400 Subject: [PATCH] Polish --- .../web/server/session/DefaultWebSession.java | 5 ++- .../session/DefaultWebSessionManager.java | 2 + .../session/HeaderWebSessionIdResolver.java | 22 +++++----- .../session/InMemoryWebSessionStore.java | 44 ++++++++++--------- .../web/server/session/WebSessionStore.java | 12 ++--- 5 files changed, 47 insertions(+), 38 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java b/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java index 74c4925cd5..ec6c17b8e7 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java @@ -60,7 +60,7 @@ class DefaultWebSession implements WebSession { /** - * Constructor for creating a brand, new session. + * Constructor for creating a new session instance. * @param idGenerator the session id generator * @param clock for access to current time */ @@ -86,7 +86,8 @@ class DefaultWebSession implements WebSession { } /** - * Constructor for creating a new session with an updated last access time. + * Copy constructor to re-create a session at the start of a new request + * refreshing the last access time of the session. * @param existingSession the existing session to copy * @param lastAccessTime the new last access time */ diff --git a/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java b/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java index 4a1cc17b58..fa50120554 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java @@ -40,6 +40,7 @@ public class DefaultWebSessionManager implements WebSessionManager { private WebSessionStore sessionStore = new InMemoryWebSessionStore(); + /** * Configure the id resolution strategy. *

By default an instance of {@link CookieWebSessionIdResolver}. @@ -74,6 +75,7 @@ public class DefaultWebSessionManager implements WebSessionManager { return this.sessionStore; } + @Override public Mono getSession(ServerWebExchange exchange) { return Mono.defer(() -> diff --git a/spring-web/src/main/java/org/springframework/web/server/session/HeaderWebSessionIdResolver.java b/spring-web/src/main/java/org/springframework/web/server/session/HeaderWebSessionIdResolver.java index 9ee5cffd61..54dafb43ce 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/HeaderWebSessionIdResolver.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/HeaderWebSessionIdResolver.java @@ -31,13 +31,13 @@ import org.springframework.web.server.ServerWebExchange; */ public class HeaderWebSessionIdResolver implements WebSessionIdResolver { - /** - * The default header name - */ + /** Default value for {@link #setHeaderName(String)}. */ public static final String DEFAULT_HEADER_NAME = "SESSION"; + private String headerName = DEFAULT_HEADER_NAME; + /** * Set the name of the session header to use for the session id. * The name is used to extract the session id from the request headers as @@ -50,6 +50,15 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver { this.headerName = headerName; } + /** + * Return the configured header name. + * @return the configured header name + */ + public String getHeaderName() { + return this.headerName; + } + + @Override public List resolveSessionIds(ServerWebExchange exchange) { HttpHeaders headers = exchange.getRequest().getHeaders(); @@ -67,11 +76,4 @@ public class HeaderWebSessionIdResolver implements WebSessionIdResolver { this.setSessionId(exchange, ""); } - /** - * Return the configured header name. - * @return the configured header name - */ - private String getHeaderName() { - return this.headerName; - } } diff --git a/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java b/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java index bd2a7a97ba..b32f7b42c1 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/InMemoryWebSessionStore.java @@ -39,11 +39,34 @@ public class InMemoryWebSessionStore implements WebSessionStore { private static final IdGenerator idGenerator = new JdkIdGenerator(); + private Clock clock = Clock.system(ZoneId.of("GMT")); private final Map sessions = new ConcurrentHashMap<>(); + /** + * Configure the {@link Clock} to use to set lastAccessTime on every created + * session and to calculate if it is expired. + *

This may be useful to align to different timezone or to set the clock + * back in a test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))} + * in order to simulate session expiration. + *

By default this is {@code Clock.system(ZoneId.of("GMT"))}. + * @param clock the clock to use + */ + public void setClock(Clock clock) { + Assert.notNull(clock, "'clock' is required."); + this.clock = clock; + } + + /** + * Return the configured clock for session lastAccessTime calculations. + */ + public Clock getClock() { + return this.clock; + } + + @Override public Mono retrieveSession(String id) { return (this.sessions.containsKey(id) ? Mono.just(this.sessions.get(id)) : Mono.empty()); @@ -70,27 +93,6 @@ public class InMemoryWebSessionStore implements WebSessionStore { }); } - /** - * Configure the {@link Clock} to use to set lastAccessTime on every created - * session and to calculate if it is expired. - *

This may be useful to align to different timezone or to set the clock - * back in a test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))} - * in order to simulate session expiration. - *

By default this is {@code Clock.system(ZoneId.of("GMT"))}. - * @param clock the clock to use - */ - public void setClock(Clock clock) { - Assert.notNull(clock, "'clock' is required."); - this.clock = clock; - } - - /** - * Return the configured clock for session lastAccessTime calculations. - */ - public Clock getClock() { - return this.clock; - } - private Mono changeSessionId(String oldId, WebSession session) { this.sessions.remove(oldId); this.sessions.put(session.getId(), session); diff --git a/spring-web/src/main/java/org/springframework/web/server/session/WebSessionStore.java b/spring-web/src/main/java/org/springframework/web/server/session/WebSessionStore.java index 26c064153b..4ff61176b3 100644 --- a/spring-web/src/main/java/org/springframework/web/server/session/WebSessionStore.java +++ b/spring-web/src/main/java/org/springframework/web/server/session/WebSessionStore.java @@ -19,8 +19,6 @@ import reactor.core.publisher.Mono; import org.springframework.web.server.WebSession; -import java.time.Instant; - /** * Strategy for {@link WebSession} persistence. * @@ -31,8 +29,12 @@ import java.time.Instant; public interface WebSessionStore { /** - * Creates the WebSession that can be stored by this WebSessionStore. - * @return the session + * Create a new WebSession. + *

Note that this does nothing more than create a new instance. + * The session can later be started explicitly via {@link WebSession#start()} + * or implicitly by adding attributes -- and then persisted via + * {@link WebSession#save()}. + * @return the created session instance */ Mono createWebSession(); @@ -51,7 +53,7 @@ public interface WebSessionStore { Mono removeSession(String sessionId); /** - * Update the last accessed time to now. + * Update the last accessed timestamp to "now". * @param webSession the session to update * @return the session with the updated last access time */