yangys
2024-10-09 7ef593e1e3c35aaeecf9318f0b3941230d3ed002
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.qianwen.mdc.collect.utils;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
/* loaded from: mdc-plugin-0.0.1-SNAPSHOT-删除lib.jar:BOOT-INF/classes/cn/net/hx/mdc/utils/PooledHttpClientUtil.class */
public class PooledHttpClientUtil {
    private static final int DEFAULT_POOL_MAX_TOTAL = 200;
    private static final int DEFAULT_POOL_MAX_PER_ROUTE = 200;
    private static final int DEFAULT_CONNECT_TIMEOUT = 500;
    private static final int DEFAULT_CONNECT_REQUEST_TIMEOUT = 500;
    private static final int DEFAULT_SOCKET_TIMEOUT = 2000;
    private final int maxTotal;
    private final int maxPerRoute;
    private final int connectTimeout;
    private final int connectRequestTimeout;
    private final int socketTimeout;
    private PoolingHttpClientConnectionManager gcm;
    private CloseableHttpClient httpClient;
    private IdleConnectionMonitorThread idleThread;
 
    public PooledHttpClientUtil() {
        this(200, 200, 500, 500, DEFAULT_SOCKET_TIMEOUT);
    }
 
    public PooledHttpClientUtil(int maxTotal, int maxPerRoute, int connectTimeout, int connectRequestTimeout, int socketTimeout) {
        this.maxTotal = maxTotal;
        this.maxPerRoute = maxPerRoute;
        this.connectTimeout = connectTimeout;
        this.connectRequestTimeout = connectRequestTimeout;
        this.socketTimeout = socketTimeout;
        //Registry<ConnectionSocketFactory> registry = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
        //TODO 修改,增加泛型参数,不知道对不对
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
        
        this.gcm = new PoolingHttpClientConnectionManager(registry);
        this.gcm.setMaxTotal(this.maxTotal);
        this.gcm.setDefaultMaxPerRoute(this.maxPerRoute);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectRequestTimeout).build();
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        this.httpClient = httpClientBuilder.setConnectionManager(this.gcm).setDefaultRequestConfig(requestConfig).build();
        this.idleThread = new IdleConnectionMonitorThread(this.gcm);
        this.idleThread.start();
    }
 
    public String doGet(String url) {
        return doGet(url, Collections.EMPTY_MAP, Collections.EMPTY_MAP);
    }
 
    public String doGet(String url, Map<String, Object> params) {
        return doGet(url, Collections.EMPTY_MAP, params);
    }
 
    public String doGet(String url, Map<String, String> headers, Map<String, Object> params) {
        HttpEntity entityRes;
        HttpGet httpGet = params == null ? new HttpGet(url) : new HttpGet(getUrlWithParams(url, params));
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpGet.addHeader(entry.getKey(), entry.getValue());
            }
        }
        CloseableHttpResponse response = null;
        try {
            response = this.httpClient.execute(httpGet);
            if (response == null || response.getStatusLine() == null) {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                    }
                }
                return null;
            }
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200 || (entityRes = response.getEntity()) == null) {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e2) {
                    }
                }
                return null;
            }
            String entityUtils = EntityUtils.toString(entityRes, "UTF-8");
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e3) {
                }
            }
            return entityUtils;
        } catch (IOException e4) {
            if (response != null) {
                try {
                    response.close();
                    return null;
                } catch (IOException e5) {
                    return null;
                }
            }
            return null;
        } catch (Throwable th) {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e6) {
                }
            }
            throw th;
        }
    }
 
    public String doPost(String apiUrl, Map<String, Object> params) {
        return doPost(apiUrl, Collections.EMPTY_MAP, params);
    }
 
    public String doPost(String apiUrl, Map<String, String> headers, Map<String, Object> params) {
        HttpEntity entityRes;
        HttpPost httpPost = new HttpPost(apiUrl);
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue());
            }
        }
        if (params != null && params.size() > 0) {
            HttpEntity entityReq = getUrlEncodedFormEntity(params);
            httpPost.setEntity(entityReq);
        }
        CloseableHttpResponse response = null;
        try {
            response = this.httpClient.execute(httpPost);
            if (response == null || response.getStatusLine() == null) {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                    }
                }
                return null;
            }
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200 || (entityRes = response.getEntity()) == null) {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e2) {
                    }
                }
                return null;
            }
            String entityUtils = EntityUtils.toString(entityRes, "UTF-8");
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e3) {
                }
            }
            return entityUtils;
        } catch (IOException e4) {
            if (response != null) {
                try {
                    response.close();
                    return null;
                } catch (IOException e5) {
                    return null;
                }
            }
            return null;
        } catch (Throwable th) {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e6) {
                }
            }
            throw th;
        }
    }
 
    private HttpEntity getUrlEncodedFormEntity(Map<String, Object> params) {
        List<NameValuePair> pairList = new ArrayList<>(params.size());
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
        }
        return new UrlEncodedFormEntity(pairList, StandardCharsets.UTF_8);
    }
 
    private String getUrlWithParams(String url, Map<String, Object> params) {
        boolean first = true;
        StringBuilder sb = new StringBuilder(url);
        for (String key : params.keySet()) {
            char ch = '&';
            if (first) {
                ch = '?';
                first = false;
            }
            String value = params.get(key).toString();
            try {
                String sval = URLEncoder.encode(value, "UTF-8");
                sb.append(ch).append(key).append("=").append(sval);
            } catch (UnsupportedEncodingException e) {
            }
        }
        return sb.toString();
    }
 
    public void shutdown() {
        this.idleThread.shutdown();
    }
 
    /* loaded from: mdc-plugin-0.0.1-SNAPSHOT-删除lib.jar:BOOT-INF/classes/cn/net/hx/mdc/utils/PooledHttpClientUtil$IdleConnectionMonitorThread.class */
    private class IdleConnectionMonitorThread extends Thread {
        private final HttpClientConnectionManager connMgr;
        private volatile boolean exitFlag = false;
 
        public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
            this.connMgr = connMgr;
            setDaemon(true);
        }
 
        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            while (!this.exitFlag) {
                synchronized (this) {
                    try {
                        wait(2000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.connMgr.closeExpiredConnections();
                this.connMgr.closeIdleConnections(30L, TimeUnit.SECONDS);
            }
        }
 
        public void shutdown() {
            this.exitFlag = true;
            synchronized (this) {
                notify();
            }
        }
    }
}