From ecf5a9f34efc6474a9cbd9e697dc10edf834ea7f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 9 Sep 2016 12:19:51 +0200 Subject: [PATCH] Added Response.build(Publisher) --- .../function/DefaultResponseBuilder.java | 6 +++ .../web/reactive/function/Response.java | 17 +++++-- .../function/VoidPublisherResponse.java | 50 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 spring-web-reactive/src/main/java/org/springframework/web/reactive/function/VoidPublisherResponse.java 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 bd1c2ce251..7da5a0f799 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 @@ -130,6 +130,12 @@ class DefaultResponseBuilder implements Response.BodyBuilder { return new EmptyResponse(this.statusCode, this.headers); } + @Override + public > Response build(T voidPublisher) { + Assert.notNull(voidPublisher, "'voidPublisher' must not be null"); + return new VoidPublisherResponse<>(this.statusCode, this.headers, voidPublisher); + } + @Override public Response body(T body) { Assert.notNull(body, "'body' must not be null"); diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/Response.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/Response.java index f1bebbc14f..18fba581fb 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/Response.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/Response.java @@ -171,7 +171,7 @@ public interface Response { Mono writeTo(ServerWebExchange exchange); /** - * Defines a builder that adds headers to the response entity. + * Defines a builder that adds headers to the response. * * @param the builder subclass */ @@ -263,13 +263,22 @@ public interface Response { /** * Build the response entity with no body. * - * @return the response entity + * @return the built response */ Response build(); + + /** + * Build the response entity with no body. + * The response will be committed when the given {@code voidPublisher} completes. + * + * @param voidPublisher publisher publisher to indicate when the response should be committed + * @return the built response + */ + > Response build(T voidPublisher); } /** - * Defines a builder that adds a body to the response entity. + * Defines a builder that adds a body to the response. */ interface BodyBuilder extends HeadersBuilder { @@ -296,7 +305,7 @@ public interface Response { /** * Set the body of the response to the given object and return it. * - * @param body the body of the response entity + * @param body the body of the response * @return the built response */ Response body(T body); diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/VoidPublisherResponse.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/VoidPublisherResponse.java new file mode 100644 index 0000000000..ec60199358 --- /dev/null +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/function/VoidPublisherResponse.java @@ -0,0 +1,50 @@ +/* + * 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 org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.http.HttpHeaders; +import org.springframework.web.server.ServerWebExchange; + +/** + * @author Arjen Poutsma + */ +class VoidPublisherResponse> extends AbstractResponse { + + private final T voidPublisher; + + public VoidPublisherResponse(int statusCode, HttpHeaders headers, + T voidPublisher) { + super(statusCode, headers); + this.voidPublisher = voidPublisher; + } + + @Override + public T body() { + return this.voidPublisher; + } + + @Override + public Mono writeTo(ServerWebExchange exchange) { + writeStatusAndHeaders(exchange); + return Flux.from(this.voidPublisher) + .then(exchange.getResponse().setComplete()); + } +}