Skip to content

Commit c859211

Browse files
committed
Polishing contribution
Closes spring-projectsgh-29691
1 parent 4dbe9d6 commit c859211

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

spring-web/src/main/java/org/springframework/web/filter/reactive/ServerWebExchangeContextFilter.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,33 +48,35 @@ public class ServerWebExchangeContextFilter implements WebFilter {
4848
@Override
4949
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
5050
return chain.filter(exchange)
51-
.contextWrite(cxt -> cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
51+
.contextWrite(context -> context.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
5252
}
5353

5454

5555
/**
56-
* Access the {@link ServerWebExchange} from the Reactor Context, if available,
57-
* which is if {@link ServerWebExchangeContextFilter} is configured for use
58-
* and the give context was obtained from a request processing chain.
59-
* @param context the context in which to access the exchange
60-
* @return the exchange
56+
* Access the {@link ServerWebExchange} from a Reactor {@link ContextView},
57+
* if available, which is generally the case when
58+
* {@link ServerWebExchangeContextFilter} is present in the filter chain.
59+
* @param contextView the contextView to get the exchange from
60+
* @return an {@link Optional} with the exchange if found
61+
* @since 6.0.6
6162
*/
62-
public static Optional<ServerWebExchange> getExchange(ContextView context) {
63-
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
63+
public static Optional<ServerWebExchange> getExchange(ContextView contextView) {
64+
return contextView.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
6465
}
6566

66-
6767
/**
68-
* Access the {@link ServerWebExchange} from the Reactor Context, if available,
69-
* which is if {@link ServerWebExchangeContextFilter} is configured for use
70-
* and the give context was obtained from a request processing chain.
71-
* @param context the context in which to access the exchange
72-
* @return the exchange
73-
* @deprecated use {@link #getExchange(ContextView)}
68+
* Access the {@link ServerWebExchange} from a Reactor {@link Context},
69+
* if available, which is generally the case when
70+
* {@link ServerWebExchangeContextFilter} is present in the filter chain.
71+
* @param context the context to get the exchange from
72+
* @return an {@link Optional} with the exchange if found
73+
* @deprecated in favor of using {@link #getExchange(ContextView)} which
74+
* accepts a {@link ContextView} instead of {@link Context}, reflecting the
75+
* fact that the {@code ContextView} is needed only for reading.
7476
*/
75-
@Deprecated(since = "6.0.6")
77+
@Deprecated(since = "6.0.6", forRemoval = true)
7678
public static Optional<ServerWebExchange> get(Context context) {
77-
return getExchange(context);
79+
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
7880
}
7981

8082
}

spring-web/src/test/java/org/springframework/web/filter/reactive/ServerWebExchangeContextFilterTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import org.junit.jupiter.api.Test;
2323
import reactor.core.publisher.Mono;
2424

25-
import org.springframework.http.server.reactive.HttpHandler;
2625
import org.springframework.web.server.ServerWebExchange;
2726
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
2827
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
@@ -36,16 +35,16 @@
3635
*/
3736
class ServerWebExchangeContextFilterTests {
3837

38+
3939
@Test
4040
void extractServerWebExchangeFromContext() {
4141
MyService service = new MyService();
4242

43-
HttpHandler httpHandler = WebHttpHandlerBuilder
43+
WebHttpHandlerBuilder
4444
.webHandler(exchange -> service.service().then())
4545
.filter(new ServerWebExchangeContextFilter())
46-
.build();
47-
48-
httpHandler.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
46+
.build()
47+
.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse())
4948
.block(Duration.ofSeconds(5));
5049

5150
assertThat(service.getExchange()).isNotNull();
@@ -56,16 +55,16 @@ private static class MyService {
5655

5756
private final AtomicReference<ServerWebExchange> exchangeRef = new AtomicReference<>();
5857

59-
6058
public ServerWebExchange getExchange() {
6159
return this.exchangeRef.get();
6260
}
6361

6462
public Mono<String> service() {
65-
return Mono.just("result").contextWrite(context -> {
66-
ServerWebExchangeContextFilter.getExchange(context).ifPresent(exchangeRef::set);
67-
return context;
68-
});
63+
return Mono.just("result")
64+
.transformDeferredContextual((mono, contextView) -> {
65+
ServerWebExchangeContextFilter.getExchange(contextView).ifPresent(exchangeRef::set);
66+
return mono;
67+
});
6968
}
7069
}
7170

0 commit comments

Comments
 (0)