Add getAttribute to WebSession and ServerWebExchange
This commit is contained in:
parent
d041b73c10
commit
a173c78d98
|
|
@ -16,6 +16,7 @@
|
||||||
package org.springframework.web.server;
|
package org.springframework.web.server;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
|
@ -46,6 +47,14 @@ public interface ServerWebExchange {
|
||||||
*/
|
*/
|
||||||
Map<String, Object> getAttributes();
|
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 the web session for the current request. Always guaranteed to
|
||||||
* return an instance either matching to the session id requested by the
|
* return an instance either matching to the session id requested by the
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.server;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
|
@ -44,6 +45,14 @@ public interface WebSession {
|
||||||
*/
|
*/
|
||||||
Map<String, Object> getAttributes();
|
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
|
* Force the creation of a session causing the session id to be sent when
|
||||||
* {@link #save()} is called.
|
* {@link #save()} is called.
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
package org.springframework.web.server.adapter;
|
package org.springframework.web.server.adapter;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import reactor.core.publisher.EmitterProcessor;
|
import reactor.core.publisher.EmitterProcessor;
|
||||||
|
|
@ -78,6 +79,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
||||||
return this.attributes;
|
return this.attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override @SuppressWarnings("unchecked")
|
||||||
|
public <T> Optional<T> getAttribute(String name) {
|
||||||
|
return Optional.ofNullable((T) this.attributes.get(name));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<WebSession> getSession() {
|
public Mono<WebSession> getSession() {
|
||||||
if (this.sessionMono == null) {
|
if (this.sessionMono == null) {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.time.Clock;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
@ -101,6 +102,11 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable {
|
||||||
return this.attributes;
|
return this.attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override @SuppressWarnings("unchecked")
|
||||||
|
public <T> Optional<T> getAttribute(String name) {
|
||||||
|
return Optional.ofNullable((T) this.attributes.get(name));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Instant getCreationTime() {
|
public Instant getCreationTime() {
|
||||||
return this.creationTime;
|
return this.creationTime;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue