Skip to content

Commit 200c8e8

Browse files
committed
add HttpRequest
1 parent 7a7e793 commit 200c8e8

File tree

10 files changed

+215
-136
lines changed

10 files changed

+215
-136
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>1.5.8</version>
15+
<version>1.5.9</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>1.5.8</version>
8+
<version>1.5.9</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-id-generator/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>1.5.8</version>
8+
<version>1.5.9</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security-jwt/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>1.5.8</version>
9+
<version>1.5.9</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security-jwt</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>1.5.8</version>
8+
<version>1.5.9</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

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

+11-121
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,42 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSON;
4-
import com.alibaba.fastjson.serializer.SerializerFeature;
54
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
65
import lombok.extern.slf4j.Slf4j;
7-
import org.springframework.http.*;
8-
import org.springframework.http.client.ClientHttpResponse;
9-
import org.springframework.http.client.SimpleClientHttpRequestFactory;
6+
import org.springframework.http.HttpHeaders;
107
import org.springframework.util.MultiValueMap;
11-
import org.springframework.web.client.DefaultResponseErrorHandler;
12-
import org.springframework.web.client.ResponseErrorHandler;
13-
import org.springframework.web.client.RestTemplate;
14-
import org.springframework.web.util.DefaultUriBuilderFactory;
15-
import org.springframework.web.util.UriComponentsBuilder;
16-
17-
import java.io.IOException;
18-
import java.net.InetSocketAddress;
19-
import java.net.Proxy;
20-
import java.net.URI;
21-
import java.util.Objects;
228

239
@Slf4j
2410
public class HttpClient {
2511

26-
public interface IHttpRequestHandler{
27-
String handler(HttpClient client,String uri,HttpMethod method,HttpHeaders headers,HttpEntity<?> httpEntity);
28-
}
29-
30-
public interface IHttpResponseHandler{
31-
String handler(HttpClient client,String uri,ResponseEntity<String> response);
32-
}
33-
34-
private final RestTemplate restTemplate;
35-
36-
private final IHttpResponseHandler responseHandler;
37-
38-
private final IHttpRequestHandler requestHandler;
39-
40-
private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler() {
41-
42-
public HttpHeaders copyHeaders(HttpHeaders headers){
43-
HttpHeaders httpHeaders = new HttpHeaders();
44-
for (String key:headers.keySet()){
45-
httpHeaders.set(key, String.join(";", Objects.requireNonNull(headers.get(key))));
46-
}
47-
return httpHeaders;
48-
}
49-
50-
@Override
51-
public String handler(HttpClient client, String url, ResponseEntity<String> response) {
52-
if(response.getStatusCode().equals(HttpStatus.OK)){
53-
return response.getBody();
54-
}
55-
56-
if(response.getStatusCode().equals(HttpStatus.NOT_FOUND)){
57-
return response.getBody();
58-
}
59-
60-
if(response.getStatusCode().equals(HttpStatus.FOUND)){
61-
URI uri = URI.create(url);
62-
HttpHeaders headers = response.getHeaders();
63-
String location = Objects.requireNonNull(headers.getLocation()).toString();
64-
String baseUrl = uri.getScheme() + "://" + uri.getHost()+":"+uri.getPort();
65-
String locationUrl = baseUrl+location;
66-
return client.get(locationUrl,copyHeaders(headers),null);
67-
}
68-
return response.getBody();
69-
}
70-
};
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-
80-
private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler() {
81-
@Override
82-
public boolean hasError(ClientHttpResponse response) throws IOException {
83-
if(response.getStatusCode()==HttpStatus.NOT_FOUND){
84-
return false;
85-
}
86-
return super.hasError(response);
87-
}
88-
};
12+
private final HttpRequest httpRequest;
8913

9014
public HttpClient() {
91-
this(null,defaultRequestHandler,defaultResponseHandler);
15+
this.httpRequest = new HttpRequest();
9216
}
9317

94-
public HttpClient(IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) {
95-
this(null,requestHandler,responseHandler);
18+
public HttpClient(HttpRequest.IHttpRequestHandler requestHandler, HttpRequest.IHttpResponseHandler responseHandler) {
19+
this.httpRequest = new HttpRequest(requestHandler,responseHandler);
9620
}
9721

9822
public HttpClient(HttpProxyProperties properties) {
99-
this(properties,defaultRequestHandler,defaultResponseHandler);
23+
this.httpRequest = new HttpRequest(properties);
10024
}
10125

102-
public HttpClient(HttpProxyProperties properties,IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) {
103-
this.requestHandler = requestHandler==null?defaultRequestHandler:requestHandler;
104-
this.responseHandler = responseHandler==null?defaultResponseHandler:responseHandler;
105-
this.restTemplate = RestTemplateContext.getInstance().getRestTemplate();
106-
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
107-
requestFactory.setConnectTimeout(3000);
108-
if (properties != null) {
109-
if (properties.isEnableProxy()) {
110-
log.info("enable proxy {}//:{}:{}", properties.getProxyType(), properties.getProxyHost(), properties.getProxyPort());
111-
requestFactory.setProxy(new Proxy(properties.getProxyType(),
112-
new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort())));
113-
}
114-
}
115-
this.restTemplate.setErrorHandler(defaultErrorHandler);
116-
this.restTemplate.setRequestFactory(requestFactory);
117-
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
118-
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
119-
this.restTemplate.setUriTemplateHandler(uriBuilderFactory);
26+
public HttpClient(HttpProxyProperties properties, HttpRequest.IHttpRequestHandler requestHandler, HttpRequest.IHttpResponseHandler responseHandler) {
27+
this.httpRequest = new HttpRequest(properties, requestHandler, responseHandler);
12028
}
12129

12230
public String post(String url, HttpHeaders headers, JSON jsonObject) {
123-
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(SerializerFeature.WriteMapNullValue), headers);
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);
31+
return httpRequest.getPostRequest(url, headers, jsonObject).execute();
12732
}
12833

