diff --git a/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpResponseDecorator.java b/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpResponseDecorator.java new file mode 100644 index 0000000..7f85718 --- /dev/null +++ b/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpResponseDecorator.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2020 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package we.spring.http.server.reactive.ext; + +import org.reactivestreams.Publisher; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.PooledDataBuffer; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.http.server.reactive.ServerHttpResponseDecorator; +import reactor.core.publisher.Mono; +import we.util.NettyDataBufferUtils; + +/** + * @author hongqiaowei + */ + +public abstract class FizzServerHttpResponseDecorator extends ServerHttpResponseDecorator { + + public FizzServerHttpResponseDecorator(ServerHttpResponse delegate) { + super(delegate); + } + + @Override + public Mono writeWith(Publisher bodyPublisher) { + + return + NettyDataBufferUtils.join(bodyPublisher).defaultIfEmpty(NettyDataBufferUtils.EMPTY_DATA_BUFFER) + .flatMap( + body -> { + DataBuffer b = null; + if (body != NettyDataBufferUtils.EMPTY_DATA_BUFFER) { + if (body instanceof PooledDataBuffer) { + try { + b = NettyDataBufferUtils.from(body.asByteBuffer()); + } finally { + NettyDataBufferUtils.release(body); + } + } else { + b = body; + } + } + Publisher r = writeWith(b); + return super.writeWith(r); + } + ); + } + + /** + * You can getDelegate().getHeaders().set("h", "v") in the method and others for response. + * @param remoteResponseBody + * @return the real http response body to client, or Mono.empty() if response without body + */ + public abstract Publisher writeWith(DataBuffer remoteResponseBody); +}