View Javadoc
1   package com.foxinmy.weixin4j.http.support.apache4;
2   
3   import java.net.InetSocketAddress;
4   
5   import org.apache.http.HttpHost;
6   import org.apache.http.client.HttpRequestRetryHandler;
7   import org.apache.http.client.config.RequestConfig;
8   import org.apache.http.config.ConnectionConfig;
9   import org.apache.http.config.SocketConfig;
10  import org.apache.http.conn.HttpClientConnectionManager;
11  import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
12  import org.apache.http.impl.client.HttpClientBuilder;
13  import org.apache.http.impl.client.HttpClients;
14  import org.apache.http.protocol.HttpProcessor;
15  
16  import com.foxinmy.weixin4j.http.HttpClient;
17  import com.foxinmy.weixin4j.http.HttpParams;
18  import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
19  import com.foxinmy.weixin4j.http.support.apache4.HttpComponent4.CustomHostnameVerifier;
20  import com.foxinmy.weixin4j.util.Consts;
21  
22  /**
23   * 使用Apache的HttpClient>=4.3
24   * 
25   * @className HttpComponent4Factory
26   * @author jinyu(foxinmy@gmail.com)
27   * @date 2015年8月12日
28   * @since JDK 1.6
29   */
30  public class HttpComponent4_2Factory extends HttpClientFactory {
31  
32  	private final HttpClientBuilder clientBuilder;
33  
34  	public HttpComponent4_2Factory() {
35  		clientBuilder = HttpClients.custom().setDefaultConnectionConfig(
36  				ConnectionConfig.custom().setCharset(Consts.UTF_8).build());
37  		clientBuilder
38  				.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
39  		clientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(
40  				HttpClientFactory.allowSSLContext(),
41  				SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
42  	}
43  
44  	public HttpComponent4_2Factory(HttpClientBuilder clientBuilder) {
45  		this.clientBuilder = clientBuilder;
46  	}
47  
48  	public HttpComponent4_2Factory setDefaultConnectionConfig(
49  			ConnectionConfig connectionConfig) {
50  		clientBuilder.setDefaultConnectionConfig(connectionConfig);
51  		return this;
52  	}
53  
54  	public HttpComponent4_2Factory setDefaultSocketConfig(
55  			SocketConfig socketConfig) {
56  		clientBuilder.setDefaultSocketConfig(socketConfig);
57  		return this;
58  	}
59  
60  	public HttpComponent4_2Factory setConnectionManager(
61  			HttpClientConnectionManager connectionManager) {
62  		clientBuilder.setConnectionManager(connectionManager);
63  		return this;
64  	}
65  
66  	public HttpComponent4_2Factory setHttpProcessor(HttpProcessor httpprocessor) {
67  		clientBuilder.setHttpProcessor(httpprocessor);
68  		return this;
69  	}
70  
71  	public HttpComponent4_2Factory retryHandler(
72  			HttpRequestRetryHandler retryHandler) {
73  		clientBuilder.setRetryHandler(retryHandler);
74  		return this;
75  	}
76  
77  	private void resolveHttpParams(HttpParams params) {
78  		if (params != null) {
79  			clientBuilder.setDefaultRequestConfig(RequestConfig.custom()
80  					.setConnectTimeout(params.getConnectTimeout())
81  					.setConnectionRequestTimeout(params.getReadTimeout())
82  					.build());
83  			if (params.getProxy() != null) {
84  				InetSocketAddress socketAddress = (InetSocketAddress) params
85  						.getProxy().address();
86  				HttpHost proxy = new HttpHost(socketAddress.getHostName(),
87  						socketAddress.getPort());
88  				clientBuilder.setProxy(proxy);
89  			}
90  			if (params.getSSLContext() != null) {
91  				clientBuilder
92  						.setSSLSocketFactory(new SSLConnectionSocketFactory(
93  								params.getSSLContext(),
94  								params.getHostnameVerifier() != null ? new CustomHostnameVerifier(
95  										params.getHostnameVerifier())
96  										: SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
97  			}
98  			if (params.getHostnameVerifier() != null) {
99  				clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(
100 						params.getHostnameVerifier()));
101 			}
102 			clientBuilder.setMaxConnTotal(params.getMaxConnections());
103 			clientBuilder.setMaxConnPerRoute(params.getMaxConnectionsPerHost());
104 		}
105 	}
106 
107 	@Override
108 	public HttpClient newInstance(HttpParams params) {
109 		resolveHttpParams(params);
110 		return new HttpComponent4_2(clientBuilder.build());
111 	}
112 }