From 7746878b5044790a09d926522318d1c1cced397c Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 6 Jul 2018 15:16:00 -0400 Subject: [PATCH] Remove workaround for Reactor Netty #171 --- .../reactive/HttpHeadResponseDecorator.java | 19 ++++++----- .../reactive/ReactorHttpHandlerAdapter.java | 32 +++++++++---------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java index 246cd60be7..00dbe21ce1 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java @@ -33,6 +33,7 @@ import org.springframework.core.io.buffer.DataBufferUtils; */ public class HttpHeadResponseDecorator extends ServerHttpResponseDecorator { + public HttpHeadResponseDecorator(ServerHttpResponse delegate) { super(delegate); } @@ -45,16 +46,14 @@ public class HttpHeadResponseDecorator extends ServerHttpResponseDecorator { */ @Override public final Mono writeWith(Publisher body) { - // After Reactor Netty #171 is fixed we can return without delegating - return getDelegate().writeWith( - Flux.from(body) - .reduce(0, (current, buffer) -> { - int next = current + buffer.readableByteCount(); - DataBufferUtils.release(buffer); - return next; - }) - .doOnNext(count -> getHeaders().setContentLength(count)) - .then(Mono.empty())); + return Flux.from(body) + .reduce(0, (current, buffer) -> { + int next = current + buffer.readableByteCount(); + DataBufferUtils.release(buffer); + return next; + }) + .doOnNext(count -> getHeaders().setContentLength(count)) + .then(); } /** diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java index f0e8539745..75445bb736 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java @@ -52,31 +52,29 @@ public class ReactorHttpHandlerAdapter implements BiFunction apply(HttpServerRequest request, HttpServerResponse response) { - NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(response.alloc()); - ServerHttpRequest adaptedRequest; - ServerHttpResponse adaptedResponse; + public Mono apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) { + NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc()); try { - adaptedRequest = new ReactorServerHttpRequest(request, bufferFactory); - adaptedResponse = new ReactorServerHttpResponse(response, bufferFactory); + ServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory); + ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory); + + if (request.getMethod() == HttpMethod.HEAD) { + response = new HttpHeadResponseDecorator(response); + } + + String logPrefix = ((ReactorServerHttpRequest) request).getLogPrefix(); + + return this.httpHandler.handle(request, response) + .doOnError(ex -> logger.trace(logPrefix + "Failed to complete: " + ex.getMessage())) + .doOnSuccess(aVoid -> logger.trace(logPrefix + "Handling completed")); } catch (URISyntaxException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to get request URI: " + ex.getMessage()); } - response.status(HttpResponseStatus.BAD_REQUEST); + reactorResponse.status(HttpResponseStatus.BAD_REQUEST); return Mono.empty(); } - - String logPrefix = ((ReactorServerHttpRequest) adaptedRequest).getLogPrefix(); - - if (adaptedRequest.getMethod() == HttpMethod.HEAD) { - adaptedResponse = new HttpHeadResponseDecorator(adaptedResponse); - } - - return this.httpHandler.handle(adaptedRequest, adaptedResponse) - .doOnError(ex -> logger.trace(logPrefix + "Failed to complete: " + ex.getMessage())) - .doOnSuccess(aVoid -> logger.trace(logPrefix + "Handling completed")); } }