Moved BodyExtractor and BodyInserter to http.codec

This commit moves the web.reactive.function.[BodyInserter|BodyExtractor]
to http.codec, so that they can be used from the client as well.

Furthermore, it parameterized both inserter and extractor over
ReactiveHttpOutputMessage and ReactiveHttpInputMessage respectively, so
that they can be limited to only be used on the client or server.
This commit is contained in:
Arjen Poutsma
2016-10-05 15:01:23 +02:00
parent ed6c533079
commit dc1926a861
21 changed files with 320 additions and 128 deletions

View File

@@ -1,101 +0,0 @@
/*
* Copyright 2002-2016 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
*
* http://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.web.reactive.function;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.tests.TestSubscriber;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
/**
* @author Arjen Poutsma
*/
public class BodyExtractorsTests {
@Test
public void toMono() throws Exception {
BodyExtractor<Mono<String>> extractor = BodyExtractors.toMono(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = new MockServerHttpRequest();
request.setBody(body);
StrategiesSupplier strategies = StrategiesSupplier.builder().build();
Mono<String> result = extractor.extract(request, strategies);
TestSubscriber.subscribe(result)
.assertComplete()
.assertValues("foo");
}
@Test
public void toFlux() throws Exception {
BodyExtractor<Flux<String>> extractor = BodyExtractors.toFlux(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = new MockServerHttpRequest();
request.setBody(body);
StrategiesSupplier strategies = StrategiesSupplier.builder().build();
Flux<String> result = extractor.extract(request, strategies);
TestSubscriber.subscribe(result)
.assertComplete()
.assertValues("foo");
}
@Test
public void toFluxUnacceptable() throws Exception {
BodyExtractor<Flux<String>> extractor = BodyExtractors.toFlux(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = new MockServerHttpRequest();
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.setBody(body);
StrategiesSupplier strategies = StrategiesSupplier.empty().build();
Flux<String> result = extractor.extract(request, strategies);
TestSubscriber.subscribe(result)
.assertError(UnsupportedMediaTypeStatusException.class);
}
}

View File

@@ -1,135 +0,0 @@
/*
* Copyright 2002-2016 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
*
* http://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.web.reactive.function;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.tests.TestSubscriber;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
*/
public class BodyInsertersTests {
@Test
public void ofObject() throws Exception {
String body = "foo";
BodyInserter<String> inserter = BodyInserters.fromObject(body);
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, StrategiesSupplier.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
ByteBuffer byteBuffer = ByteBuffer.wrap(body.getBytes(UTF_8));
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
TestSubscriber.subscribe(response.getBody())
.assertComplete()
.assertValues(buffer);
}
@Test
public void ofPublisher() throws Exception {
Flux<String> body = Flux.just("foo");
BodyInserter<Flux<String>> inserter = BodyInserters.fromPublisher(body, String.class);
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, StrategiesSupplier.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
ByteBuffer byteBuffer = ByteBuffer.wrap("foo".getBytes(UTF_8));
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
TestSubscriber.subscribe(response.getBody())
.assertComplete()
.assertValues(buffer);
}
@Test
public void ofResource() throws Exception {
Resource body = new ClassPathResource("response.txt", getClass());
BodyInserter<Resource> inserter = BodyInserters.fromResource(body);
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, StrategiesSupplier.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath());
TestSubscriber.subscribe(response.getBody())
.assertComplete()
.assertValuesWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
assertArrayEquals(expectedBytes, resultBytes);
});
}
@Test
public void ofServerSentEventFlux() throws Exception {
ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
Flux<ServerSentEvent<String>> body = Flux.just(event);
BodyInserter<Flux<ServerSentEvent<String>>> inserter =
BodyInserters.fromServerSentEvents(body);
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, StrategiesSupplier.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
}
@Test
public void ofServerSentEventClass() throws Exception {
Flux<String> body = Flux.just("foo");
BodyInserter<Flux<String>> inserter =
BodyInserters.fromServerSentEvents(body, String.class);
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, StrategiesSupplier.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
}
}

View File

@@ -52,7 +52,7 @@ import org.springframework.web.server.ServerWebExchange;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import static org.springframework.http.codec.BodyExtractors.toMono;
/**
* @author Arjen Poutsma

View File

@@ -38,6 +38,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.BodyInserter;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -217,7 +218,7 @@ public class DefaultResponseBuilderTests {
public void bodyInserter() throws Exception {
String body = "foo";
Supplier<String> supplier = () -> body;
BiFunction<ServerHttpResponse, StrategiesSupplier, Mono<Void>> writer =
BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer =
(response, strategies) -> {
byte[] bodyBytes = body.getBytes(UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);

View File

@@ -50,6 +50,7 @@ import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.codec.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.RouterFunctions.route;
/**
@@ -155,14 +156,14 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
public Response<Publisher<Person>> mono(Request request) {
Person person = new Person("John");
return Response.ok().body(BodyInserters.fromPublisher(Mono.just(person), Person.class));
return Response.ok().body(fromPublisher(Mono.just(person), Person.class));
}
public Response<Publisher<Person>> flux(Request request) {
Person person1 = new Person("John");
Person person2 = new Person("Jane");
return Response.ok().body(
BodyInserters.fromPublisher(Flux.just(person1, person2), Person.class));
fromPublisher(Flux.just(person1, person2), Person.class));
}
}

View File

@@ -33,6 +33,8 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.codec.BodyExtractor;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -88,9 +90,9 @@ public class MockRequest<T> implements Request {
return this.headers;
}
@SuppressWarnings("unchecked")
@Override
public <S> S body(BodyExtractor<S> extractor) {
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor){
return (S) this.body;
}

View File

@@ -33,8 +33,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
import static org.springframework.http.codec.BodyExtractors.toMono;
import static org.springframework.http.codec.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.RequestPredicates.GET;
import static org.springframework.web.reactive.function.RequestPredicates.POST;
import static org.springframework.web.reactive.function.RouterFunctions.route;

View File

@@ -23,7 +23,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.http.codec.BodyInserters.fromObject;
/**
* @author Arjen Poutsma

View File

@@ -30,6 +30,7 @@ import org.springframework.http.codec.ServerSentEvent;
import org.springframework.tests.TestSubscriber;
import org.springframework.web.client.reactive.WebClient;
import static org.springframework.http.codec.BodyInserters.fromServerSentEvents;
import static org.springframework.web.client.reactive.ClientWebRequestBuilders.get;
import static org.springframework.web.client.reactive.ResponseExtractors.bodyStream;
import static org.springframework.web.reactive.function.RouterFunctions.route;
@@ -111,13 +112,13 @@ public class SseHandlerFunctionIntegrationTests
public Response<Publisher<String>> string(Request request) {
Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
return Response.ok().body(BodyInserters.fromServerSentEvents(flux, String.class));
return Response.ok().body(fromServerSentEvents(flux, String.class));
}
public Response<Publisher<Person>> person(Request request) {
Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
.map(l -> new Person("foo " + l)).take(2);
return Response.ok().body(BodyInserters.fromServerSentEvents(flux, Person.class));
return Response.ok().body(fromServerSentEvents(flux, Person.class));
}
public Response<Publisher<ServerSentEvent<String>>> sse(Request request) {
@@ -127,7 +128,7 @@ public class SseHandlerFunctionIntegrationTests
.comment("bar")
.build()).take(2);
return Response.ok().body(BodyInserters.fromServerSentEvents(flux));
return Response.ok().body(fromServerSentEvents(flux));
}
}

View File

@@ -1,2 +0,0 @@
Hello World
This is a sample response text file.