From b6035ce9aeac24ac076ce430ae9dd920e233ec95 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Sat, 17 Sep 2016 11:45:38 +0200 Subject: [PATCH] Refactor BodyInsertor Refactor BodyInsertor to expose insertion logic directly, rather than exposing a writer function and supplier. --- .../web/reactive/function/BodyInsertor.java | 16 +++++++---- .../web/reactive/function/BodyInsertors.java | 9 ++++--- .../function/DefaultResponseBuilder.java | 4 +-- .../reactive/function/BodyInsertorsTests.java | 27 +++++++------------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertor.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertor.java index b8a4f9936c..43764bd35d 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertor.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertor.java @@ -25,8 +25,9 @@ import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.Assert; /** - * A combination of functions that can insert data into a {@link Response} body. + * A component that can insert data into a {@link Response} body. * + * @param the type of data to insert * @author Arjen Poutsma * @since 5.0 * @see Response#body() @@ -36,14 +37,19 @@ import org.springframework.util.Assert; public interface BodyInsertor { /** - * Return a function that writes to the given response body. + * Insert into the given response. + * @param response the response to insert into + * @param configuration the configuration to use + * @return a {@code Mono} that indicates completion or error */ - BiFunction> writer(); + Mono insert(ServerHttpResponse response, Configuration configuration); /** - * Return a function that supplies the type contained in the body. + * Return the type contained in the body. + * @return the type contained in the body */ - Supplier supplier(); + T t(); + /** * Return a new {@code BodyInsertor} described by the given writer and supplier functions. diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertors.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertors.java index 9b12adf2a3..856f38a37b 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertors.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/BodyInsertors.java @@ -240,14 +240,15 @@ public abstract class BodyInsertors { } @Override - public BiFunction> writer() { - return this.writer; + public Mono insert(ServerHttpResponse response, Configuration configuration) { + return this.writer.apply(response, configuration); } @Override - public Supplier supplier() { - return this.supplier; + public T t() { + return this.supplier.get(); } + } diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/DefaultResponseBuilder.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/DefaultResponseBuilder.java index 7ee70da385..dca54d6e66 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/DefaultResponseBuilder.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/DefaultResponseBuilder.java @@ -246,14 +246,14 @@ class DefaultResponseBuilder implements Response.BodyBuilder { @Override public T body() { - return this.insertor.supplier().get(); + return this.insertor.t(); } @Override public Mono writeTo(ServerWebExchange exchange, Configuration configuration) { ServerHttpResponse response = exchange.getResponse(); writeStatusAndHeaders(response); - return this.insertor.writer().apply(response, configuration); + return this.insertor.insert(response, configuration); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyInsertorsTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyInsertorsTests.java index 63e0d934e4..072f783519 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyInsertorsTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyInsertorsTests.java @@ -18,7 +18,6 @@ package org.springframework.web.reactive.function; import java.nio.ByteBuffer; import java.nio.file.Files; -import java.util.function.BiFunction; import org.junit.Test; import reactor.core.publisher.Flux; @@ -29,7 +28,6 @@ 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.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.tests.TestSubscriber; @@ -47,11 +45,10 @@ public class BodyInsertorsTests { String body = "foo"; BodyInsertor insertor = BodyInsertors.fromObject(body); - assertEquals(body, insertor.supplier().get()); + assertEquals(body, insertor.t()); - BiFunction> writer = insertor.writer(); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = writer.apply(response, Configuration.builder().build()); + Mono result = insertor.insert(response, Configuration.builder().build()); TestSubscriber.subscribe(result) .assertComplete(); @@ -67,11 +64,10 @@ public class BodyInsertorsTests { Flux body = Flux.just("foo"); BodyInsertor> insertor = BodyInsertors.fromPublisher(body, String.class); - assertEquals(body, insertor.supplier().get()); + assertEquals(body, insertor.t()); - BiFunction> writer = insertor.writer(); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = writer.apply(response, Configuration.builder().build()); + Mono result = insertor.insert(response, Configuration.builder().build()); TestSubscriber.subscribe(result) .assertComplete(); @@ -87,11 +83,10 @@ public class BodyInsertorsTests { Resource body = new ClassPathResource("response.txt", getClass()); BodyInsertor insertor = BodyInsertors.fromResource(body); - assertEquals(body, insertor.supplier().get()); + assertEquals(body, insertor.t()); - BiFunction> writer = insertor.writer(); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = writer.apply(response, Configuration.builder().build()); + Mono result = insertor.insert(response, Configuration.builder().build()); TestSubscriber.subscribe(result) .assertComplete(); @@ -113,11 +108,10 @@ public class BodyInsertorsTests { BodyInsertor>> insertor = BodyInsertors.fromServerSentEvents(body); - assertEquals(body, insertor.supplier().get()); + assertEquals(body, insertor.t()); - BiFunction> writer = insertor.writer(); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = writer.apply(response, Configuration.builder().build()); + Mono result = insertor.insert(response, Configuration.builder().build()); TestSubscriber.subscribe(result) .assertComplete(); @@ -129,11 +123,10 @@ public class BodyInsertorsTests { BodyInsertor> insertor = BodyInsertors.fromServerSentEvents(body, String.class); - assertEquals(body, insertor.supplier().get()); + assertEquals(body, insertor.t()); - BiFunction> writer = insertor.writer(); MockServerHttpResponse response = new MockServerHttpResponse(); - Mono result = writer.apply(response, Configuration.builder().build()); + Mono result = insertor.insert(response, Configuration.builder().build()); TestSubscriber.subscribe(result) .assertComplete();