Skip to content

Commit fab2f9d

Browse files
author
jordanqin
committed
update qcloud sdk to 1.5.71
1 parent 86f1752 commit fab2f9d

29 files changed

+1186
-133
lines changed

QCloudFoundation/foundation/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
minSdkVersion 15
88
targetSdkVersion 28
99

10-
versionCode 10570
11-
versionName "1.5.70"
10+
versionCode 10572
11+
versionName "1.5.71"
1212

1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1414

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/HttpTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void onProgress(long complete, long target) {
7272
super("HttpTask-" + httpRequest.tag() + "-" + increments.getAndIncrement(), httpRequest.tag());
7373
this.httpRequest = httpRequest;
7474
this.credentialProvider = credentialProvider;
75-
this.networkProxy = networkClient.getNetworkProxy();
75+
this.networkProxy = networkClient.getNetworkProxyWrapper();
7676
this.networkProxy.identifier = this.getIdentifier();
7777
this.networkProxy.mProgressListener = mProgressListener;
7878
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/MultipartStreamRequestBody.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ public long contentLength() throws IOException {
143143

144144
@Override
145145
public void writeTo(BufferedSink sink) throws IOException {
146-
try {
147-
multipartBody.writeTo(sink);
148-
}finally {
149-
if(streamingRequestBody.countingSink != null) OkhttpInternalUtils.closeQuietly(streamingRequestBody.countingSink);
150-
}
146+
multipartBody.writeTo(sink);
151147
}
152148

153149
@Override

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/NetworkClient.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
package com.tencent.qcloud.core.http;
2424

25+
import com.tencent.qcloud.core.http.interceptor.QCloudRetryInterceptor;
26+
import com.tencent.qcloud.core.http.interceptor.QCloudTrafficControlInterceptor;
2527
import com.tencent.qcloud.core.task.RetryStrategy;
26-
import okhttp3.Dns;
2728

2829
import javax.net.ssl.HostnameVerifier;
29-
import javax.net.ssl.SSLSocketFactory;
30+
31+
import okhttp3.Dns;
3032

3133
public abstract class NetworkClient {
3234

@@ -35,6 +37,15 @@ public abstract class NetworkClient {
3537
protected boolean enableDebugLog;
3638
protected Dns dns;
3739

40+
private QCloudRetryInterceptor retryInterceptor;
41+
42+
/**
43+
* 开启业务拦截器:重试、流量控制
44+
*/
45+
public void enableQCloudInterceptor() {
46+
retryInterceptor = new QCloudRetryInterceptor(retryStrategy, new QCloudTrafficControlInterceptor());
47+
}
48+
3849
public void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier,
3950
Dns dns, HttpLogger httpLogger){
4051
this.retryStrategy = b.retryStrategy;
@@ -45,4 +56,9 @@ public void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier,
4556

4657
public abstract NetworkProxy getNetworkProxy();
4758

59+
public NetworkProxy getNetworkProxyWrapper(){
60+
NetworkProxy networkProxy = getNetworkProxy();
61+
networkProxy.setRetryInterceptor(retryInterceptor);
62+
return networkProxy;
63+
}
4864
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/NetworkProxy.java

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,98 @@
2525
import com.tencent.qcloud.core.common.QCloudClientException;
2626
import com.tencent.qcloud.core.common.QCloudProgressListener;
2727
import com.tencent.qcloud.core.common.QCloudServiceException;
28+
import com.tencent.qcloud.core.http.interceptor.QCloudRetryInterceptor;
29+
import com.tencent.qcloud.core.util.OkhttpInternalUtils;
30+
31+
import java.io.IOException;
32+
33+
import okhttp3.Request;
2834
import okhttp3.Response;
2935

3036
public abstract class NetworkProxy<T> {
31-
3237
protected HttpTaskMetrics metrics;
3338

3439
protected String identifier;
3540
protected QCloudProgressListener mProgressListener;
3641

42+
private QCloudRetryInterceptor mRetryInterceptor;
43+
public void setRetryInterceptor(QCloudRetryInterceptor qCloudRetryInterceptor) {
44+
this.mRetryInterceptor = qCloudRetryInterceptor;
45+
}
46+
3747
protected abstract void cancel();
3848

39-
protected abstract HttpResult<T> executeHttpRequest(HttpRequest<T> httpRequest) throws QCloudClientException,
40-
QCloudServiceException;
49+
protected HttpResult<T> executeHttpRequest(HttpRequest<T> httpRequest) throws QCloudClientException,
50+
QCloudServiceException{
51+
QCloudClientException clientException = null;
52+
QCloudServiceException serviceException = null;
53+
Response response = null;
54+
HttpResult<T> httpResult = null;
55+
boolean selfCloseConverter = httpRequest.getResponseBodyConverter() instanceof SelfCloseConverter;
56+
try {
57+
httpRequest.setOkHttpRequestTag(identifier);
58+
Request okHttpRequest = httpRequest.buildRealRequest();
59+
60+
if(mRetryInterceptor != null){
61+
response = mRetryInterceptor.intercept(this, okHttpRequest);
62+
} else {
63+
response = callHttpRequest(okHttpRequest);
64+
}
65+
66+
if (response != null) {
67+
httpResult = convertResponse(httpRequest, response);
68+
} else {
69+
serviceException = new QCloudServiceException("http response is null");
70+
}
71+
} catch (IOException e) {
72+
if (e.getCause() instanceof QCloudClientException) {
73+
clientException = (QCloudClientException) e.getCause();
74+
} else if (e.getCause() instanceof QCloudServiceException) {
75+
serviceException = (QCloudServiceException) e.getCause();
76+
} else {
77+
clientException = new QCloudClientException(e);
78+
}
79+
} finally {
80+
if(response != null && !selfCloseConverter) {
81+
OkhttpInternalUtils.closeQuietly(response);
82+
}
83+
try {
84+
disconnect();
85+
} catch (Exception ignore) {}
86+
}
87+
88+
if (clientException != null) {
89+
throw clientException;
90+
} else if (serviceException != null) {
91+
throw serviceException;
92+
} else {
93+
return httpResult;
94+
}
95+
}
96+
97+
protected HttpResult<T> convertResponse(HttpRequest<T> request, Response response) throws QCloudClientException,
98+
QCloudServiceException{
99+
HttpResponse<T> httpResponse = new HttpResponse<>(request, response);
100+
ResponseBodyConverter<T> converter = request.getResponseBodyConverter();
101+
if (converter instanceof ProgressBody) {
102+
((ProgressBody) converter).setProgressListener(mProgressListener);
103+
}
104+
T content = converter.convert(httpResponse);
105+
return new HttpResult<T>(httpResponse, content);
106+
}
107+
108+
// TODO: 2025/1/6 去除okhttp的Response和Request类型依赖
109+
110+
/**
111+
* 发送网络请求
112+
* @param okHttpRequest 请求
113+
* @return 响应
114+
* @throws IOException iO异常
115+
*/
116+
public abstract Response callHttpRequest(Request okHttpRequest) throws IOException;
41117

42-
protected abstract HttpResult<T> convertResponse(HttpRequest<T> httpRequest, Response response) throws QCloudClientException,
43-
QCloudServiceException;
118+
/**
119+
* 断开网络连接(有必要的话可以重写此方法)
120+
*/
121+
protected void disconnect(){}
44122
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/OkHttpProxy.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ protected HttpResult<T> executeHttpRequest(HttpRequest<T> httpRequest) throws QC
126126
}
127127
}
128128

129+
@Override
130+
public Response callHttpRequest(Request okHttpRequest) throws IOException {
131+
// 如果重写了executeHttpRequest,则不需要再调用callHttpRequest
132+
return null;
133+
}
134+
129135
private void recordDns(String host, CallMetricsListener eventListener) {
130136

131137
List<InetAddress> dnsRecord = null;
@@ -139,14 +145,5 @@ private boolean isCosResponse(Response response) {
139145
return response != null && "tencent-cos".equalsIgnoreCase(response.header("Server"));
140146
}
141147

142-
@Override
143-
protected HttpResult<T> convertResponse(HttpRequest<T> request, Response response) throws QCloudClientException, QCloudServiceException {
144-
HttpResponse<T> httpResponse = new HttpResponse<>(request, response);
145-
ResponseBodyConverter<T> converter = request.getResponseBodyConverter();
146-
if (converter instanceof ProgressBody) {
147-
((ProgressBody) converter).setProgressListener(mProgressListener);
148-
}
149-
T content = converter.convert(httpResponse);
150-
return new HttpResult<T>(httpResponse, content);
151-
}
148+
152149
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/QCloudHttpClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ private QCloudHttpClient(Builder b) {
225225
int hashCode = networkClientType.hashCode();
226226
if(!networkClientMap.containsKey(hashCode)){
227227
networkClient.init(b, hostnameVerifier(), mDns, httpLogger);
228+
// 如果是自定义的网络库则需要在业务层实现流量控制和重试
229+
// quic暂时保持原样,后续重构quic时放开这里,把quic也当做自定义网络层处理
230+
if(!"com.tencent.qcloud.quic.QuicClientImpl".equals(networkClientType)) {
231+
networkClient.enableQCloudInterceptor();
232+
}
228233
networkClientMap.put(hashCode, networkClient);
229234
}
230235
}
@@ -245,6 +250,11 @@ public void setNetworkClientType(Builder b){
245250
int hashCode = networkClientType.hashCode();
246251
if(!networkClientMap.containsKey(hashCode)){
247252
networkClient.init(b, hostnameVerifier(), mDns, httpLogger);
253+
// 如果是自定义的网络库则需要在业务层实现流量控制和重试
254+
// quic暂时保持原样,后续重构quic时放开这里,把quic也当做自定义网络层处理
255+
if(!"com.tencent.qcloud.quic.QuicClientImpl".equals(networkClientType)) {
256+
networkClient.enableQCloudInterceptor();
257+
}
248258
networkClientMap.put(hashCode, networkClient);
249259
}
250260
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/StreamingRequestBody.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ public void writeTo(BufferedSink sink) throws IOException {
273273
} finally {
274274
if(inputStream != null) OkhttpInternalUtils.closeQuietly(inputStream);
275275
if(source != null) OkhttpInternalUtils.closeQuietly(source);
276-
if(countingSink != null) OkhttpInternalUtils.closeQuietly(countingSink);
277276
}
278277
}
279278

0 commit comments

Comments
 (0)