/
HttpReq.java
305 lines (234 loc) · 6.32 KB
/
HttpReq.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package org.rapidoid.http;
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.data.JSON;
import org.rapidoid.io.Upload;
import org.rapidoid.u.U;
import org.rapidoid.util.Expectation;
import org.rapidoid.util.Msc;
import java.util.List;
import java.util.Map;
/*
* #%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 HttpReq extends RapidoidThing {
private final HttpClient client;
private volatile HttpVerb verb = null;
private volatile String url = null;
private volatile byte[] body = null;
private final Map<String, String> headers = Coll.synchronizedMap();
private final Map<String, String> cookies = Coll.synchronizedMap();
private final Map<String, Object> data = Coll.synchronizedMap();
private final Map<String, List<Upload>> files = Coll.synchronizedMap();
private volatile String contentType = null;
private volatile boolean raw = false;
private volatile int socketTimeout;
private volatile int connectTimeout;
private volatile int connectionRequestTimeout;
public HttpReq(HttpClient client) {
this.client = client;
this.socketTimeout = client.timeout();
this.connectTimeout = client.timeout();
this.connectionRequestTimeout = client.timeout();
}
public HttpReq verb(HttpVerb verb) {
this.verb = verb;
return this;
}
public HttpReq verb(String verb) {
return verb(HttpVerb.from(verb.toUpperCase()));
}
public HttpVerb verb() {
return this.verb;
}
public HttpReq url(String url) {
this.url = url;
return this;
}
public String url() {
return this.url;
}
public HttpReq body(byte[] body) {
this.body = body;
return this;
}
public byte[] body() {
return this.body;
}
public HttpReq headers(Map<String, String> headers) {
Coll.assign(this.headers, headers);
return this;
}
public Map<String, String> headers() {
return this.headers;
}
public HttpReq cookies(Map<String, String> cookies) {
Coll.assign(this.cookies, cookies);
return this;
}
public Map<String, String> cookies() {
return this.cookies;
}
public HttpReq data(Map<String, ?> data) {
Coll.assign(this.data, data);
return this;
}
public Map<String, Object> data() {
return this.data;
}
public HttpReq files(Map<String, List<Upload>> files) {
Coll.assign(this.files, files);
return this;
}
public Map<String, List<Upload>> files() {
return this.files;
}
public HttpReq contentType(String contentType) {
this.contentType = contentType;
return this;
}
public String contentType() {
return this.contentType;
}
public String fetch() {
return execute().body();
}
public String fetchRaw() {
raw(true);
try {
return execute().body();
} finally {
raw(false);
}
}
public HttpReq raw(boolean raw) {
this.raw = raw;
return this;
}
public boolean raw() {
return this.raw;
}
public HttpReq header(String name, String value) {
headers().put(name, value);
return this;
}
public HttpReq cookie(String name, String value) {
cookies().put(name, value);
return this;
}
public HttpReq data(String name, Object value) {
data().put(name, value);
return this;
}
public HttpReq file(String name, List<Upload> files) {
files().put(name, files);
return this;
}
public HttpReq socketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public int socketTimeout() {
return this.socketTimeout;
}
public HttpReq connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public int connectTimeout() {
return this.connectTimeout;
}
public HttpReq connectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
return this;
}
public int connectionRequestTimeout() {
return this.connectionRequestTimeout;
}
public <T> T parse() {
return JSON.parse(fetch());
}
public void print() {
U.print(fetch());
}
public Map<String, Object> toMap() {
return parse();
}
public Expectation expect() {
return new Expectation(fetch());
}
public HttpReq expect(String expectedValue) {
expect().value(expectedValue);
return this;
}
public <T> T toBean(Class<T> beanClass) {
return JSON.MAPPER.convertValue(toMap(), beanClass);
}
public HttpResp execute() {
return client.executeRequest(this, null).get();
}
public Future<HttpResp> execute(Callback<HttpResp> callback) {
return client.executeRequest(this, callback);
}
public HttpReq get(String url) {
return verb(HttpVerb.GET).url(url);
}
public HttpReq post(String url) {
return verb(HttpVerb.POST).url(url);
}
public HttpReq put(String url) {
return verb(HttpVerb.PUT).url(url);
}
public HttpReq delete(String url) {
return verb(HttpVerb.DELETE).url(url);
}
public HttpReq patch(String url) {
return verb(HttpVerb.PATCH).url(url);
}
public HttpReq options(String url) {
return verb(HttpVerb.OPTIONS).url(url);
}
public HttpReq head(String url) {
return verb(HttpVerb.HEAD).url(url);
}
public HttpReq trace(String url) {
return verb(HttpVerb.TRACE).url(url);
}
public void benchmark(int rounds, int threads, int requests) {
final HttpClient client = HTTP.client().reuseConnections(true).keepAlive(true)
.maxConnTotal(threads).maxConnPerRoute(threads).keepCookies(false);
final HttpReq req = this;
for (int i = 0; i < rounds; i++) {
Msc.benchmarkMT(threads, "req", requests, new Runnable() {
@Override
public void run() {
HttpResp resp = client.executeRequest(req, null).get();
U.notNull(resp, "HTTP response");
}
});
}
client.close();
}
}