Skip to content

Commit 6159e20

Browse files
committed
refactor: response struct
1 parent 353d839 commit 6159e20

File tree

10 files changed

+223
-219
lines changed

10 files changed

+223
-219
lines changed

src/main/java/com/hb0730/https/HttpSync.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import com.hb0730.https.exception.HttpException;
66
import com.hb0730.https.inter.AbstractSyncHttp;
77
import com.hb0730.https.inter.SyncHttp;
8+
import com.hb0730.https.support.SimpleHttpResponse;
89
import com.hb0730.https.support.httpclient.HttpClientSyncImpl;
910
import com.hb0730.https.support.hutool.HutoolSyncImpl;
1011
import com.hb0730.https.support.okhttp3.OkHttp3SyncImpl;
1112
import com.hb0730.https.utils.ClassUtils;
1213

13-
import java.io.InputStream;
1414
import java.util.Map;
1515

1616
/**
@@ -89,7 +89,7 @@ public HttpSync setHeader(HttpHeader header) {
8989
* @return 结果
9090
*/
9191
@Override
92-
public String get(String url) {
92+
public SimpleHttpResponse get(String url) {
9393
checkHttpNotNull(proxy);
9494
return proxy.get(url);
9595
}
@@ -102,7 +102,7 @@ public String get(String url) {
102102
* @return 结果
103103
*/
104104
@Override
105-
public String get(String url, Map<String, String> params) {
105+
public SimpleHttpResponse get(String url, Map<String, String> params) {
106106
checkHttpNotNull(proxy);
107107
return proxy.get(url, params);
108108
}
@@ -114,7 +114,7 @@ public String get(String url, Map<String, String> params) {
114114
* @return 结果
115115
*/
116116
@Override
117-
public String post(String url) {
117+
public SimpleHttpResponse post(String url) {
118118
checkHttpNotNull(proxy);
119119
return proxy.post(url);
120120
}
@@ -127,23 +127,17 @@ public String post(String url) {
127127
* @return 结果
128128
*/
129129
@Override
130-
public String post(String url, String data) {
130+
public SimpleHttpResponse post(String url, String data) {
131131
checkHttpNotNull(proxy);
132132
return proxy.post(url, data);
133133
}
134134

135135
@Override
136-
public String post(String url, String dataJson, HttpHeader header) {
136+
public SimpleHttpResponse post(String url, String dataJson, HttpHeader header) {
137137
checkHttpNotNull(proxy);
138138
return proxy.post(url, dataJson, header);
139139
}
140140

141-
@Override
142-
public InputStream postStream(String url, String dataJson) {
143-
checkHttpNotNull(proxy);
144-
return proxy.postStream(url, dataJson);
145-
}
146-
147141
/**
148142
* POST 请求
149143
*
@@ -152,13 +146,13 @@ public InputStream postStream(String url, String dataJson) {
152146
* @return 结果
153147
*/
154148
@Override
155-
public String post(String url, Map<String, String> params) {
149+
public SimpleHttpResponse post(String url, Map<String, String> params) {
156150
checkHttpNotNull(proxy);
157151
return proxy.post(url, params);
158152
}
159153

160154
@Override
161-
public String post(String url, Map<String, String> formData, HttpHeader header) {
155+
public SimpleHttpResponse post(String url, Map<String, String> formData, HttpHeader header) {
162156
checkHttpNotNull(proxy);
163157
return proxy.post(url, formData, header);
164158
}

src/main/java/com/hb0730/https/inter/SyncHttp.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hb0730.https.inter;
22

33
import com.hb0730.https.HttpHeader;
4+
import com.hb0730.https.support.SimpleHttpResponse;
45

56
import java.io.InputStream;
67
import java.util.Map;
@@ -19,7 +20,7 @@ public interface SyncHttp extends Http {
1920
* @param url 请求地址
2021
* @return 响应结果
2122
*/
22-
String get(String url);
23+
SimpleHttpResponse get(String url);
2324

2425
/**
2526
* get 请求
@@ -28,15 +29,15 @@ public interface SyncHttp extends Http {
2829
* @param params 请求参数
2930
* @return 响应结果
3031
*/
31-
String get(String url, Map<String, String> params);
32+
SimpleHttpResponse get(String url, Map<String, String> params);
3233

3334
/**
3435
* post请求
3536
*
3637
* @param url 请求地址
3738
* @return 响应结果
3839
*/
39-
String post(String url);
40+
SimpleHttpResponse post(String url);
4041

4142
/**
4243
* post请求
@@ -45,7 +46,7 @@ public interface SyncHttp extends Http {
4546
* @param dataJson 请求参数,json格式
4647
* @return 响应结果
4748
*/
48-
String post(String url, String dataJson);
49+
SimpleHttpResponse post(String url, String dataJson);
4950

5051
/**
5152
* post请求
@@ -55,16 +56,7 @@ public interface SyncHttp extends Http {
5556
* @param header 一次性请求头
5657
* @return 响应结果
5758
*/
58-
String post(String url, String dataJson, HttpHeader header);
59-
60-
/**
61-
* post请求
62-
*
63-
* @param url 请求地址
64-
* @param dataJson 请求参数,json格式
65-
* @return 响应流
66-
*/
67-
InputStream postStream(String url, String dataJson);
59+
SimpleHttpResponse post(String url, String dataJson, HttpHeader header);
6860

6961
/**
7062
* post请求
@@ -73,7 +65,7 @@ public interface SyncHttp extends Http {
7365
* @param formdata form 参数
7466
* @return 响应结果
7567
*/
76-
String post(String url, Map<String, String> formdata);
68+
SimpleHttpResponse post(String url, Map<String, String> formdata);
7769

7870
/**
7971
* post 请求
@@ -83,6 +75,6 @@ public interface SyncHttp extends Http {
8375
* @param header 请求头
8476
* @return 响应结果
8577
*/
86-
String post(String url, Map<String, String> formData, HttpHeader header);
78+
SimpleHttpResponse post(String url, Map<String, String> formData, HttpHeader header);
8779

8880
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.hb0730.https.support;
2+
3+
import cn.hutool.core.io.IoUtil;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.io.InputStream;
10+
import java.nio.charset.Charset;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
/**
16+
* 响应封装
17+
*
18+
* @author <a href="mailto:huangbing0730@gmail">hb0730</a>
19+
* @date 2022/3/14
20+
* @since 1.0.0
21+
*/
22+
@Data
23+
@AllArgsConstructor
24+
@NoArgsConstructor
25+
@Builder
26+
public class SimpleHttpResponse {
27+
/**
28+
* 是否成功
29+
*/
30+
private boolean success;
31+
/**
32+
* 请求头
33+
*/
34+
private Map<String, List<String>> headers;
35+
/**
36+
* body stream
37+
*/
38+
private InputStream body;
39+
40+
/**
41+
* 获取body,默认UTF-8编码
42+
*
43+
* @return body
44+
* @see #getBody(Charset)
45+
*/
46+
public String getBody() {
47+
return this.getBody(StandardCharsets.UTF_8);
48+
}
49+
50+
/**
51+
* 获取body,自动关闭输入流
52+
*
53+
* @param charset 编码格式
54+
* @return body
55+
*/
56+
public String getBody(Charset charset) {
57+
return IoUtil.read(this.body, charset);
58+
}
59+
60+
public SimpleHttpResponse body(byte[] body) {
61+
this.body = IoUtil.toStream(body);
62+
return this;
63+
}
64+
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.hb0730.https.support.callback;
22

3-
import com.hb0730.https.support.httpclient.HttpClientAsyncImpl;
4-
import com.hb0730.https.support.okhttp3.OkHttp3AsyncImpl;
5-
import okhttp3.Request;
6-
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
7-
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
3+
import com.hb0730.https.support.SimpleHttpResponse;
84

95
import java.io.IOException;
106

@@ -21,16 +17,26 @@ public interface HttpCallback {
2117
*
2218
* @param result 响应结果
2319
* @throws IOException 异常
24-
* @see OkHttp3AsyncImpl#exec(Request.Builder, HttpCallback)
25-
* @see HttpClientAsyncImpl#exec(HttpUriRequestBase, BasicRequestProducer, HttpCallback)
20+
* @see #response(SimpleHttpResponse)
2621
*/
27-
void success(String result) throws IOException;
22+
@Deprecated
23+
default void success(String result) throws IOException {
24+
}
25+
26+
/**
27+
* 响应成功 200 &gt;= http code &lt; 300
28+
*
29+
* @param response 响应结果
30+
* @throws IOException 异常
31+
*/
32+
void response(SimpleHttpResponse response) throws IOException;
2833

2934
/**
3035
* 请求参数
3136
*
3237
* @param e 异常
3338
*/
34-
void failure(Exception e);
35-
39+
@Deprecated
40+
default void failure(Exception e) {
41+
}
3642
}

src/main/java/com/hb0730/https/support/httpclient/HttpClientAsyncImpl.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.hb0730.https.constants.Constants;
55
import com.hb0730.https.exception.HttpException;
66
import com.hb0730.https.inter.AbstractAsyncHttp;
7+
import com.hb0730.https.support.SimpleHttpResponse;
78
import com.hb0730.https.support.callback.HttpCallback;
89
import com.hb0730.https.utils.CollectionUtils;
910
import com.hb0730.https.utils.MapUtils;
1011
import com.hb0730.https.utils.StringUtils;
1112
import lombok.Getter;
12-
import lombok.SneakyThrows;
1313
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
1414
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
1515
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
@@ -18,30 +18,33 @@
1818
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
1919
import org.apache.hc.core5.concurrent.FutureCallback;
2020
import org.apache.hc.core5.http.ContentType;
21+
import org.apache.hc.core5.http.Header;
2122
import org.apache.hc.core5.http.HttpHost;
2223
import org.apache.hc.core5.http.HttpRequest;
2324
import org.apache.hc.core5.http.HttpResponse;
2425
import org.apache.hc.core5.http.HttpStatus;
2526
import org.apache.hc.core5.http.Message;
2627
import org.apache.hc.core5.http.NameValuePair;
27-
import org.apache.hc.core5.http.config.CharCodingConfig;
2828
import org.apache.hc.core5.http.message.BasicNameValuePair;
2929
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
30-
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
30+
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
3131
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
3232
import org.apache.hc.core5.net.URIBuilder;
3333
import org.apache.hc.core5.net.WWWFormCodec;
3434

35+
import java.io.IOException;
3536
import java.net.InetSocketAddress;
3637
import java.net.Proxy;
3738
import java.net.URI;
3839
import java.net.URISyntaxException;
3940
import java.nio.charset.Charset;
4041
import java.nio.charset.StandardCharsets;
4142
import java.util.ArrayList;
43+
import java.util.Arrays;
4244
import java.util.List;
4345
import java.util.Map;
4446
import java.util.concurrent.TimeUnit;
47+
import java.util.stream.Collectors;
4548

4649
/**
4750
* HTTPClient async,需要自行关闭{@link #httpClient}
@@ -140,21 +143,28 @@ public void post(String url, Map<String, String> formdata, HttpCallback httpCall
140143

141144
private void exec(AsyncRequestProducer producer, HttpCallback httpCallback) {
142145
this.httpClient.start();
143-
CharCodingConfig config = CharCodingConfig.custom().setCharset(getCharSet()).build();
144-
StringAsyncEntityConsumer stringConsumer = new StringAsyncEntityConsumer(config);
145-
BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(stringConsumer);
146-
this.httpClient.execute(producer, responseConsumer, new FutureCallback<Message<HttpResponse, String>>() {
147-
@SneakyThrows
146+
this.httpClient.execute(producer, new BasicResponseConsumer<byte[]>(new BasicAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, byte[]>>() {
148147
@Override
149-
public void completed(Message<HttpResponse, String> result) {
150-
if (null == httpCallback) {
151-
return;
152-
}
153-
HttpResponse head = result.getHead();
154-
if (head.getCode() >= HttpStatus.SC_SUCCESS && head.getCode() < HttpStatus.SC_REDIRECTION) {
155-
httpCallback.success(result.getBody());
156-
} else {
157-
httpCallback.failure(new HttpException("Unexpected response status: " + head.getCode()));
148+
public void completed(Message<HttpResponse, byte[]> result) {
149+
HttpResponse response = result.getHead();
150+
boolean success = (response.getCode() >= HttpStatus.SC_SUCCESS && response.getCode() < HttpStatus.SC_REDIRECTION);
151+
Map<String, List<String>> headers = Arrays.stream(response.getHeaders())
152+
.collect(Collectors.toMap(Header::getName,
153+
(value) -> {
154+
List<String> valueList = new ArrayList<>();
155+
valueList.add(value.getValue());
156+
return valueList;
157+
}
158+
, (v1Value, v2Value) -> v2Value));
159+
SimpleHttpResponse httpResponse = SimpleHttpResponse
160+
.builder()
161+
.success(success)
162+
.headers(headers)
163+
.build().body(result.getBody());
164+
try {
165+
httpCallback.response(httpResponse);
166+
} catch (IOException e) {
167+
e.printStackTrace();
158168
}
159169
}
160170

0 commit comments

Comments
 (0)