Skip to content

Commit d2f2465

Browse files
committed
update Rest
1 parent 16093e7 commit d2f2465

File tree

11 files changed

+231
-149
lines changed

11 files changed

+231
-149
lines changed
Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSON;
4-
import com.codingapi.springboot.framework.rest.properties.RestApiProperties;
4+
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.http.*;
77
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -12,20 +12,59 @@
1212
import java.net.InetSocketAddress;
1313
import java.net.Proxy;
1414
import java.net.URI;
15+
import java.util.Objects;
1516

1617
@Slf4j
1718
public 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
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSONObject;
4-
import com.codingapi.springboot.framework.rest.param.ApiGetParamBuilder;
5-
import com.codingapi.springboot.framework.rest.param.ApiPostParamBuilder;
6-
import com.codingapi.springboot.framework.rest.properties.RestApiProperties;
4+
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
5+
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
76
import lombok.extern.slf4j.Slf4j;
87
import org.springframework.http.HttpHeaders;
98

@@ -16,23 +15,29 @@ public class RestClient {
1615

1716
private final static String EMPTY = "{}";
1817

19-
public RestClient(RestApiProperties restApiProperties, String baseUrl) {
20-
this.httpClient = new HttpClient(restApiProperties,baseUrl);
18+
private final String baseUrl;
19+
20+
public RestClient(HttpProxyProperties httpProxyProperties, String baseUrl) {
21+
this.baseUrl = baseUrl;
22+
this.httpClient = new HttpClient(httpProxyProperties);
2123
}
2224

2325
public RestClient(String baseUrl) {
24-
this.httpClient = new HttpClient(null,baseUrl);
26+
this(null,baseUrl);
2527
}
2628

27-
private String _get(String api, ApiGetParamBuilder paramBuilder) {
29+
private String toUrl(String api) {
30+
return baseUrl + api;
31+
}
32+
private String _get(String api, RestParamBuilder paramBuilder) {
2833
return _get(api,new HttpHeaders(),paramBuilder);
2934
}
3035

31-
private String _get(String api,HttpHeaders headers, ApiGetParamBuilder paramBuilder) {
32-
return httpClient.get(api, headers,paramBuilder!=null?paramBuilder.build():null);
36+
private String _get(String api,HttpHeaders headers, RestParamBuilder paramBuilder) {
37+
return httpClient.get(toUrl(api), headers,paramBuilder!=null?paramBuilder.toFormRequest():null);
3338
}
3439

35-
public String get(String api,HttpHeaders headers, ApiGetParamBuilder paramBuilder) {
40+
public String get(String api,HttpHeaders headers, RestParamBuilder paramBuilder) {
3641
for(int i=0; i< RETRY_COUNT; i++){
3742
try {
3843
return _get(api,headers, paramBuilder);
@@ -43,7 +48,7 @@ public String get(String api,HttpHeaders headers, ApiGetParamBuilder paramBuilde
4348
}
4449
return EMPTY;
4550
}
46-
public String get(String api, ApiGetParamBuilder paramBuilder) {
51+
public String get(String api, RestParamBuilder paramBuilder) {
4752
return get(api,new HttpHeaders(),paramBuilder);
4853
}
4954

@@ -60,15 +65,15 @@ private String _post(String api, JSONObject requestBody) {
6065
}
6166

6267
private String _post(String api, HttpHeaders headers, JSONObject requestBody) {
63-
return httpClient.post(api,headers, requestBody);
68+
return httpClient.post(toUrl(api),headers, requestBody);
6469
}
6570

6671
public String post(String api, JSONObject requestBody) {
6772
return post(api,new HttpHeaders(),requestBody);
6873
}
6974

70-
public String post(String api, ApiPostParamBuilder paramBuilder) {
71-
return post(api,new HttpHeaders(),paramBuilder.build());
75+
public String post(String api, RestParamBuilder paramBuilder) {
76+
return post(api,new HttpHeaders(),paramBuilder.toJsonRequest());
7277
}
7378

7479
public String post(String api,HttpHeaders headers, JSONObject requestBody) {
@@ -83,8 +88,8 @@ public String post(String api,HttpHeaders headers, JSONObject requestBody) {
8388
return EMPTY;
8489
}
8590

86-
public String post(String api,HttpHeaders headers, ApiPostParamBuilder paramBuilder) {
87-
return post(api, headers, paramBuilder.build());
91+
public String post(String api,HttpHeaders headers, RestParamBuilder paramBuilder) {
92+
return post(api, headers, paramBuilder.toJsonRequest());
8893
}
8994

9095

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
4+
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
9+
import java.net.URI;
10+
import java.util.Objects;
11+
12+
public class SessionClient {
13+
14+
private final HttpClient httpClient;
15+
16+
private final HttpHeaders httpHeaders;
17+
18+
public SessionClient(HttpProxyProperties properties) {
19+
HttpClient.IHttpResponseHandler responseHandler = new HttpClient.IHttpResponseHandler() {
20+
21+
public HttpHeaders copyHeaders(HttpHeaders headers) {
22+
for (String key : headers.keySet()) {
23+
httpHeaders.set(key, String.join(";", Objects.requireNonNull(headers.get(key))));
24+
}
25+
return httpHeaders;
26+
}
27+
28+
@Override
29+
public String toResponse(HttpClient client, URI uri, ResponseEntity<String> response) {
30+
if (response.getStatusCode().equals(HttpStatus.OK)) {
31+
return response.getBody();
32+
}
33+
if (response.getStatusCode().equals(HttpStatus.FOUND)) {
34+
HttpHeaders headers = response.getHeaders();
35+
String location = Objects.requireNonNull(headers.getLocation()).toString();
36+
String baseUrl = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort();
37+
String url = baseUrl + location;
38+
return client.get(url, copyHeaders(headers));
39+
}
40+
return response.getBody();
41+
}
42+
};
43+
this.httpClient = new HttpClient(properties, responseHandler);
44+
this.httpHeaders = new HttpHeaders();
45+
}
46+
47+
public SessionClient(){
48+
this(null);
49+
}
50+
51+
public SessionClient addHeader(String key, String value){
52+
this.httpHeaders.add(key, value);
53+
return this;
54+
}
55+
56+
public String post(String url, RestParamBuilder restParam){
57+
return httpClient.post(url,httpHeaders,restParam.toFormRequest());
58+
}
59+
60+
public String get(String url){
61+
return httpClient.get(url,httpHeaders);
62+
}
63+
64+
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/param/ApiGetParam.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/param/ApiGetParamBuilder.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/param/ApiPostParam.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)