forked from troyzhxu/okhttps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHttpTask.java
More file actions
150 lines (130 loc) · 3.29 KB
/
SHttpTask.java
File metadata and controls
150 lines (130 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cn.zhxu.okhttps;
import cn.zhxu.okhttps.HttpResult.State;
import cn.zhxu.okhttps.internal.AbstractHttpClient;
import cn.zhxu.okhttps.internal.RealHttpResult;
import okhttp3.Call;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
* 同步 Http 请求任务
*
* @author Troy.Zhou
*
*/
public class SHttpTask extends HttpTask<SHttpTask> {
public SHttpTask(AbstractHttpClient httpImpl, String url) {
super(httpImpl, url);
}
@Override
public boolean isSyncHttp() {
return true;
}
/**
* 发起 GET 请求(Rest:获取资源,幂等)
* @return 请求结果
*/
public HttpResult get() {
return request(HTTP.GET);
}
/**
* 发起 HEAD 请求(Rest:读取资源头信息,幂等)
* @return 请求结果
*/
public HttpResult head() {
return request(HTTP.HEAD);
}
/**
* 发起 POST 请求(Rest:创建资源,非幂等)
* @return 请求结果
*/
public HttpResult post() {
return request(HTTP.POST);
}
/**
* 发起 PUT 请求(Rest:更新资源,幂等)
* @return 请求结果
*/
public HttpResult put() {
return request(HTTP.PUT);
}
/**
* 发起 PATCH 请求(Rest:更新资源,部分更新,幂等)
* @return HttpCall
*/
public HttpResult patch() {
return request(HTTP.PATCH);
}
/**
* 发起 DELETE 请求(Rest:删除资源,幂等)
* @return 请求结果
*/
public HttpResult delete() {
return request(HTTP.DELETE);
}
/**
* 发起 HTTP 请求
* @param method 请求方法
* @return 请求结果
*/
public HttpResult request(String method) {
if (method == null || method.isEmpty()) {
throw new IllegalArgumentException("HTTP 请求方法 method 不可为空!");
}
RealHttpResult result = new RealHttpResult(this, httpClient.executor());
SyncHttpCall httpCall = new SyncHttpCall();
// 注册标签任务
registeTagTask(httpCall);
CountDownLatch latch = new CountDownLatch(1);
httpClient.preprocess(this, () -> {
synchronized (httpCall) {
if (httpCall.canceled) {
result.exception(State.CANCELED, null);
latch.countDown();
return;
}
httpCall.call = prepareCall(method);
}
try {
result.response(httpCall.call.execute());
httpCall.done = true;
} catch (IOException e) {
result.exception(toState(e), e);
} finally {
latch.countDown();
}
}, skipPreproc, skipSerialPreproc);
boolean timeout = false;
if (result.getState() == null) {
timeout = timeoutAwait(latch);
}
// 移除标签任务
removeTagTask();
if (timeout) {
httpCall.cancel();
return timeoutResult();
}
IOException e = result.getError();
State state = result.getState();
if (e != null && state != State.CANCELED
&& !nothrow) {
throw new OkHttpsException(state, "同步请求异常:" + getUrl(), e);
}
return result;
}
static class SyncHttpCall implements Cancelable {
Call call;
boolean done = false;
boolean canceled = false;
@Override
public synchronized boolean cancel() {
if (done) {
return false;
}
if (call != null) {
call.cancel();
}
canceled = true;
return true;
}
}
}