HttpResponse.sendHandler

This commit is contained in:
redkale
2024-08-19 16:20:25 +08:00
parent 4adf23e57c
commit b48182f7cc
2 changed files with 40 additions and 1 deletions

View File

@@ -162,6 +162,8 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
private BiFunction<HttpRequest, RetResult, RetResult> retResultHandler;
private BiFunction<HttpResponse, ByteArray, ByteArray> sendHandler;
// ------------------------------------------------
private final String plainContentType;
@@ -255,6 +257,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
this.headerArray.clear();
this.cacheHandler = null;
this.retResultHandler = null;
this.sendHandler = null;
this.respHeadContainsConnection = false;
this.jsonWriter.recycle();
return super.recycle();
@@ -988,7 +991,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
protected <A> void finish(
boolean kill,
final String contentType,
@Nonnull final byte[] bodyContent,
@Nonnull byte[] bodyContent,
int bodyOffset,
int bodyLength,
Consumer<A> callback,
@@ -996,6 +999,14 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
if (isClosed()) {
return; // 避免重复关闭
}
if (sendHandler != null) {
ByteArray bodyArray = new ByteArray(bodyContent, bodyOffset, bodyLength);
bodyArray = sendHandler.apply(this, bodyArray);
bodyContent = bodyArray.content();
bodyOffset = bodyArray.offset();
bodyLength = bodyArray.length();
}
if (this.headWritedSize < 0) {
if (contentType != null) {
this.contentType = contentType;
@@ -1592,6 +1603,24 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
this.retResultHandler = retResultHandler;
}
/**
* 获取输出RetResult时的拦截器
*
* @return 拦截器
*/
protected BiFunction<HttpResponse, ByteArray, ByteArray> getSendHandler() {
return sendHandler;
}
/**
* 设置输出结果时的拦截器
*
* @param sendHandler 拦截器
*/
public void sendHandler(BiFunction<HttpResponse, ByteArray, ByteArray> sendHandler) {
this.sendHandler = sendHandler;
}
// protected final class TransferFileHandler implements CompletionHandler<Integer, Void> {
//
// private final File file;

View File

@@ -46,6 +46,16 @@ public final class ByteArray implements ByteTuple {
this.count = bs.length;
}
public ByteArray(byte[] bs, int offset, int length) {
if (offset == 0) {
this.content = bs;
this.count = bs.length >= length ? length : bs.length;
} else {
this.content = Arrays.copyOfRange(bs, offset, length);
this.count = bs.length;
}
}
/**
* 清空数据,将count置为0,并不清掉byte[]的内容
*