-
Notifications
You must be signed in to change notification settings - Fork 164
/
HttpClient.java
240 lines (184 loc) · 5.23 KB
/
HttpClient.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.rapidoid.http;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.concurrent.Future;
import org.rapidoid.log.Log;
import org.rapidoid.util.LazyInit;
import java.util.Map;
import java.util.concurrent.Callable;
/*
* #%L
* rapidoid-http-client
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class HttpClient extends RapidoidThing {
private volatile String host = null;
private volatile String userAgent = null;
private volatile boolean followRedirects = false;
private volatile boolean keepAlive = false;
private volatile boolean keepCookies = false;
private volatile boolean reuseConnections = false;
private volatile boolean decompress = true;
private volatile int maxConnPerRoute = 0;
private volatile int maxConnTotal = 0;
private volatile int maxRedirects = 5;
private volatile int timeout = 5000;
private final Map<String, String> cookies = Coll.synchronizedMap();
private final LazyInit<CloseableHttpAsyncClient> client = new LazyInit<CloseableHttpAsyncClient>(
new Callable<CloseableHttpAsyncClient>() {
@Override
public CloseableHttpAsyncClient call() throws Exception {
CloseableHttpAsyncClient client = HttpClientUtil.client(HttpClient.this);
client.start();
return client;
}
});
public Future<HttpResp> executeRequest(HttpReq req, Callback<HttpResp> callback) {
return HttpClientUtil.request(req, client.get(), callback, false);
}
public HttpClient userAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public String userAgent() {
return this.userAgent;
}
public HttpClient followRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public boolean followRedirects() {
return this.followRedirects;
}
public HttpClient keepAlive(boolean keepAlive) {
this.keepAlive = keepAlive;
return this;
}
public boolean keepAlive() {
return this.keepAlive;
}
public HttpClient keepCookies(boolean keepCookies) {
this.keepCookies = keepCookies;
return this;
}
public boolean keepCookies() {
return this.keepCookies;
}
public HttpClient reuseConnections(boolean reuseConnections) {
this.reuseConnections = reuseConnections;
return this;
}
public boolean reuseConnections() {
return this.reuseConnections;
}
public HttpClient decompress(boolean decompress) {
this.decompress = decompress;
return this;
}
public boolean decompress() {
return this.decompress;
}
public HttpClient maxConnPerRoute(int maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
return this;
}
public int maxConnPerRoute() {
return this.maxConnPerRoute;
}
public HttpClient maxConnTotal(int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}
public int maxConnTotal() {
return this.maxConnTotal;
}
public HttpClient maxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
return this;
}
public int maxRedirects() {
return this.maxRedirects;
}
public HttpClient cookie(String name, String value) {
cookies().put(name, value);
return this;
}
public String host() {
return host;
}
public HttpClient host(String host) {
this.host = host;
return this;
}
public HttpClient cookies(Map<String, String> cookies) {
Coll.assign(this.cookies, cookies);
return this;
}
public Map<String, String> cookies() {
return this.cookies;
}
public int timeout() {
return timeout;
}
public HttpClient timeout(int timeout) {
this.timeout = timeout;
return this;
}
public HttpReq req() {
return new HttpReq(this);
}
public HttpReq get(String url) {
return req().verb(HttpVerb.GET).url(url);
}
public HttpReq post(String url) {
return req().verb(HttpVerb.POST).url(url);
}
public HttpReq put(String url) {
return req().verb(HttpVerb.PUT).url(url);
}
public HttpReq delete(String url) {
return req().verb(HttpVerb.DELETE).url(url);
}
public HttpReq patch(String url) {
return req().verb(HttpVerb.PATCH).url(url);
}
public HttpReq options(String url) {
return req().verb(HttpVerb.OPTIONS).url(url);
}
public HttpReq head(String url) {
return req().verb(HttpVerb.HEAD).url(url);
}
public HttpReq trace(String url) {
return req().verb(HttpVerb.TRACE).url(url);
}
public synchronized void close() {
try {
client.resetAndClose();
} catch (Exception e) {
Log.error("Error while closing the HTTP client!", e);
}
}
public synchronized void reset() {
close();
}
}