Skip to content

Commit 65b0cf4

Browse files
committed
refactor: 拦截器优化
1 parent 23f1e77 commit 65b0cf4

36 files changed

+50
-47
lines changed

forest-core/src/main/java/com/dtflys/forest/backend/httpclient/HttpClientLifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
55
import com.dtflys.forest.reflection.ForestMethod;
66

7-
public class HttpClientLifeCycle implements MethodAnnotationLifeCycle<HttpClient, Object> {
7+
public class HttpClientLifeCycle implements MethodAnnotationLifeCycle<HttpClient, Void> {
88

99
private final static String PARAM_KEY_HTTPCLIENT_PROVIDER = "__httpclient_provider";
1010

forest-core/src/main/java/com/dtflys/forest/backend/okhttp3/OkHttp3LifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
55
import com.dtflys.forest.reflection.ForestMethod;
66

7-
public class OkHttp3LifeCycle implements MethodAnnotationLifeCycle<OkHttp3, Object> {
7+
public class OkHttp3LifeCycle implements MethodAnnotationLifeCycle<OkHttp3, Void> {
88

99
private final static String PARAM_KEY_OKHTTP3_PROVIDER = "__okhttp3_provider";
1010

forest-core/src/main/java/com/dtflys/forest/handler/ResultHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public Object getResult(Optional<?> resultOpt, ForestRequest request, ForestResp
116116
}
117117
if (isReceivedResponseData(response)) {
118118
try {
119-
if (void.class.isAssignableFrom(resultClass)) {
119+
if (void.class.isAssignableFrom(resultClass) || Void.class.isAssignableFrom(resultClass)) {
120120
return null;
121121
}
122122
// 处理特殊泛型类型 (ForestResponse, ForestRequest, Optional)

forest-core/src/main/java/com/dtflys/forest/http/Lazy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.dtflys.forest.http;
22

3+
import com.dtflys.forest.reflection.ForestVariable;
4+
35
/**
46
* Forest 延迟求值 Lambda 接口
57
*
68
* @param <T> Lambda 返回值类型
79
* @author gongjun
810
* @since 1.5.29
911
*/
10-
public interface Lazy<T> {
12+
public interface Lazy<T> extends ForestVariable {
1113

1214
/**
1315
* 调用 Lambda 进行求值

forest-core/src/main/java/com/dtflys/forest/lifecycles/authorization/BasicAuthLifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.dtflys.forest.utils.Base64Utils;
88
import com.dtflys.forest.utils.StringUtils;
99

10-
public class BasicAuthLifeCycle implements MethodAnnotationLifeCycle<BasicAuth, Object> {
10+
public class BasicAuthLifeCycle implements MethodAnnotationLifeCycle<BasicAuth, Void> {
1111

1212
@Override
1313
public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {

forest-core/src/main/java/com/dtflys/forest/lifecycles/authorization/BearerAuthLifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.dtflys.forest.reflection.ForestMethod;
77
import com.dtflys.forest.utils.StringUtils;
88

9-
public class BearerAuthLifeCycle implements MethodAnnotationLifeCycle<BasicAuth, Object> {
9+
public class BearerAuthLifeCycle implements MethodAnnotationLifeCycle<BasicAuth, Void> {
1010

1111
@Override
1212
public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {

forest-core/src/main/java/com/dtflys/forest/lifecycles/authorization/OAuth2LifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @author HouKunLin
2626
* @since 1.5.0-BETA9
2727
*/
28-
public class OAuth2LifeCycle implements MethodAnnotationLifeCycle<OAuth2, Object> {
28+
public class OAuth2LifeCycle implements MethodAnnotationLifeCycle<OAuth2, Void> {
2929
private final byte[] lock = new byte[0];
3030
/**
3131
* Token 缓存

forest-core/src/main/java/com/dtflys/forest/lifecycles/base/BaseRequestLifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author gongjun[[email protected]]
1212
* @since 2020-08-23 23:03
1313
*/
14-
public class BaseRequestLifeCycle implements BaseAnnotationLifeCycle<BaseRequest, Object> {
14+
public class BaseRequestLifeCycle implements BaseAnnotationLifeCycle<BaseRequest, Void> {
1515

1616
@Override
1717
public void onProxyHandlerInitialized(InterfaceProxyHandler interfaceProxyHandler, BaseRequest annotation) {

forest-core/src/main/java/com/dtflys/forest/lifecycles/file/DownloadLifeCycle.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.dtflys.forest.extensions.DownloadFile;
77
import com.dtflys.forest.http.ForestRequest;
88
import com.dtflys.forest.http.ForestResponse;
9+
import com.dtflys.forest.interceptor.ResponseResult;
910
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
1011
import com.dtflys.forest.logging.ForestLogHandler;
1112
import com.dtflys.forest.logging.LogConfiguration;
@@ -28,7 +29,7 @@
2829
* @author gongjun[[email protected]]
2930
* @since 2020-08-04 02:29
3031
*/
31-
public class DownloadLifeCycle implements MethodAnnotationLifeCycle<DownloadFile, Object> {
32+
public class DownloadLifeCycle implements MethodAnnotationLifeCycle<DownloadFile, Void> {
3233

3334
public final static String ATTACHMENT_NAME_FILE = "__file";
3435

@@ -77,7 +78,7 @@ public void onProgress(ForestProgress progress) {
7778
}
7879

7980
@Override
80-
public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
81+
public void onSuccess(Void data, ForestRequest request, ForestResponse response) {
8182
final String dirPath = getAttributeAsString(request, "dir");
8283
String filename = getAttributeAsString(request, "filename");
8384
final Type resultType = getAttribute(request, "__resultType", Type.class);
@@ -99,17 +100,14 @@ public void onSuccess(Object data, ForestRequest request, ForestResponse respons
99100
}
100101
}
101102
InputStream in = null;
102-
if (data != null && data instanceof byte[]) {
103-
in = new ByteArrayInputStream((byte[]) data);
104-
} else {
105-
try {
106-
in = response.getInputStream();
107-
} catch (Exception e) {
108-
throw new ForestRuntimeException(e);
109-
}
103+
try {
104+
in = response.getInputStream();
105+
} catch (Exception e) {
106+
throw new ForestRuntimeException(e);
110107
}
111108
final String path = dir.getAbsolutePath() + File.separator + filename;
112109
final File file = new File(path);
110+
Object resultData = null;
113111
try {
114112
FileUtils.copyInputStreamToFile(in, file);
115113
FileUtils.waitFor(file, 10);
@@ -122,8 +120,8 @@ public void onSuccess(Object data, ForestRequest request, ForestResponse respons
122120
.getConfiguration()
123121
.getConverterMap()
124122
.get(ForestDataType.AUTO);
125-
data = converter.convertToJavaObject(file, resultType);
126-
response.setResult(data);
123+
resultData = converter.convertToJavaObject(file, resultType);
124+
response.setResult(resultData);
127125
}
128126
} catch (IOException e) {
129127
throw new ForestRuntimeException(e);
@@ -134,5 +132,8 @@ public void onSuccess(Object data, ForestRequest request, ForestResponse respons
134132
throw new ForestRuntimeException(e);
135133
}
136134
}
135+
137136
}
137+
138+
138139
}

forest-core/src/main/java/com/dtflys/forest/lifecycles/logging/BaseLogEnabledLifeCycle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.util.Optional;
1313

14-
public class BaseLogEnabledLifeCycle implements BaseAnnotationLifeCycle<LogEnabled, Object> {
14+
public class BaseLogEnabledLifeCycle implements BaseAnnotationLifeCycle<LogEnabled, Void> {
1515

1616
@Override
1717
public void onProxyHandlerInitialized(InterfaceProxyHandler interfaceProxyHandler, LogEnabled annotation) {

0 commit comments

Comments
 (0)