Refactor WebSession#getAttribute options
Issue: SPR-15718
This commit is contained in:
@@ -19,10 +19,12 @@ 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;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Main contract for using a server-side session that provides access to session
|
||||
* attributes across HTTP requests.
|
||||
@@ -48,12 +50,42 @@ public interface WebSession {
|
||||
Map<String, Object> getAttributes();
|
||||
|
||||
/**
|
||||
* Return the attribute value if present.
|
||||
* Return the session attribute value if present.
|
||||
* @param name the attribute name
|
||||
* @param <T> the attribute type
|
||||
* @return the attribute value
|
||||
*/
|
||||
<T> Optional<T> getAttribute(String name);
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
default <T> T getAttribute(String name) {
|
||||
return (T) getAttributes().get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the session attribute value or if not present raise an
|
||||
* {@link IllegalArgumentException}.
|
||||
* @param name the attribute name
|
||||
* @param <T> the attribute type
|
||||
* @return the attribute value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getRequiredAttribute(String name) {
|
||||
T value = getAttribute(name);
|
||||
Assert.notNull(value, "Required attribute '" + name + "' is missing.");
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the session attribute value, or a default, fallback value.
|
||||
* @param name the attribute name
|
||||
* @param defaultValue a default value to return instead
|
||||
* @param <T> the attribute type
|
||||
* @return the attribute value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getAttributeOrDefault(String name, T defaultValue) {
|
||||
return (T) getAttributes().getOrDefault(name, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force the creation of a session causing the session id to be sent when
|
||||
|
||||
@@ -20,7 +20,6 @@ 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;
|
||||
@@ -108,11 +107,6 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user