I'm using the ServerWebExchangeContextFilter filter to store the current ServerWebExchange context inside the context map provided by spring.
But when I try to get this saved ServerWebExchange context from a different part of my code (downstream), I get an empty context from the context map against the key EXCHANGE_CONTEXT_ATTRIBUTE
.
Context ctx = Mono.subscriberContext()
.map(ServerWebExchangeContextFilter::get)
assert ctx.isPresent()==true // This assert always fails
The ServerWebExchangeContextFitler class has the following definition:
public class ServerWebExchangeContextFilter implements WebFilter {
public static final String EXCHANGE_CONTEXT_ATTRIBUTE =
ServerWebExchangeContextFilter.class.getName() + ".EXCHANGE_CONTEXT";
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.subscriberContext(cxt -> cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
}
public static Optional<ServerWebExchange> get(Context context) {
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
}
}
I am writing a common library function for my organisation, and would hence like to avoid passing the ServerHttpRequest inside the argument tuple. Hence please post an answer which would allow me to access the context from a static function.