12934
public String post(String url, HttpHeaders headers, MultiValueMap<String, String> formData) {
130-
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(formData, headers);
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);
35+
return httpRequest.getPostRequest(url, headers, formData).execute();
13436
}
13537

13638
public String get(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
137-
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
138-
ResponseEntity<String> httpResponse;
139-
if(uriVariables!=null&&!uriVariables.isEmpty()) {
140-
URI uri = UriComponentsBuilder.fromHttpUrl(url)
141-
.queryParams(uriVariables)
142-
.build(true).toUri();
143-
String requestUrl = requestHandler.handler(this,uri.toString(), HttpMethod.GET,headers,httpEntity);
144-
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
145-
}else{
146-
String requestUrl = requestHandler.handler(this,url, HttpMethod.GET,headers,httpEntity);
147-
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
148-
}
149-
return responseHandler.handler(this, url, httpResponse);
39+
return httpRequest.getGetRequest(url, headers, uriVariables).execute();
15040
}
15141

15242

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.http.*;
8+
import org.springframework.http.client.ClientHttpResponse;
9+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
10+
import org.springframework.util.MultiValueMap;
11+
import org.springframework.web.client.DefaultResponseErrorHandler;
12+
import org.springframework.web.client.ResponseErrorHandler;
13+
import org.springframework.web.client.RestTemplate;
14+
import org.springframework.web.util.DefaultUriBuilderFactory;
15+
import org.springframework.web.util.UriComponentsBuilder;
16+
17+
import java.io.IOException;
18+
import java.net.InetSocketAddress;
19+
import java.net.Proxy;
20+
import java.net.URI;
21+
import java.util.Objects;
22+
23+
@Slf4j
24+
public class HttpRequest {
25+
26+
public interface IHttpRequestHandler{
27+
String handler(HttpRequest client, String uri, HttpMethod method, HttpHeaders headers, HttpEntity<?> httpEntity);
28+
}
29+
30+
public interface IHttpResponseHandler{
31+
String handler(HttpRequest client, String uri, ResponseEntity<String> response);
32+
}
33+
34+
private final RestTemplate restTemplate;
35+
36+
private final IHttpResponseHandler responseHandler;
37+
38+
private final IHttpRequestHandler requestHandler;
39+
40+
private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler() {
41+
42+
public HttpHeaders copyHeaders(HttpHeaders headers){
43+
HttpHeaders httpHeaders = new HttpHeaders();
44+
for (String key:headers.keySet()){
45+
httpHeaders.set(key, String.join(";", Objects.requireNonNull(headers.get(key))));
46+
}
47+
return httpHeaders;
48+
}
49+
50+
@Override
51+
public String handler(HttpRequest client, String url, ResponseEntity<String> response) {
52+
if(response.getStatusCode().equals(HttpStatus.OK)){
53+
return response.getBody();
54+
}
55+
56+
if(response.getStatusCode().equals(HttpStatus.NOT_FOUND)){
57+
return response.getBody();
58+
}
59+
60+
if(response.getStatusCode().equals(HttpStatus.FOUND)){
61+
URI uri = URI.create(url);
62+
HttpHeaders headers = response.getHeaders();
63+
String location = Objects.requireNonNull(headers.getLocation()).toString();
64+
String baseUrl = uri.getScheme() + "://" + uri.getHost()+":"+uri.getPort();
65+
String locationUrl = baseUrl+location;
66+
return client.get(locationUrl,copyHeaders(headers),null);
67+
}
68+
return response.getBody();
69+
}
70+
};
71+
72+
private static final IHttpRequestHandler defaultRequestHandler = new IHttpRequestHandler() {
73+
74+
@Override
75+
public String handler(HttpRequest client, String uri, HttpMethod method, HttpHeaders headers, HttpEntity<?> httpEntity) {
76+
return uri;
77+
}
78+
};
79+
80+
private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler() {
81+
@Override
82+
public boolean hasError(ClientHttpResponse response) throws IOException {
83+
if(response.getStatusCode()==HttpStatus.NOT_FOUND){
84+
return false;
85+
}
86+
return super.hasError(response);
87+
}
88+
};
89+
90+
public HttpRequest() {
91+
this(null,defaultRequestHandler,defaultResponseHandler);
92+
}
93+
94+
public HttpRequest(IHttpRequestHandler requestHandler, IHttpResponseHandler responseHandler) {
95+
this(null,requestHandler,responseHandler);
96+
}
97+
98+
public HttpRequest(HttpProxyProperties properties) {
99+
this(properties,defaultRequestHandler,defaultResponseHandler);
100+
}
101+
102+
public HttpRequest(HttpProxyProperties properties, IHttpRequestHandler requestHandler, IHttpResponseHandler responseHandler) {
103+
this.requestHandler = requestHandler==null?defaultRequestHandler:requestHandler;
104+
this.responseHandler = responseHandler==null?defaultResponseHandler:responseHandler;
105+
this.restTemplate = RestTemplateContext.getInstance().getRestTemplate();
106+
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
107+
requestFactory.setConnectTimeout(3000);
108+
if (properties != null) {
109+
if (properties.isEnableProxy()) {
110+
log.info("enable proxy {}//:{}:{}", properties.getProxyType(), properties.getProxyHost(), properties.getProxyPort());
111+
requestFactory.setProxy(new Proxy(properties.getProxyType(),
112+
new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort())));
113+
}
114+
}
115+
this.restTemplate.setErrorHandler(defaultErrorHandler);
116+
this.restTemplate.setRequestFactory(requestFactory);
117+
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
118+
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
119+
this.restTemplate.setUriTemplateHandler(uriBuilderFactory);
120+
}
121+
122+
123+
public Request getPostRequest(String url, HttpHeaders headers, JSON jsonObject) {
124+
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(SerializerFeature.WriteMapNullValue), headers);
125+
String requestUrl = requestHandler.handler(this,url, HttpMethod.POST,headers,httpEntity);
126+
return () -> {
127+
ResponseEntity<String> httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
128+
return responseHandler.handler(HttpRequest.this,url,httpResponse);
129+
};
130+
}
131+
132+
public Request getPostRequest(String url, HttpHeaders headers, MultiValueMap<String, String> formData) {
133+
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(formData, headers);
134+
String requestUrl = requestHandler.handler(this,url ,HttpMethod.POST,headers,httpEntity);
135+
return () -> {
136+
ResponseEntity<String> httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
137+
return responseHandler.handler(HttpRequest.this,url,httpResponse);
138+
};
139+
}
140+
141+
public Request getGetRequest(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
142+
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
143+
String requestUrl;
144+
if(uriVariables!=null&&!uriVariables.isEmpty()) {
145+
URI uri = UriComponentsBuilder.fromHttpUrl(url)
146+
.queryParams(uriVariables)
147+
.build(true).toUri();
148+
requestUrl = requestHandler.handler(this,uri.toString(), HttpMethod.GET,headers,httpEntity);
149+
}else{
150+
requestUrl = requestHandler.handler(this,url, HttpMethod.GET,headers,httpEntity);
151+
}
152+
return () -> {
153+
ResponseEntity<String> httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
154+
return responseHandler.handler(HttpRequest.this, url, httpResponse);
155+
};
156+
}
157+
158+
159+
protected String get(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
160+
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
161+
ResponseEntity<String> httpResponse;
162+
if(uriVariables!=null&&!uriVariables.isEmpty()) {
163+
URI uri = UriComponentsBuilder.fromHttpUrl(url)
164+
.queryParams(uriVariables)
165+
.build(true).toUri();
166+
String requestUrl = requestHandler.handler(this,uri.toString(), HttpMethod.GET,headers,httpEntity);
167+
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
168+
}else{
169+
String requestUrl = requestHandler.handler(this,url, HttpMethod.GET,headers,httpEntity);
170+
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
171+
}
172+
return responseHandler.handler(this, url, httpResponse);
173+
}
174+
175+
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
public interface Request {
4+
5+
String execute();
6+
7+
}

0 commit comments

Comments
 (0)