1
1
package com .codingapi .springboot .framework .rest ;
2
2
3
3
import com .alibaba .fastjson .JSON ;
4
- import com .codingapi .springboot .framework .rest .properties .RestApiProperties ;
4
+ import com .codingapi .springboot .framework .rest .properties .HttpProxyProperties ;
5
5
import lombok .extern .slf4j .Slf4j ;
6
6
import org .springframework .http .*;
7
7
import org .springframework .http .client .SimpleClientHttpRequestFactory ;
12
12
import java .net .InetSocketAddress ;
13
13
import java .net .Proxy ;
14
14
import java .net .URI ;
15
+ import java .util .Objects ;
15
16
16
17
@ Slf4j
17
18
public class HttpClient {
18
19
20
+ public interface IHttpResponseHandler {
21
+ String toResponse (HttpClient client ,URI uri ,ResponseEntity <String > response );
22
+ }
23
+
19
24
private final RestTemplate restTemplate ;
20
25
21
- private final String baseUrl ;
26
+ private final IHttpResponseHandler responseHandler ;
27
+
28
+ private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler () {
29
+
30
+ public HttpHeaders copyHeaders (HttpHeaders headers ){
31
+ HttpHeaders httpHeaders = new HttpHeaders ();
32
+ for (String key :headers .keySet ()){
33
+ httpHeaders .set (key , String .join (";" , Objects .requireNonNull (headers .get (key ))));
34
+ }
35
+ return httpHeaders ;
36
+ }
22
37
23
- public HttpClient (String baseUrl ) {
24
- this (null ,baseUrl );
38
+ @ Override
39
+ public String toResponse (HttpClient client , URI uri , ResponseEntity <String > response ) {
40
+ if (response .getStatusCode ().equals (HttpStatus .OK )){
41
+ return response .getBody ();
42
+ }
43
+ if (response .getStatusCode ().equals (HttpStatus .FOUND )){
44
+ HttpHeaders headers = response .getHeaders ();
45
+ String location = Objects .requireNonNull (headers .getLocation ()).toString ();
46
+ String baseUrl = uri .getScheme () + "://" + uri .getHost ()+":" +uri .getPort ();
47
+ String url = baseUrl +location ;
48
+ return client .get (url ,copyHeaders (headers ));
49
+ }
50
+ return response .getBody ();
51
+ }
52
+ };
53
+
54
+ public HttpClient () {
55
+ this (null ,defaultResponseHandler );
56
+ }
57
+
58
+ public HttpClient (IHttpResponseHandler responseHandler ) {
59
+ this (null ,responseHandler );
25
60
}
26
61
27
- public HttpClient (RestApiProperties properties , String baseUrl ) {
28
- this .baseUrl = baseUrl ;
62
+ public HttpClient (HttpProxyProperties properties ) {
63
+ this (properties ,defaultResponseHandler );
64
+ }
65
+
66
+ public HttpClient (HttpProxyProperties properties ,IHttpResponseHandler responseHandler ) {
67
+ this .responseHandler = responseHandler ;
29
68
this .restTemplate = RestTemplateContext .getInstance ().getRestTemplate ();
30
69
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory ();
31
70
requestFactory .setConnectTimeout (3000 );
@@ -39,37 +78,53 @@ public HttpClient(RestApiProperties properties, String baseUrl) {
39
78
this .restTemplate .setRequestFactory (requestFactory );
40
79
}
41
80
42
-
43
- private String buildUrl (String api ) {
44
- return baseUrl + api ;
45
- }
46
-
47
- public String post (String api , JSON jsonObject ) {
48
- return post (api , new HttpHeaders (), jsonObject );
81
+ public String post (String url , JSON jsonObject ) {
82
+ return post (url , new HttpHeaders (), jsonObject );
49
83
}
50
84
51
- public String post (String api , HttpHeaders headers , JSON jsonObject ) {
85
+ public String post (String url , HttpHeaders headers , JSON jsonObject ) {
52
86
headers .setContentType (MediaType .APPLICATION_JSON );
53
- String url = buildUrl (api );
54
87
HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (), headers );
55
88
ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
56
89
return httpResponse .getBody ();
57
90
}
58
91
59
- public String get (String api , HttpHeaders headers , MultiValueMap <String , String > uriVariables ) {
92
+ public String post (String url , MultiValueMap <String , String > formData ) {
93
+ return post (url ,new HttpHeaders (),formData );
94
+ }
95
+
96
+ public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
97
+ headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
98
+ HttpEntity <MultiValueMap <String , String >> httpEntity = new HttpEntity <>(formData , headers );
99
+ UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
100
+ URI uri = uriComponentsBuilder .build ().toUri ();
101
+ ResponseEntity <String > httpResponse = restTemplate .exchange (uri , HttpMethod .POST , httpEntity , String .class );
102
+ return responseHandler .toResponse (this ,uri ,httpResponse );
103
+ }
104
+
105
+ public String get (String url , HttpHeaders headers , MultiValueMap <String , String > uriVariables ) {
60
106
headers .setContentType (MediaType .APPLICATION_JSON );
61
- String url = buildUrl (api );
62
107
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
63
108
if (uriVariables != null ) {
64
109
uriComponentsBuilder = uriComponentsBuilder .queryParams (uriVariables );
65
110
}
66
111
URI uri = uriComponentsBuilder .build ().toUri ();
67
112
HttpEntity <String > httpEntity = new HttpEntity <>(headers );
68
113
ResponseEntity <String > httpResponse = restTemplate .exchange (uri , HttpMethod .GET , httpEntity , String .class );
69
- return httpResponse .getBody ();
114
+ return responseHandler .toResponse (this ,uri ,httpResponse );
115
+ }
116
+
117
+
118
+ public String get (String url , MultiValueMap <String , String > uriVariables ) {
119
+ return get (url , new HttpHeaders (), uriVariables );
70
120
}
71
121
72
- public String get (String api , MultiValueMap < String , String > uriVariables ) {
73
- return get (api , new HttpHeaders (), uriVariables );
122
+ public String get (String url , HttpHeaders headers ) {
123
+ return get (url , headers , null );
74
124
}
125
+
126
+ public String get (String url ) {
127
+ return get (url , new HttpHeaders (), null );
128
+ }
129
+
75
130
}
0 commit comments