23
23
@ Slf4j
24
24
public class HttpClient {
25
25
26
+ public interface IHttpRequestHandler {
27
+ String handler (HttpClient client ,String uri ,HttpMethod method ,HttpHeaders headers ,HttpEntity <?> httpEntity );
28
+ }
29
+
26
30
public interface IHttpResponseHandler {
27
- String toResponse (HttpClient client ,String uri ,ResponseEntity <String > response );
31
+ String handler (HttpClient client ,String uri ,ResponseEntity <String > response );
28
32
}
29
33
30
34
private final RestTemplate restTemplate ;
31
35
32
36
private final IHttpResponseHandler responseHandler ;
33
37
38
+ private final IHttpRequestHandler requestHandler ;
39
+
34
40
private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler () {
35
41
36
42
public HttpHeaders copyHeaders (HttpHeaders headers ){
@@ -42,7 +48,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
42
48
}
43
49
44
50
@ Override
45
- public String toResponse (HttpClient client , String url , ResponseEntity <String > response ) {
51
+ public String handler (HttpClient client , String url , ResponseEntity <String > response ) {
46
52
if (response .getStatusCode ().equals (HttpStatus .OK )){
47
53
return response .getBody ();
48
54
}
@@ -63,6 +69,14 @@ public String toResponse(HttpClient client, String url, ResponseEntity<String> r
63
69
}
64
70
};
65
71
72
+ private static final IHttpRequestHandler defaultRequestHandler = new IHttpRequestHandler () {
73
+
74
+ @ Override
75
+ public String handler (HttpClient client , String uri ,HttpMethod method , HttpHeaders headers , HttpEntity <?> httpEntity ) {
76
+ return uri ;
77
+ }
78
+ };
79
+
66
80
private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler () {
67
81
@ Override
68
82
public boolean hasError (ClientHttpResponse response ) throws IOException {
@@ -74,19 +88,20 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
74
88
};
75
89
76
90
public HttpClient () {
77
- this (null ,defaultResponseHandler );
91
+ this (null ,defaultRequestHandler , defaultResponseHandler );
78
92
}
79
93
80
- public HttpClient (IHttpResponseHandler responseHandler ) {
81
- this (null ,responseHandler );
94
+ public HttpClient (IHttpRequestHandler requestHandler , IHttpResponseHandler responseHandler ) {
95
+ this (null ,requestHandler , responseHandler );
82
96
}
83
97
84
98
public HttpClient (HttpProxyProperties properties ) {
85
- this (properties ,defaultResponseHandler );
99
+ this (properties ,defaultRequestHandler , defaultResponseHandler );
86
100
}
87
101
88
- public HttpClient (HttpProxyProperties properties ,IHttpResponseHandler responseHandler ) {
89
- this .responseHandler = responseHandler ;
102
+ public HttpClient (HttpProxyProperties properties ,IHttpRequestHandler requestHandler ,IHttpResponseHandler responseHandler ) {
103
+ this .requestHandler = requestHandler ==null ?defaultRequestHandler :requestHandler ;
104
+ this .responseHandler = responseHandler ==null ?defaultResponseHandler :responseHandler ;
90
105
this .restTemplate = RestTemplateContext .getInstance ().getRestTemplate ();
91
106
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory ();
92
107
requestFactory .setConnectTimeout (3000 );
@@ -106,14 +121,16 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
106
121
107
122
public String post (String url , HttpHeaders headers , JSON jsonObject ) {
108
123
HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (SerializerFeature .WriteMapNullValue ), headers );
109
- ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
110
- return responseHandler .toResponse (this ,url ,httpResponse );
124
+ String requestUrl = requestHandler .handler (this ,url , HttpMethod .POST ,headers ,httpEntity );
125
+ ResponseEntity <String > httpResponse = restTemplate .exchange (requestUrl , HttpMethod .POST , httpEntity , String .class );
126
+ return responseHandler .handler (this ,url ,httpResponse );
111
127
}
112
128
113
129
public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
114
130
HttpEntity <MultiValueMap <String , String >> httpEntity = new HttpEntity <>(formData , headers );
115
- ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
116
- return responseHandler .toResponse (this ,url ,httpResponse );
131
+ String requestUrl = requestHandler .handler (this ,url ,HttpMethod .POST ,headers ,httpEntity );
132
+ ResponseEntity <String > httpResponse = restTemplate .exchange (requestUrl , HttpMethod .POST , httpEntity , String .class );
133
+ return responseHandler .handler (this ,url ,httpResponse );
117
134
}
118
135
119
136
public String get (String url , HttpHeaders headers , MultiValueMap <String , String > uriVariables ) {
@@ -123,11 +140,13 @@ public String get(String url, HttpHeaders headers, MultiValueMap<String, String>
123
140
URI uri = UriComponentsBuilder .fromHttpUrl (url )
124
141
.queryParams (uriVariables )
125
142
.build (true ).toUri ();
126
- httpResponse = restTemplate .exchange (uri , HttpMethod .GET , httpEntity , String .class );
143
+ String requestUrl = requestHandler .handler (this ,uri .toString (), HttpMethod .GET ,headers ,httpEntity );
144
+ httpResponse = restTemplate .exchange (requestUrl , HttpMethod .GET , httpEntity , String .class );
127
145
}else {
128
- httpResponse = restTemplate .exchange (url , HttpMethod .GET , httpEntity , String .class );
146
+ String requestUrl = requestHandler .handler (this ,url , HttpMethod .GET ,headers ,httpEntity );
147
+ httpResponse = restTemplate .exchange (requestUrl , HttpMethod .GET , httpEntity , String .class );
129
148
}
130
- return responseHandler .toResponse (this , url , httpResponse );
149
+ return responseHandler .handler (this , url , httpResponse );
131
150
}
132
151
133
152
0 commit comments