#983 - Switch to Spring's ServerWebExchangeContextFilter.

Replace HypermediaWebFilter with ServerWebExchangeContextFilter.

Related issues: #986, spring-projects/spring-framework#21746.
This commit is contained in:
Greg Turnquist
2019-04-16 11:20:54 -05:00
committed by Oliver Drotbohm
parent 440e1852cc
commit 6a7e343acc
5 changed files with 13 additions and 59 deletions

View File

@@ -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();
}
/**

View File

@@ -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<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange) //
.subscriberContext(Context.of(SERVER_WEB_EXCHANGE, exchange));
}
}

View File

@@ -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<WebFluxLinkBuilder> 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<WebFluxLinkBuilder> linkToInternal(Object invocation, Mono<UriComponentsBuilder> exchange) {

View File

@@ -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 {

View File

@@ -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);