From a173c78d989d1c70fd63625fb678e16a18312b88 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 19 Apr 2016 17:34:08 -0400 Subject: [PATCH] Add getAttribute to WebSession and ServerWebExchange --- .../springframework/web/server/ServerWebExchange.java | 9 +++++++++ .../java/org/springframework/web/server/WebSession.java | 9 +++++++++ .../web/server/adapter/DefaultServerWebExchange.java | 6 ++++++ .../web/server/session/DefaultWebSession.java | 6 ++++++ 4 files changed, 30 insertions(+) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java b/spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java index 144cc81953..a2b3857c1d 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/server/ServerWebExchange.java @@ -16,6 +16,7 @@ package org.springframework.web.server; import java.util.Map; +import java.util.Optional; import reactor.core.publisher.Mono; @@ -46,6 +47,14 @@ public interface ServerWebExchange { */ Map getAttributes(); + /** + * Return the request attribute value if present. + * @param name the attribute name + * @param the attribute type + * @return the attribute value + */ + Optional getAttribute(String name); + /** * Return the web session for the current request. Always guaranteed to * return an instance either matching to the session id requested by the diff --git a/spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java b/spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java index ca6554cbcf..136daccc15 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/server/WebSession.java @@ -18,6 +18,7 @@ package org.springframework.web.server; import java.time.Duration; import java.time.Instant; import java.util.Map; +import java.util.Optional; import reactor.core.publisher.Mono; @@ -44,6 +45,14 @@ public interface WebSession { */ Map getAttributes(); + /** + * Return the attribute value if present. + * @param name the attribute name + * @param the attribute type + * @return the attribute value + */ + Optional getAttribute(String name); + /** * Force the creation of a session causing the session id to be sent when * {@link #save()} is called. diff --git a/spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index ee6decb2eb..8e9807491c 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -16,6 +16,7 @@ package org.springframework.web.server.adapter; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import reactor.core.publisher.EmitterProcessor; @@ -78,6 +79,11 @@ public class DefaultServerWebExchange implements ServerWebExchange { return this.attributes; } + @Override @SuppressWarnings("unchecked") + public Optional getAttribute(String name) { + return Optional.ofNullable((T) this.attributes.get(name)); + } + @Override public Mono getSession() { if (this.sessionMono == null) { diff --git a/spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java b/spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java index 0301563994..146c4e0a8e 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSession.java @@ -20,6 +20,7 @@ import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -101,6 +102,11 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable { return this.attributes; } + @Override @SuppressWarnings("unchecked") + public Optional getAttribute(String name) { + return Optional.ofNullable((T) this.attributes.get(name)); + } + @Override public Instant getCreationTime() { return this.creationTime;