diff --git a/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java index 0aa5871c..9597b2b3 100644 --- a/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java @@ -27,13 +27,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; -import org.springframework.hateoas.server.reactive.HypermediaWebFilter; import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.lang.NonNull; import org.springframework.util.MimeType; +import org.springframework.web.filter.reactive.ServerWebExchangeContextFilter; import org.springframework.web.reactive.config.WebFluxConfigurer; import org.springframework.web.reactive.function.client.WebClient; @@ -67,14 +67,10 @@ class WebFluxHateoasConfiguration { return new HypermediaWebFluxConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes); } - /** - * TODO: Replace with Spring Framework filter when https://github.com/spring-projects/spring-framework/issues/21746 is - * completed. - */ @Bean - @Lazy // To avoid creation on a WebMVC app using WebClient only - HypermediaWebFilter hypermediaWebFilter() { - return new HypermediaWebFilter(); + @Lazy + ServerWebExchangeContextFilter serverWebExchangeContextFilter() { + return new ServerWebExchangeContextFilter(); } /** diff --git a/src/main/java/org/springframework/hateoas/server/reactive/HypermediaWebFilter.java b/src/main/java/org/springframework/hateoas/server/reactive/HypermediaWebFilter.java deleted file mode 100644 index 1b643712..00000000 --- a/src/main/java/org/springframework/hateoas/server/reactive/HypermediaWebFilter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.hateoas.server.reactive; - -import reactor.core.publisher.Mono; -import reactor.util.context.Context; - -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebFilter; -import org.springframework.web.server.WebFilterChain; - -/** - * {@link WebFilter} that ensures a copy of the {@link ServerWebExchange} is added to the Reactor {@link Context}. - * - * @author Greg Turnquist - * @since 1.0 - */ -public class HypermediaWebFilter implements WebFilter { - - public static final String SERVER_WEB_EXCHANGE = "serverWebExchange"; - - /* - * (non-Javadoc) - * @see org.springframework.web.server.WebFilter#filter(org.springframework.web.server.ServerWebExchange, org.springframework.web.server.WebFilterChain) - */ - @Override - public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { - return chain.filter(exchange) // - .subscriberContext(Context.of(SERVER_WEB_EXCHANGE, exchange)); - } -} diff --git a/src/main/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilder.java b/src/main/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilder.java index fbb032a2..de40e516 100644 --- a/src/main/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilder.java @@ -15,7 +15,7 @@ */ package org.springframework.hateoas.server.reactive; -import static org.springframework.hateoas.server.reactive.HypermediaWebFilter.*; +import static org.springframework.web.filter.reactive.ServerWebExchangeContextFilter.*; import lombok.RequiredArgsConstructor; @@ -241,7 +241,7 @@ public class WebFluxLinkBuilder extends TemplateVariableAwareLinkBuilderSupport< private static Mono linkToInternal(Object invocation) { return linkToInternal(invocation, - Mono.subscriberContext().map(context -> getBuilder(context.getOrDefault(SERVER_WEB_EXCHANGE, null)))); + Mono.subscriberContext().map(context -> getBuilder(context.getOrDefault(EXCHANGE_CONTEXT_ATTRIBUTE, null)))); } private static Mono linkToInternal(Object invocation, Mono exchange) { diff --git a/src/test/java/org/springframework/hateoas/server/reactive/HypermediaWebFilterTest.java b/src/test/java/org/springframework/hateoas/server/reactive/HypermediaWebFilterTest.java index f1c5df50..d02de0e8 100644 --- a/src/test/java/org/springframework/hateoas/server/reactive/HypermediaWebFilterTest.java +++ b/src/test/java/org/springframework/hateoas/server/reactive/HypermediaWebFilterTest.java @@ -40,6 +40,8 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.config.EnableWebFlux; /** + * Verify WebFlux properly activates the {@link org.springframework.web.filter.reactive.ServerWebExchangeContextFilter}. + * * @author Greg Turnquist */ class HypermediaWebFilterTest { diff --git a/src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java b/src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java index 0fa4afa1..75870310 100644 --- a/src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java +++ b/src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java @@ -17,8 +17,8 @@ package org.springframework.hateoas.server.reactive; import static org.assertj.core.api.Assertions.*; import static org.mockito.Mockito.*; -import static org.springframework.hateoas.server.reactive.HypermediaWebFilter.*; import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.*; +import static org.springframework.web.filter.reactive.ServerWebExchangeContextFilter.*; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -82,7 +82,7 @@ class WebFluxLinkBuilderTest { when(this.request.getHeaders()).thenReturn(new HttpHeaders()); linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() // - .subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) // + .subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) // .as(StepVerifier::create).expectNextMatches(link -> { assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF); @@ -126,7 +126,7 @@ class WebFluxLinkBuilderTest { when(this.request.getHeaders()).thenReturn(new HttpHeaders()); linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() // - .subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) // + .subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) // .as(StepVerifier::create).expectNextMatches(link -> { assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF); @@ -169,7 +169,7 @@ class WebFluxLinkBuilderTest { when(this.request.getHeaders()).thenReturn(new HttpHeaders()); linkTo(methodOn(TestController.class).deep()).withSelfRel().toMono() // - .subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) // + .subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) // .as(StepVerifier::create).expectNextMatches(link -> { assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF); @@ -191,7 +191,7 @@ class WebFluxLinkBuilderTest { when(this.request.getHeaders()).thenReturn(new HttpHeaders()); linkTo(methodOn(TestController2.class).root()).withSelfRel().toMono() // - .subscriberContext(Context.of(SERVER_WEB_EXCHANGE, this.exchange)) // + .subscriberContext(Context.of(EXCHANGE_CONTEXT_ATTRIBUTE, this.exchange)) // .as(StepVerifier::create).expectNextMatches(link -> { assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF);