2323@ Slf4j
2424public class HttpClient {
2525
26+ public interface IHttpRequestHandler {
27+ String handler (HttpClient client ,String uri ,HttpMethod method ,HttpHeaders headers ,HttpEntity <?> httpEntity );
28+ }
29+
2630 public interface IHttpResponseHandler {
27- String toResponse (HttpClient client ,String uri ,ResponseEntity <String > response );
31+ String handler (HttpClient client ,String uri ,ResponseEntity <String > response );
2832 }
2933
3034 private final RestTemplate restTemplate ;
3135
3236 private final IHttpResponseHandler responseHandler ;
3337
38+ private final IHttpRequestHandler requestHandler ;
39+
3440 private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler () {
3541
3642 public HttpHeaders copyHeaders (HttpHeaders headers ){
@@ -42,7 +48,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
4248 }
4349
4450 @ Override
45- public String toResponse (HttpClient client , String url , ResponseEntity <String > response ) {
51+ public String handler (HttpClient client , String url , ResponseEntity <String > response ) {
4652 if (response .getStatusCode ().equals (HttpStatus .OK )){
4753 return response .getBody ();
4854 }
@@ -63,6 +69,14 @@ public String toResponse(HttpClient client, String url, ResponseEntity<String> r
6369 }
6470 };
6571
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+
6680 private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler () {
6781 @ Override
6882 public boolean hasError (ClientHttpResponse response ) throws IOException {
@@ -74,19 +88,20 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
7488 };
7589
7690 public HttpClient () {
77- this (null ,defaultResponseHandler );
91+ this (null ,defaultRequestHandler , defaultResponseHandler );
7892 }
7993
80- public HttpClient (IHttpResponseHandler responseHandler ) {
81- this (null ,responseHandler );
94+ public HttpClient (IHttpRequestHandler requestHandler , IHttpResponseHandler responseHandler ) {
95+ this (null ,requestHandler , responseHandler );
8296 }
8397
8498 public HttpClient (HttpProxyProperties properties ) {
85- this (properties ,defaultResponseHandler );
99+ this (properties ,defaultRequestHandler , defaultResponseHandler );
86100 }
87101
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 ;
90105 this .restTemplate = RestTemplateContext .getInstance ().getRestTemplate ();
91106 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory ();
92107 requestFactory .setConnectTimeout (3000 );
@@ -106,14 +121,16 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
106121
107122 public String post (String url , HttpHeaders headers , JSON jsonObject ) {
108123 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 );
111127 }
112128
113129 public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
114130 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 );
117134 }
118135
119136 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>
123140 URI uri = UriComponentsBuilder .fromHttpUrl (url )
124141 .queryParams (uriVariables )
125142 .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 );
127145 }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 );
129148 }
130- return responseHandler .toResponse (this , url , httpResponse );
149+ return responseHandler .handler (this , url , httpResponse );
131150 }
132151
133152
0 commit comments