Skip to content

Commit ab752f8

Browse files
author
berry_cooper
committed
httpclient config
1 parent 8c0c1ef commit ab752f8

File tree

4 files changed

+74
-47
lines changed

4 files changed

+74
-47
lines changed

src/main/java/com/berry/http/HttpClient.java

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class HttpClient {
3131
/**
3232
* 默认 http 客户端
3333
*/
34-
private static final OkHttpClient CLIENT;
34+
private static OkHttpClient CLIENT;
3535
/**
3636
* 连接超时时间 单位秒(默认10s)
3737
*/
@@ -63,15 +63,12 @@ public class HttpClient {
6363

6464
private static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
6565

66+
private static Dispatcher dispatcher = new Dispatcher();
67+
private static ConnectionPool pool = new ConnectionPool(CONNECTION_POOL_MAX_IDLE_COUNT, CONNECTION_POOL_MAX_IDLE_MINUTES, TimeUnit.MINUTES);
6668

6769
static {
68-
Dispatcher dispatcher = new Dispatcher();
6970
dispatcher.setMaxRequests(DISPATCHER_MAX_REQUESTS);
7071
dispatcher.setMaxRequestsPerHost(DISPATCHER_MAX_REQUESTS_PER_HOST);
71-
72-
ConnectionPool pool = new ConnectionPool(CONNECTION_POOL_MAX_IDLE_COUNT,
73-
CONNECTION_POOL_MAX_IDLE_MINUTES, TimeUnit.MINUTES);
74-
7572
CLIENT = new OkHttpClient.Builder()
7673
.callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS)
7774
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
@@ -81,13 +78,26 @@ public class HttpClient {
8178
.build();
8279
}
8380

81+
public HttpClient() {
82+
}
83+
84+
public HttpClient(int timeout) {
85+
CLIENT = new OkHttpClient.Builder()
86+
.callTimeout(timeout, TimeUnit.SECONDS)
87+
.connectTimeout(timeout, TimeUnit.SECONDS)
88+
.readTimeout(timeout * 3, TimeUnit.SECONDS)
89+
.dispatcher(dispatcher)
90+
.connectionPool(pool)
91+
.build();
92+
}
93+
8494
/**
8595
* Get 请求 无参数,无请求头
8696
*
8797
* @param url 地址
8898
* @return 响应
8999
*/
90-
public static Response get(String url) {
100+
public Response get(String url) {
91101
return get(url, null, null);
92102
}
93103

@@ -98,7 +108,7 @@ public static Response get(String url) {
98108
* @param header
99109
* @return
100110
*/
101-
public static Response get(String url, StringMap header) {
111+
public Response get(String url, StringMap header) {
102112
return get(url, null, header);
103113
}
104114

@@ -110,7 +120,7 @@ public static Response get(String url, StringMap header) {
110120
* @param header 请求头 map
111121
* @return 响应
112122
*/
113-
public static Response get(String url, @Nullable StringMap params, StringMap header) {
123+
public Response get(String url, @Nullable StringMap params, StringMap header) {
114124
if (params != null) {
115125
String urlParams = StringUtils.parseUrlParams(params);
116126
url = url + "?" + urlParams;
@@ -127,7 +137,7 @@ public static Response get(String url, @Nullable StringMap params, StringMap hea
127137
* @param headers 请求头 map
128138
* @return 响应
129139
*/
130-
public static Response postForm(String url, StringMap params, StringMap headers) {
140+
public Response postForm(String url, StringMap params, StringMap headers) {
131141
final FormBody.Builder fb = new FormBody.Builder();
132142
for (Map.Entry<String, Object> entry : params.entrySet()) {
133143
fb.add(entry.getKey(), entry.getValue().toString());
@@ -138,14 +148,14 @@ public static Response postForm(String url, StringMap params, StringMap headers)
138148
/**
139149
* 请求体为 字符串, 默认媒体类型-JSON
140150
*/
141-
public static Response post(String url, String body, StringMap header) {
151+
public Response post(String url, String body, StringMap header) {
142152
return post(url, StringUtils.utf8Bytes(body), header, JSON_MIME);
143153
}
144154

145155
/**
146156
* 复杂Map(包含字节数组)对象 以 json 格式请求,
147157
*/
148-
public static Response postComplex(String url, StringMap params, StringMap header) {
158+
public Response postComplex(String url, StringMap params, StringMap header) {
149159
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
150160
RequestBody requestBody = RequestBody.create(MediaType.get(APPLICATION_JSON_UTF8_VALUE), gson.toJson(params.map()));
151161
return post(url, requestBody, header);
@@ -154,26 +164,26 @@ public static Response postComplex(String url, StringMap params, StringMap heade
154164
/**
155165
* 请求体为 字节数组,默认媒体类型-JSON
156166
*/
157-
public static Response post(String url, byte[] body, StringMap header) {
167+
public Response post(String url, byte[] body, StringMap header) {
158168
return post(url, body, header, JSON_MIME);
159169
}
160170

161171
/**
162172
* 请求体为 字节数组,指定 媒体类型
163173
*/
164-
public static Response post(String url, byte[] body, StringMap header, String contentType) {
174+
public Response post(String url, byte[] body, StringMap header, String contentType) {
165175
RequestBody requestBody = RequestBody.create(MediaType.parse(contentType), body);
166176
return post(url, requestBody, header);
167177
}
168178

169179
/**
170180
* 批量文件上传
171181
*/
172-
public static Response multipartPost(String url,
173-
StringMap fields,
174-
String name,
175-
File[] files,
176-
StringMap headers) {
182+
public Response multipartPost(String url,
183+
StringMap fields,
184+
String name,
185+
File[] files,
186+
StringMap headers) {
177187
final MultipartBody.Builder mb = new MultipartBody.Builder();
178188
for (File file : files) {
179189
RequestBody fileBody = RequestBody.create(MediaType.parse(Constants.MULTIPART_MIME), file);
@@ -193,12 +203,12 @@ public static Response multipartPost(String url,
193203
/**
194204
* 文件上传 文件体为 file
195205
*/
196-
public static Response multipartPost(String url,
197-
StringMap fields,
198-
String name,
199-
String fileName,
200-
File fileBody,
201-
StringMap headers) {
206+
public Response multipartPost(String url,
207+
StringMap fields,
208+
String name,
209+
String fileName,
210+
File fileBody,
211+
StringMap headers) {
202212
RequestBody file = RequestBody.create(MediaType.parse(Constants.MULTIPART_MIME), fileBody);
203213
Request.Builder requestBuilder = getBuilder(url, fields, name, fileName, file);
204214
return send(requestBuilder, headers);
@@ -216,7 +226,7 @@ public static Response multipartPost(String url,
216226
* @param contentType 请求体类型
217227
* @param cb 异步回调
218228
*/
219-
public static void asyncPost(String url, byte[] body, int offset, int size, StringMap header, String contentType, AsyncCallback cb) {
229+
public void asyncPost(String url, byte[] body, int offset, int size, StringMap header, String contentType, AsyncCallback cb) {
220230
RequestBody requestBody = RequestBody.create(MediaType.parse(contentType), body, offset, size);
221231
Request.Builder requestBuilder = new Request.Builder().url(url).post(requestBody);
222232
asyncSend(requestBuilder, header, cb);
@@ -225,27 +235,27 @@ public static void asyncPost(String url, byte[] body, int offset, int size, Stri
225235
/**
226236
* 异步文件上传 文件体为 字节数组
227237
*/
228-
public static void asyncMultipartPost(String url,
229-
StringMap fields,
230-
String name,
231-
String fileName,
232-
byte[] fileBody,
233-
StringMap headers,
234-
AsyncCallback cb) {
238+
public void asyncMultipartPost(String url,
239+
StringMap fields,
240+
String name,
241+
String fileName,
242+
byte[] fileBody,
243+
StringMap headers,
244+
AsyncCallback cb) {
235245
RequestBody file = RequestBody.create(MediaType.parse(Constants.MULTIPART_MIME), fileBody);
236246
asyncMultipartPost(url, fields, name, fileName, file, headers, cb);
237247
}
238248

239249
/**
240250
* 异步文件上传 文件体为 file
241251
*/
242-
public static void asyncMultipartPost(String url,
243-
StringMap fields,
244-
String name,
245-
String fileName,
246-
File fileBody,
247-
StringMap headers,
248-
AsyncCallback cb) {
252+
public void asyncMultipartPost(String url,
253+
StringMap fields,
254+
String name,
255+
String fileName,
256+
File fileBody,
257+
StringMap headers,
258+
AsyncCallback cb) {
249259
RequestBody file = RequestBody.create(MediaType.parse(Constants.MULTIPART_MIME), fileBody);
250260
asyncMultipartPost(url, fields, name, fileName, file, headers, cb);
251261
}

src/main/java/com/berry/http/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public <T> T jsonToObject(Class<T> clazz) {
9090
}
9191

9292
public boolean isJson() {
93-
return this.contentType.equals(Constants.JSON_MIME);
93+
return StringUtils.isNotBlank(this.contentType) && this.contentType.startsWith(Constants.JSON_MIME);
9494
}
9595

9696
public int getCode() {

src/main/java/com/berry/storage/Config.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public final class Config {
1616
*/
1717
private String host;
1818

19+
/**
20+
* 上传超时 单位秒
21+
*/
22+
private int uploadTimeout = 10;
23+
1924
/**
2025
* 空间相关上传管理操作是否使用 https , 默认 否
2126
*/
@@ -25,11 +30,20 @@ public Config(String host) {
2530
this.host = host;
2631
}
2732

28-
public Config(String host, boolean useHttpsDomains) {
33+
public Config(String host, int uploadTimeout, boolean useHttpsDomains) {
2934
this.host = host;
35+
this.uploadTimeout = uploadTimeout;
3036
this.useHttpsDomains = useHttpsDomains;
3137
}
3238

39+
public int getUploadTimeout() {
40+
return uploadTimeout;
41+
}
42+
43+
public void setUploadTimeout(int uploadTimeout) {
44+
this.uploadTimeout = uploadTimeout;
45+
}
46+
3347
public String getHost() {
3448
return host;
3549
}

src/main/java/com/berry/storage/ObjectManage.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public final class ObjectManage {
3636
private final Auth auth;
3737
private final Config config;
3838

39+
private final HttpClient client;
40+
3941
public ObjectManage(Auth auth, Config config) {
4042
this.auth = auth;
4143
this.config = config;
44+
this.client = new HttpClient(config.getUploadTimeout());
4245
}
4346

4447
/**
@@ -59,7 +62,7 @@ public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, S
5962
params.put("data", fileData);
6063
String url = String.format("%s%s", config.getAddress(), UrlFactory.ObjectUrl.upload_byte.getUrl());
6164
StringMap header = auth.authorization(url);
62-
Response response = HttpClient.postComplex(url, params, header);
65+
Response response = client.postComplex(url, params, header);
6366
Result result = response.jsonToObject(Result.class);
6467
if (result.getCode().equals(Constants.API_SUCCESS_CODE) && result.getMsg().equals(Constants.API_SUCCESS_MSG)) {
6568
return Json.decode(Json.encode(result.getData()), ObjectInfo.class);
@@ -90,7 +93,7 @@ public JSONArray upload(String bucket, String acl, @Nullable String filePath, Fi
9093
String url = String.format("%s%s", config.getAddress(), UrlFactory.ObjectUrl.create.getUrl());
9194

9295
StringMap header = auth.authorization(url);
93-
Response response = HttpClient.multipartPost(url, fields, "file", files, header);
96+
Response response = client.multipartPost(url, fields, "file", files, header);
9497
Result result = response.jsonToObject(Result.class);
9598
if (result.getCode().equals(Constants.API_SUCCESS_CODE) && result.getMsg().equals(Constants.API_SUCCESS_MSG)) {
9699
return JSON.parseArray(JSON.toJSONString(result.getData()));
@@ -114,7 +117,7 @@ public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, S
114117
params.put("data", base64Data);
115118
String url = String.format("%s%s", config.getAddress(), UrlFactory.ObjectUrl.upload_base64.getUrl());
116119
StringMap header = auth.authorization(url);
117-
Response response = HttpClient.postComplex(url, params, header);
120+
Response response = client.postComplex(url, params, header);
118121
Result result = response.jsonToObject(Result.class);
119122
if (result.getCode().equals(Constants.API_SUCCESS_CODE) && result.getMsg().equals(Constants.API_SUCCESS_MSG)) {
120123
return Json.decode(Json.encode(result.getData()), ObjectInfo.class);
@@ -216,13 +219,13 @@ private Response get(String url) {
216219
logger.debug("request url:" + url);
217220
StringMap header = auth.authorization(url);
218221
System.out.println("header:" + Json.encode(header));
219-
return HttpClient.get(url, header);
222+
return client.get(url, header);
220223
}
221224

222225
private Response post(String url, StringMap params) {
223226
logger.debug("request url:" + url);
224227
StringMap header = auth.authorization(url);
225228
System.out.println("header:" + Json.encode(header));
226-
return HttpClient.post(url, params.jsonString(), header);
229+
return client.post(url, params.jsonString(), header);
227230
}
228231
}

0 commit comments

Comments
 (0)