|
1 | 1 | package com.codingapi.springboot.framework.rest;
|
2 | 2 |
|
3 | 3 | import com.alibaba.fastjson.JSON;
|
4 |
| -import com.alibaba.fastjson.serializer.SerializerFeature; |
5 | 4 | import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
|
6 | 5 | 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; |
10 | 7 | 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 | 8 |
|
23 | 9 | @Slf4j
|
24 | 10 | public class HttpClient {
|
25 | 11 |
|
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; |
89 | 13 |
|
90 | 14 | public HttpClient() {
|
91 |
| - this(null,defaultRequestHandler,defaultResponseHandler); |
| 15 | + this.httpRequest = new HttpRequest(); |
92 | 16 | }
|
93 | 17 |
|
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); |
96 | 20 | }
|
97 | 21 |
|
98 | 22 | public HttpClient(HttpProxyProperties properties) {
|
99 |
| - this(properties,defaultRequestHandler,defaultResponseHandler); |
| 23 | + this.httpRequest = new HttpRequest(properties); |
100 | 24 | }
|
101 | 25 |
|
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); |
120 | 28 | }
|
121 | 29 |
|
122 | 30 | 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(); |
127 | 32 | }
|
128 | 33 |
|
129 | 34 | 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(); |
134 | 36 | }
|
135 | 37 |
|
136 | 38 | 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(); |
150 | 40 | }
|
151 | 41 |
|
152 | 42 |
|
|
0 commit comments