11package com .codingapi .springboot .framework .rest ;
22
33import com .alibaba .fastjson .JSON ;
4- import com .codingapi .springboot .framework .rest .properties .RestApiProperties ;
4+ import com .codingapi .springboot .framework .rest .properties .HttpProxyProperties ;
55import lombok .extern .slf4j .Slf4j ;
66import org .springframework .http .*;
77import org .springframework .http .client .SimpleClientHttpRequestFactory ;
1212import java .net .InetSocketAddress ;
1313import java .net .Proxy ;
1414import java .net .URI ;
15+ import java .util .Objects ;
1516
1617@ Slf4j
1718public class HttpClient {
1819
20+ public interface IHttpResponseHandler {
21+ String toResponse (HttpClient client ,URI uri ,ResponseEntity <String > response );
22+ }
23+
1924 private final RestTemplate restTemplate ;
2025
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+ }
2237
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 );
2560 }
2661
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 ;
2968 this .restTemplate = RestTemplateContext .getInstance ().getRestTemplate ();
3069 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory ();
3170 requestFactory .setConnectTimeout (3000 );
@@ -39,37 +78,53 @@ public HttpClient(RestApiProperties properties, String baseUrl) {
3978 this .restTemplate .setRequestFactory (requestFactory );
4079 }
4180
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 );
4983 }
5084
51- public String post (String api , HttpHeaders headers , JSON jsonObject ) {
85+ public String post (String url , HttpHeaders headers , JSON jsonObject ) {
5286 headers .setContentType (MediaType .APPLICATION_JSON );
53- String url = buildUrl (api );
5487 HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (), headers );
5588 ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
5689 return httpResponse .getBody ();
5790 }
5891
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 ) {
60106 headers .setContentType (MediaType .APPLICATION_JSON );
61- String url = buildUrl (api );
62107 UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
63108 if (uriVariables != null ) {
64109 uriComponentsBuilder = uriComponentsBuilder .queryParams (uriVariables );
65110 }
66111 URI uri = uriComponentsBuilder .build ().toUri ();
67112 HttpEntity <String > httpEntity = new HttpEntity <>(headers );
68113 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 );
70120 }
71121
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 );
74124 }
125+
126+ public String get (String url ) {
127+ return get (url , new HttpHeaders (), null );
128+ }
129+
75130}
0 commit comments