Skip to content

Commit

Permalink
#983 - Switch to Spring's ServerWebExchangeContextFilter.
Browse files Browse the repository at this point in the history
Replace HypermediaWebFilter with ServerWebExchangeContextFilter.

Related issues: #986, spring-projects/spring-framework#21746.
  • Loading branch information
gregturn authored and odrotbohm committed Jun 27, 2019
1 parent 440e185 commit 6a7e343
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
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;

Expand Down Expand Up @@ -67,14 +67,10 @@ HypermediaWebFluxConfigurer hypermediaWebFluxConfigurer(ObjectProvider<ObjectMap
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();
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -241,7 +241,7 @@ private static UriComponentsBuilder getBuilder(@Nullable ServerWebExchange excha
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.springframework.web.reactive.config.EnableWebFlux;

/**
* Verify WebFlux properly activates the {@link org.springframework.web.filter.reactive.ServerWebExchangeContextFilter}.
*
* @author Greg Turnquist
*/
class HypermediaWebFilterTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

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;
Expand Down Expand Up @@ -82,7 +82,7 @@ void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() {
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);
Expand Down Expand Up @@ -126,7 +126,7 @@ void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() {
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);
Expand Down Expand Up @@ -169,7 +169,7 @@ void deepLinkFromShallowContextProvidedServerExchangeShouldWork() {
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);
Expand All @@ -191,7 +191,7 @@ void linkToRouteWithNoMappingShouldWork() {
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);
Expand Down

0 comments on commit 6a7e343

Please sign in to comment.