Refactor ServerWebExchange#getAttribute options
Issue: SPR-15718
This commit is contained in:
@@ -19,7 +19,6 @@ package org.springframework.web.server;
|
||||
import java.security.Principal;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -29,6 +28,7 @@ import org.springframework.http.codec.multipart.Part;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.server.i18n.LocaleContextResolver;
|
||||
|
||||
@@ -63,7 +63,37 @@ public interface ServerWebExchange {
|
||||
* @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 request 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 request 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the web session for the current request. Always guaranteed to
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.server;
|
||||
import java.security.Principal;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -76,11 +75,6 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
|
||||
return getDelegate().getAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Optional<T> getAttribute(String name) {
|
||||
return getDelegate().getAttribute(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<WebSession> getSession() {
|
||||
return getDelegate().getSession();
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -46,9 +45,9 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.i18n.LocaleContextResolver;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.i18n.LocaleContextResolver;
|
||||
import org.springframework.web.server.session.WebSessionManager;
|
||||
|
||||
/**
|
||||
@@ -178,11 +177,6 @@ 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() {
|
||||
return this.sessionMono;
|
||||
|
||||
Reference in New Issue
Block a user