diff --git a/src/main/java/com/imyeyu/network/CommonRequest.java b/src/main/java/com/imyeyu/network/CommonRequest.java index d6690a0..cadfb6d 100644 --- a/src/main/java/com/imyeyu/network/CommonRequest.java +++ b/src/main/java/com/imyeyu/network/CommonRequest.java @@ -1,8 +1,12 @@ package com.imyeyu.network; import com.imyeyu.java.TimiJava; +import com.imyeyu.java.bean.CallbackArg; +import org.apache.hc.client5.http.fluent.ContentResponseHandler; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.util.Timeout; import java.io.IOException; @@ -16,6 +20,9 @@ public class CommonRequest { protected final Request request; + /// 响应回调 + protected CallbackArg responseCallback; + protected CommonRequest(Request request) { this.request = request; } @@ -54,13 +61,22 @@ public class CommonRequest { return header("Accept-Language", langHeader); } + /// 设置响应回调,回调在响应体读取前执行 + /// + /// @param callback 响应回调,传入 null 表示移除回调 + /// @return 当前请求 + public CommonRequest responseCallback(CallbackArg callback) { + this.responseCallback = callback; + return this; + } + public CommonRequest bodyEntity(HttpEntity entity) { request.body(entity); return this; } public String asString() throws IOException { - return request.execute().returnContent().asString(); + return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asString()); } public Number asNumber() throws IOException { @@ -68,14 +84,29 @@ public class CommonRequest { } public byte[] asBytes() throws IOException { - return request.execute().returnContent().asBytes(); + return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asBytes()); } public InputStream asStream() throws IOException { - return request.execute().returnContent().asStream(); + return handleResponse(response -> new ContentResponseHandler().handleResponse(response).asStream()); } public void execute() throws IOException { - request.execute(); + handleResponse(response -> null); + } + + /// 使用统一入口处理响应,子类的自定义响应处理必须调用此方法 + /// + /// @param handler 响应处理器 + /// @param 返回值类型 + /// @return 响应处理结果 + /// @throws IOException 请求或响应处理失败 + protected T handleResponse(HttpClientResponseHandler handler) throws IOException { + return request.execute().handleResponse(response -> { + if (responseCallback != null) { + responseCallback.handler(response); + } + return handler.handleResponse(response); + }); } } diff --git a/src/main/java/com/imyeyu/network/FileRequest.java b/src/main/java/com/imyeyu/network/FileRequest.java index 642442c..f8ecddf 100644 --- a/src/main/java/com/imyeyu/network/FileRequest.java +++ b/src/main/java/com/imyeyu/network/FileRequest.java @@ -1,6 +1,7 @@ package com.imyeyu.network; import com.imyeyu.io.IO; +import com.imyeyu.java.bean.CallbackArg; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; @@ -59,6 +60,12 @@ public class FileRequest extends JacksonRequest { return this; } + @Override + public FileRequest responseCallback(CallbackArg callback) { + super.responseCallback(callback); + return this; + } + @Override public FileRequest body(Object object) { super.body(object); @@ -84,15 +91,16 @@ public class FileRequest extends JacksonRequest { } public long length() throws IOException, ProtocolException { - HttpResponse response = request.execute().returnResponse(); - final int status = response.getCode(); - if (status < 200 || 300 <= status) { - throw new IOException("HTTP error status: " + status); - } - final Header contentLengthHeader = response.getHeader("Content-Length"); - if (contentLengthHeader == null) { - throw new IOException("Content-Length header missing"); - } - return Long.parseLong(contentLengthHeader.getValue()); + return handleResponse(response -> { + final int status = response.getCode(); + if (status < 200 || 300 <= status) { + throw new IOException("HTTP error status: " + status); + } + final Header contentLengthHeader = response.getHeader("Content-Length"); + if (contentLengthHeader == null) { + throw new IOException("Content-Length header missing"); + } + return Long.parseLong(contentLengthHeader.getValue()); + }); } } diff --git a/src/main/java/com/imyeyu/network/JacksonRequest.java b/src/main/java/com/imyeyu/network/JacksonRequest.java index 8d9a0ea..f2c8f8e 100644 --- a/src/main/java/com/imyeyu/network/JacksonRequest.java +++ b/src/main/java/com/imyeyu/network/JacksonRequest.java @@ -8,9 +8,11 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.imyeyu.java.bean.CallbackArg; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; import java.io.IOException; import java.lang.reflect.Type; @@ -115,6 +117,12 @@ public class JacksonRequest extends CommonRequest { return this; } + @Override + public JacksonRequest responseCallback(CallbackArg callback) { + super.responseCallback(callback); + return this; + } + /** * 设置 JSON 请求体 * diff --git a/src/main/java/com/imyeyu/network/ProgressiveRequest.java b/src/main/java/com/imyeyu/network/ProgressiveRequest.java index e0639c6..bc102b9 100644 --- a/src/main/java/com/imyeyu/network/ProgressiveRequest.java +++ b/src/main/java/com/imyeyu/network/ProgressiveRequest.java @@ -2,9 +2,10 @@ package com.imyeyu.network; import com.imyeyu.io.IO; import com.imyeyu.java.TimiJava; +import com.imyeyu.java.bean.CallbackArg; import org.apache.hc.client5.http.fluent.Request; -import org.apache.hc.client5.http.fluent.Response; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -66,6 +67,12 @@ public class ProgressiveRequest extends FileRequest { return this; } + @Override + public ProgressiveRequest responseCallback(CallbackArg callback) { + super.responseCallback(callback); + return this; + } + @Override public ProgressiveRequest body(Object object) { super.body(object); @@ -80,12 +87,12 @@ public class ProgressiveRequest extends FileRequest { @Override public void toFile(Path outputPath) throws IOException, NoPermissionException { - processResponse(request.execute(), IO.getOutputStream(outputPath.toFile())); + processResponse(IO.getOutputStream(outputPath.toFile())); } @Override public void toFile(File file) throws IOException, NoPermissionException { - processResponse(request.execute(), IO.getOutputStream(file)); + processResponse(IO.getOutputStream(file)); } @Override @@ -96,17 +103,17 @@ public class ProgressiveRequest extends FileRequest { @Override public byte[] asBytes() throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); - processResponse(request.execute(), os); + processResponse(os); return os.toByteArray(); } - private void processResponse(Response response, OutputStream os) throws IOException { - response.handleResponse((HttpClientResponseHandler) resp -> { - HttpEntity entity = resp.getEntity(); + private void processResponse(OutputStream os) throws IOException { + handleResponse((HttpClientResponseHandler) response -> { + HttpEntity entity = response.getEntity(); if (entity == null) { throw new IOException("not found response entity"); } - int code = resp.getCode(); + int code = response.getCode(); if (400 <= code) { throw new IOException("response error: %s".formatted(code)); } diff --git a/src/main/java/com/imyeyu/network/TimiRequest.java b/src/main/java/com/imyeyu/network/TimiRequest.java index 3400124..ce68619 100644 --- a/src/main/java/com/imyeyu/network/TimiRequest.java +++ b/src/main/java/com/imyeyu/network/TimiRequest.java @@ -2,9 +2,11 @@ package com.imyeyu.network; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JavaType; +import com.imyeyu.java.bean.CallbackArg; import com.imyeyu.java.bean.timi.TimiResponse; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpResponse; import java.io.IOException; import java.lang.reflect.Type; @@ -55,6 +57,12 @@ public class TimiRequest extends JacksonRequest { return this; } + @Override + public TimiRequest responseCallback(CallbackArg callback) { + super.responseCallback(callback); + return this; + } + @Override public TimiRequest body(Object object) { super.body(object);