Added Response.build(Publisher<Void>)

This commit is contained in:
Arjen Poutsma
2016-09-09 12:19:51 +02:00
parent f4ae831f7f
commit ecf5a9f34e
3 changed files with 69 additions and 4 deletions

View File

@@ -130,6 +130,12 @@ class DefaultResponseBuilder implements Response.BodyBuilder {
return new EmptyResponse(this.statusCode, this.headers);
}
@Override
public <T extends Publisher<Void>> Response<T> build(T voidPublisher) {
Assert.notNull(voidPublisher, "'voidPublisher' must not be null");
return new VoidPublisherResponse<>(this.statusCode, this.headers, voidPublisher);
}
@Override
public <T> Response<T> body(T body) {
Assert.notNull(body, "'body' must not be null");

View File

@@ -171,7 +171,7 @@ public interface Response<T> {
Mono<Void> writeTo(ServerWebExchange exchange);
/**
* Defines a builder that adds headers to the response entity.
* Defines a builder that adds headers to the response.
*
* @param <B> the builder subclass
*/
@@ -263,13 +263,22 @@ public interface Response<T> {
/**
* Build the response entity with no body.
*
* @return the response entity
* @return the built response
*/
Response<Void> 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
*/
<T extends Publisher<Void>> Response<T> 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<BodyBuilder> {
@@ -296,7 +305,7 @@ public interface Response<T> {
/**
* 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
*/
<T> Response<T> body(T body);

View File

@@ -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<T extends Publisher<Void>> extends AbstractResponse<T> {
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<Void> writeTo(ServerWebExchange exchange) {
writeStatusAndHeaders(exchange);
return Flux.from(this.voidPublisher)
.then(exchange.getResponse().setComplete());
}
}