Polishing contribution

Closes gh-29691
This commit is contained in:
rstoyanchev 2023-02-27 17:37:28 +00:00
parent 4dbe9d6709
commit c859211f7a
2 changed files with 34 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,33 +48,35 @@ public class ServerWebExchangeContextFilter implements WebFilter {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange) return chain.filter(exchange)
.contextWrite(cxt -> cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange)); .contextWrite(context -> context.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
} }
/** /**
* Access the {@link ServerWebExchange} from the Reactor Context, if available, * Access the {@link ServerWebExchange} from a Reactor {@link ContextView},
* which is if {@link ServerWebExchangeContextFilter} is configured for use * if available, which is generally the case when
* and the give context was obtained from a request processing chain. * {@link ServerWebExchangeContextFilter} is present in the filter chain.
* @param context the context in which to access the exchange * @param contextView the contextView to get the exchange from
* @return the exchange * @return an {@link Optional} with the exchange if found
* @since 6.0.6
*/ */
public static Optional<ServerWebExchange> getExchange(ContextView context) { public static Optional<ServerWebExchange> getExchange(ContextView contextView) {
return contextView.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
}
/**
* Access the {@link ServerWebExchange} from a Reactor {@link Context},
* if available, which is generally the case when
* {@link ServerWebExchangeContextFilter} is present in the filter chain.
* @param context the context to get the exchange from
* @return an {@link Optional} with the exchange if found
* @deprecated in favor of using {@link #getExchange(ContextView)} which
* accepts a {@link ContextView} instead of {@link Context}, reflecting the
* fact that the {@code ContextView} is needed only for reading.
*/
@Deprecated(since = "6.0.6", forRemoval = true)
public static Optional<ServerWebExchange> get(Context context) {
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE); return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
} }
/**
* Access the {@link ServerWebExchange} from the Reactor Context, if available,
* which is if {@link ServerWebExchangeContextFilter} is configured for use
* and the give context was obtained from a request processing chain.
* @param context the context in which to access the exchange
* @return the exchange
* @deprecated use {@link #getExchange(ContextView)}
*/
@Deprecated(since = "6.0.6")
public static Optional<ServerWebExchange> get(Context context) {
return getExchange(context);
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
@ -36,16 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class ServerWebExchangeContextFilterTests { class ServerWebExchangeContextFilterTests {
@Test @Test
void extractServerWebExchangeFromContext() { void extractServerWebExchangeFromContext() {
MyService service = new MyService(); MyService service = new MyService();
HttpHandler httpHandler = WebHttpHandlerBuilder WebHttpHandlerBuilder
.webHandler(exchange -> service.service().then()) .webHandler(exchange -> service.service().then())
.filter(new ServerWebExchangeContextFilter()) .filter(new ServerWebExchangeContextFilter())
.build(); .build()
.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
httpHandler.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
.block(Duration.ofSeconds(5)); .block(Duration.ofSeconds(5));
assertThat(service.getExchange()).isNotNull(); assertThat(service.getExchange()).isNotNull();
@ -56,15 +55,15 @@ class ServerWebExchangeContextFilterTests {
private final AtomicReference<ServerWebExchange> exchangeRef = new AtomicReference<>(); private final AtomicReference<ServerWebExchange> exchangeRef = new AtomicReference<>();
public ServerWebExchange getExchange() { public ServerWebExchange getExchange() {
return this.exchangeRef.get(); return this.exchangeRef.get();
} }
public Mono<String> service() { public Mono<String> service() {
return Mono.just("result").contextWrite(context -> { return Mono.just("result")
ServerWebExchangeContextFilter.getExchange(context).ifPresent(exchangeRef::set); .transformDeferredContextual((mono, contextView) -> {
return context; ServerWebExchangeContextFilter.getExchange(contextView).ifPresent(exchangeRef::set);
return mono;
}); });
} }
} }