提交 778ef026 编写于 作者: A Arjen Poutsma

Rename BodyInsertor to BodyInserter

上级 b6035ce9
......@@ -31,10 +31,10 @@ import org.springframework.util.Assert;
* @author Arjen Poutsma
* @since 5.0
* @see Response#body()
* @see Response.BodyBuilder#body(BodyInsertor)
* @see BodyInsertors
* @see Response.BodyBuilder#body(BodyInserter)
* @see BodyInserters
*/
public interface BodyInsertor<T> {
public interface BodyInserter<T> {
/**
* Insert into the given response.
......@@ -52,19 +52,19 @@ public interface BodyInsertor<T> {
/**
* Return a new {@code BodyInsertor} described by the given writer and supplier functions.
* @param writer the writer function for the new insertor
* @param supplier the supplier function for the new insertor
* @param <T> the type supplied and written by the insertor
* @return the new {@code BodyInsertor}
* Return a new {@code BodyInserter} described by the given writer and supplier functions.
* @param writer the writer function for the new inserter
* @param supplier the supplier function for the new inserter
* @param <T> the type supplied and written by the inserter
* @return the new {@code BodyInserter}
*/
static <T> BodyInsertor<T> of(BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer,
static <T> BodyInserter<T> of(BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer,
Supplier<T> supplier) {
Assert.notNull(writer, "'writer' must not be null");
Assert.notNull(supplier, "'supplier' must not be null");
return new BodyInsertors.DefaultBodyInsertor<T>(writer, supplier);
return new BodyInserters.DefaultBodyInserter<T>(writer, supplier);
}
}
......@@ -37,13 +37,13 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Implementations of {@link BodyInsertor} that write various bodies, such a reactive streams,
* Implementations of {@link BodyInserter} that write various bodies, such a reactive streams,
* server-sent events, resources, etc.
*
* @author Arjen Poutsma
* @since 5.0
*/
public abstract class BodyInsertors {
public abstract class BodyInserters {
private static final ResolvableType RESOURCE_TYPE = ResolvableType.forClass(Resource.class);
......@@ -52,32 +52,32 @@ public abstract class BodyInsertors {
private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
BodyInsertors.class.getClassLoader()) &&
BodyInserters.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator",
BodyInsertors.class.getClassLoader());
BodyInserters.class.getClassLoader());
/**
* Return a {@code BodyInsertor} that writes the given single object.
* Return a {@code BodyInserter} that writes the given single object.
* @param body the body of the response
* @return a {@code BodyInsertor} that writes a single object
* @return a {@code BodyInserter} that writes a single object
*/
public static <T> BodyInsertor<T> fromObject(T body) {
public static <T> BodyInserter<T> fromObject(T body) {
Assert.notNull(body, "'body' must not be null");
return BodyInsertor.of(
return BodyInserter.of(
(response, configuration) -> writeWithMessageWriters(response, configuration,
Mono.just(body), ResolvableType.forInstance(body)),
() -> body);
}
/**
* Return a {@code BodyInsertor} that writes the given {@link Publisher}.
* Return a {@code BodyInserter} that writes the given {@link Publisher}.
* @param publisher the publisher to stream to the response body
* @param elementClass the class of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <S> the type of the {@code Publisher}.
* @return a {@code BodyInsertor} that writes a {@code Publisher}
* @return a {@code BodyInserter} that writes a {@code Publisher}
*/
public static <S extends Publisher<T>, T> BodyInsertor<S> fromPublisher(S publisher,
public static <S extends Publisher<T>, T> BodyInserter<S> fromPublisher(S publisher,
Class<T> elementClass) {
Assert.notNull(publisher, "'publisher' must not be null");
......@@ -86,19 +86,19 @@ public abstract class BodyInsertors {
}
/**
* Return a {@code BodyInsertor} that writes the given {@link Publisher}.
* Return a {@code BodyInserter} that writes the given {@link Publisher}.
* @param publisher the publisher to stream to the response body
* @param elementType the type of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <S> the type of the {@code Publisher}.
* @return a {@code BodyInsertor} that writes a {@code Publisher}
* @return a {@code BodyInserter} that writes a {@code Publisher}
*/
public static <S extends Publisher<T>, T> BodyInsertor<S> fromPublisher(S publisher,
public static <S extends Publisher<T>, T> BodyInserter<S> fromPublisher(S publisher,
ResolvableType elementType) {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(elementType, "'elementType' must not be null");
return BodyInsertor.of(
return BodyInserter.of(
(response, configuration) -> writeWithMessageWriters(response, configuration,
publisher, elementType),
() -> publisher
......@@ -106,17 +106,17 @@ public abstract class BodyInsertors {
}
/**
* Return a {@code BodyInsertor} that writes the given {@code Resource}.
* Return a {@code BodyInserter} that writes the given {@code Resource}.
* If the resource can be resolved to a {@linkplain Resource#getFile() file}, it will be copied
* using
* <a href="https://en.wikipedia.org/wiki/Zero-copy">zero-copy</a>
* @param resource the resource to write to the response
* @param <T> the type of the {@code Resource}
* @return a {@code BodyInsertor} that writes a {@code Publisher}
* @return a {@code BodyInserter} that writes a {@code Publisher}
*/
public static <T extends Resource> BodyInsertor<T> fromResource(T resource) {
public static <T extends Resource> BodyInserter<T> fromResource(T resource) {
Assert.notNull(resource, "'resource' must not be null");
return BodyInsertor.of(
return BodyInserter.of(
(response, configuration) -> {
ResourceHttpMessageWriter messageWriter = new ResourceHttpMessageWriter();
MediaType contentType = response.getHeaders().getContentType();
......@@ -128,17 +128,17 @@ public abstract class BodyInsertors {
}
/**
* Return a {@code BodyInsertor} that writes the given {@code ServerSentEvent} publisher.
* Return a {@code BodyInserter} that writes the given {@code ServerSentEvent} publisher.
* @param eventsPublisher the {@code ServerSentEvent} publisher to write to the response body
* @param <T> the type of the elements contained in the {@link ServerSentEvent}
* @return a {@code BodyInsertor} that writes a {@code ServerSentEvent} publisher
* @return a {@code BodyInserter} that writes a {@code ServerSentEvent} publisher
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
*/
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInsertor<S> fromServerSentEvents(
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S> fromServerSentEvents(
S eventsPublisher) {
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
return BodyInsertor.of(
return BodyInserter.of(
(response, configuration) -> {
ServerSentEventHttpMessageWriter messageWriter = sseMessageWriter();
MediaType contentType = response.getHeaders().getContentType();
......@@ -150,16 +150,16 @@ public abstract class BodyInsertors {
}
/**
* Return a {@code BodyInsertor} that writes the given {@code Publisher} publisher as
* Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
* Server-Sent Events.
* @param eventsPublisher the publisher to write to the response body as Server-Sent Events
* @param eventClass the class of event contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @return a {@code BodyInsertor} that writes the given {@code Publisher} publisher as
* @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
* Server-Sent Events
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
*/
public static <T, S extends Publisher<T>> BodyInsertor<S> fromServerSentEvents(S eventsPublisher,
public static <T, S extends Publisher<T>> BodyInserter<S> fromServerSentEvents(S eventsPublisher,
Class<T> eventClass) {
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
......@@ -168,21 +168,21 @@ public abstract class BodyInsertors {
}
/**
* Return a {@code BodyInsertor} that writes the given {@code Publisher} publisher as
* Return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
* Server-Sent Events.
* @param eventsPublisher the publisher to write to the response body as Server-Sent Events
* @param eventType the type of event contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @return a {@code BodyInsertor} that writes the given {@code Publisher} publisher as
* @return a {@code BodyInserter} that writes the given {@code Publisher} publisher as
* Server-Sent Events
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
*/
public static <T, S extends Publisher<T>> BodyInsertor<S> fromServerSentEvents(S eventsPublisher,
public static <T, S extends Publisher<T>> BodyInserter<S> fromServerSentEvents(S eventsPublisher,
ResolvableType eventType) {
Assert.notNull(eventsPublisher, "'eventsPublisher' must not be null");
Assert.notNull(eventType, "'eventType' must not be null");
return BodyInsertor.of(
return BodyInserter.of(
(response, configuration) -> {
ServerSentEventHttpMessageWriter messageWriter = sseMessageWriter();
MediaType contentType = response.getHeaders().getContentType();
......@@ -211,7 +211,7 @@ public abstract class BodyInsertors {
.filter(messageWriter -> messageWriter.canWrite(bodyType, contentType, Collections
.emptyMap()))
.findFirst()
.map(BodyInsertors::cast)
.map(BodyInserters::cast)
.map(messageWriter -> messageWriter
.write(body, bodyType, contentType, response, Collections
.emptyMap()))
......@@ -226,13 +226,13 @@ public abstract class BodyInsertors {
return (HttpMessageWriter<T>) messageWriter;
}
static class DefaultBodyInsertor<T> implements BodyInsertor<T> {
static class DefaultBodyInserter<T> implements BodyInserter<T> {
private final BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer;
private final Supplier<T> supplier;
public DefaultBodyInsertor(
public DefaultBodyInserter(
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer,
Supplier<T> supplier) {
this.writer = writer;
......
......@@ -141,7 +141,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
@Override
public Response<Void> build() {
return body(BodyInsertor.of(
return body(BodyInserter.of(
(response, configuration) -> response.setComplete(),
() -> null));
}
......@@ -149,7 +149,7 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
@Override
public <T extends Publisher<Void>> Response<T> build(T voidPublisher) {
Assert.notNull(voidPublisher, "'voidPublisher' must not be null");
return body(BodyInsertor.of(
return body(BodyInserter.of(
(response, configuration) -> Flux.from(voidPublisher).then(response.setComplete()),
() -> null));
}
......@@ -157,13 +157,13 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
@Override
public <T> Response<T> body(BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer,
Supplier<T> supplier) {
return body(BodyInsertor.of(writer, supplier));
return body(BodyInserter.of(writer, supplier));
}
@Override
public <T> Response<T> body(BodyInsertor<T> insertor) {
Assert.notNull(insertor, "'insertor' must not be null");
return new BodyInsertorResponse<T>(this.statusCode, this.headers, insertor);
public <T> Response<T> body(BodyInserter<T> inserter) {
Assert.notNull(inserter, "'inserter' must not be null");
return new BodyInserterResponse<T>(this.statusCode, this.headers, inserter);
}
@Override
......@@ -233,27 +233,27 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
}
}
private static final class BodyInsertorResponse<T> extends AbstractResponse<T> {
private static final class BodyInserterResponse<T> extends AbstractResponse<T> {
private final BodyInsertor<T> insertor;
private final BodyInserter<T> inserter;
public BodyInsertorResponse(
int statusCode, HttpHeaders headers, BodyInsertor<T> insertor) {
public BodyInserterResponse(
int statusCode, HttpHeaders headers, BodyInserter<T> inserter) {
super(statusCode, headers);
this.insertor = insertor;
this.inserter = inserter;
}
@Override
public T body() {
return this.insertor.t();
return this.inserter.t();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, Configuration configuration) {
ServerHttpResponse response = exchange.getResponse();
writeStatusAndHeaders(response);
return this.insertor.insert(response, configuration);
return this.inserter.insert(response, configuration);
}
}
......
......@@ -308,7 +308,8 @@ public interface Response<T> {
BodyBuilder contentType(MediaType contentType);
/**
* Write the body to the given {@code BodyInsertor} and return it.
* Set the body with the given {@code supplier} function, and write it with the given
* {@code writer} function.
* @param writer a function that writes the body to the {@code ServerHttpResponse}
* @param supplier a function that returns the body instance
* @param <T> the type contained in the body
......@@ -318,12 +319,12 @@ public interface Response<T> {
Supplier<T> supplier);
/**
* Set the body of the response to the given {@code BodyInsertor} and return it.
* @param insertor the {@code BodyInsertor} that writes to the response
* Set the body of the response to the given {@code BodyInserter} and return it.
* @param inserter the {@code BodyInserter} that writes to the response
* @param <T> the type contained in the body
* @return the built response
*/
<T> Response<T> body(BodyInsertor<T> insertor);
<T> Response<T> body(BodyInserter<T> inserter);
/**
* Render the template with the given {@code name} using the given {@code modelAttributes}.
......
......@@ -38,17 +38,17 @@ import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
*/
public class BodyInsertorsTests {
public class BodyInsertersTests {
@Test
public void ofObject() throws Exception {
String body = "foo";
BodyInsertor<String> insertor = BodyInsertors.fromObject(body);
BodyInserter<String> inserter = BodyInserters.fromObject(body);
assertEquals(body, insertor.t());
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = insertor.insert(response, Configuration.builder().build());
Mono<Void> result = inserter.insert(response, Configuration.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
......@@ -62,12 +62,12 @@ public class BodyInsertorsTests {
@Test
public void ofPublisher() throws Exception {
Flux<String> body = Flux.just("foo");
BodyInsertor<Flux<String>> insertor = BodyInsertors.fromPublisher(body, String.class);
BodyInserter<Flux<String>> inserter = BodyInserters.fromPublisher(body, String.class);
assertEquals(body, insertor.t());
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = insertor.insert(response, Configuration.builder().build());
Mono<Void> result = inserter.insert(response, Configuration.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
......@@ -81,12 +81,12 @@ public class BodyInsertorsTests {
@Test
public void ofResource() throws Exception {
Resource body = new ClassPathResource("response.txt", getClass());
BodyInsertor<Resource> insertor = BodyInsertors.fromResource(body);
BodyInserter<Resource> inserter = BodyInserters.fromResource(body);
assertEquals(body, insertor.t());
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = insertor.insert(response, Configuration.builder().build());
Mono<Void> result = inserter.insert(response, Configuration.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
......@@ -105,13 +105,13 @@ public class BodyInsertorsTests {
public void ofServerSentEventFlux() throws Exception {
ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
Flux<ServerSentEvent<String>> body = Flux.just(event);
BodyInsertor<Flux<ServerSentEvent<String>>> insertor =
BodyInsertors.fromServerSentEvents(body);
BodyInserter<Flux<ServerSentEvent<String>>> inserter =
BodyInserters.fromServerSentEvents(body);
assertEquals(body, insertor.t());
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = insertor.insert(response, Configuration.builder().build());
Mono<Void> result = inserter.insert(response, Configuration.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
......@@ -120,13 +120,13 @@ public class BodyInsertorsTests {
@Test
public void ofServerSentEventClass() throws Exception {
Flux<String> body = Flux.just("foo");
BodyInsertor<Flux<String>> insertor =
BodyInsertors.fromServerSentEvents(body, String.class);
BodyInserter<Flux<String>> inserter =
BodyInserters.fromServerSentEvents(body, String.class);
assertEquals(body, insertor.t());
assertEquals(body, inserter.t());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = insertor.insert(response, Configuration.builder().build());
Mono<Void> result = inserter.insert(response, Configuration.builder().build());
TestSubscriber.subscribe(result)
.assertComplete();
......
......@@ -214,7 +214,7 @@ public class DefaultResponseBuilderTests {
}
@Test
public void bodyInsertor() throws Exception {
public void bodyInserter() throws Exception {
String body = "foo";
Supplier<String> supplier = () -> body;
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer =
......
......@@ -155,14 +155,14 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
public Response<Publisher<Person>> mono(Request request) {
Person person = new Person("John");
return Response.ok().body(BodyInsertors.fromPublisher(Mono.just(person), Person.class));
return Response.ok().body(BodyInserters.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(
BodyInsertors.fromPublisher(Flux.just(person1, person2), Person.class));
BodyInserters.fromPublisher(Flux.just(person1, person2), Person.class));
}
}
......
......@@ -34,7 +34,7 @@ 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.BodyInsertors.fromPublisher;
import static org.springframework.web.reactive.function.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;
......
......@@ -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.BodyInsertors.fromObject;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
/**
* @author Arjen Poutsma
......
......@@ -111,13 +111,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(BodyInsertors.fromServerSentEvents(flux, String.class));
return Response.ok().body(BodyInserters.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(BodyInsertors.fromServerSentEvents(flux, Person.class));
return Response.ok().body(BodyInserters.fromServerSentEvents(flux, Person.class));
}
public Response<Publisher<ServerSentEvent<String>>> sse(Request request) {
......@@ -127,7 +127,7 @@ public class SseHandlerFunctionIntegrationTests
.comment("bar")
.build()).take(2);
return Response.ok().body(BodyInsertors.fromServerSentEvents(flux));
return Response.ok().body(BodyInserters.fromServerSentEvents(flux));
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册