Add getAttribute to WebSession and ServerWebExchange

This commit is contained in:
Rossen Stoyanchev 2016-04-19 17:34:08 -04:00
parent d041b73c10
commit a173c78d98
4 changed files with 30 additions and 0 deletions

View File

@ -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<String, Object> getAttributes();
/**
* Return the request attribute value if present.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
<T> Optional<T> 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

View File

@ -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<String, Object> getAttributes();
/**
* Return the attribute value if present.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
<T> Optional<T> getAttribute(String name);
/**
* Force the creation of a session causing the session id to be sent when
* {@link #save()} is called.

View File

@ -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 <T> Optional<T> getAttribute(String name) {
return Optional.ofNullable((T) this.attributes.get(name));
}
@Override
public Mono<WebSession> getSession() {
if (this.sessionMono == null) {

View File

@ -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 <T> Optional<T> getAttribute(String name) {
return Optional.ofNullable((T) this.attributes.get(name));
}
@Override
public Instant getCreationTime() {
return this.creationTime;