Browse Source

Remove workaround for Reactor Netty #171

pull/1783/head
Rossen Stoyanchev 6 years ago
parent
commit
7746878b50
  1. 19
      spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java
  2. 32
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java

19
spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java

@ -33,6 +33,7 @@ import org.springframework.core.io.buffer.DataBufferUtils; @@ -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 { @@ -45,16 +46,14 @@ public class HttpHeadResponseDecorator extends ServerHttpResponseDecorator {
*/
@Override
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> 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();
}
/**

32
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorHttpHandlerAdapter.java

@ -52,31 +52,29 @@ public class ReactorHttpHandlerAdapter implements BiFunction<HttpServerRequest, @@ -52,31 +52,29 @@ public class ReactorHttpHandlerAdapter implements BiFunction<HttpServerRequest,
@Override
public Mono<Void> apply(HttpServerRequest request, HttpServerResponse response) {
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(response.alloc());
ServerHttpRequest adaptedRequest;
ServerHttpResponse adaptedResponse;
public Mono<Void> 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"));
}
}

Loading…
Cancel
Save