1 package com.foxinmy.weixin4j.http.support.apache4;
2
3 import java.net.InetSocketAddress;
4
5 import javax.net.ssl.SSLContext;
6
7 import org.apache.http.HttpHost;
8 import org.apache.http.conn.ClientConnectionManager;
9 import org.apache.http.conn.params.ConnRoutePNames;
10 import org.apache.http.conn.scheme.Scheme;
11 import org.apache.http.conn.ssl.SSLSocketFactory;
12 import org.apache.http.conn.ssl.X509HostnameVerifier;
13 import org.apache.http.impl.client.DefaultHttpClient;
14 import org.apache.http.impl.conn.PoolingClientConnectionManager;
15 import org.apache.http.params.CoreConnectionPNames;
16 import org.apache.http.params.CoreProtocolPNames;
17
18 import com.foxinmy.weixin4j.http.HttpClient;
19 import com.foxinmy.weixin4j.http.HttpClientException;
20 import com.foxinmy.weixin4j.http.HttpHeaders;
21 import com.foxinmy.weixin4j.http.HttpParams;
22 import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
23 import com.foxinmy.weixin4j.http.support.apache4.HttpComponent4.CustomHostnameVerifier;
24 import com.foxinmy.weixin4j.util.Consts;
25
26
27
28
29
30
31
32
33
34 public class HttpComponent4_1Factory extends HttpClientFactory {
35
36 private final org.apache.http.client.HttpClient httpClient;
37
38
39
40
41
42
43
44 public HttpComponent4_1Factory() {
45 httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
46 httpClient.getParams().setParameter(
47 CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8);
48 httpClient.getParams().setParameter(
49 CoreProtocolPNames.HTTP_ELEMENT_CHARSET, Consts.UTF_8.name());
50 httpClient.getParams().setParameter(
51 CoreProtocolPNames.STRICT_TRANSFER_ENCODING, Consts.UTF_8);
52 httpClient.getParams().setParameter(HttpHeaders.CONTENT_ENCODING,
53 Consts.UTF_8);
54 httpClient.getParams().setParameter(HttpHeaders.ACCEPT_CHARSET,
55 Consts.UTF_8);
56 SSLSocketFactory socketFactory = new SSLSocketFactory(
57 HttpClientFactory.allowSSLContext());
58 socketFactory
59 .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
60 Scheme scheme = new Scheme("https", socketFactory, 443);
61 httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
62 }
63
64 public HttpComponent4_1Factory(org.apache.http.client.HttpClient httpClient) {
65 this.httpClient = httpClient;
66 }
67
68
69
70
71
72
73
74
75
76
77 public void setParameter(String name, Object value) {
78 httpClient.getParams().setParameter(name, value);
79 }
80
81 private void resolveHttpParams(HttpParams params) {
82 if (params != null) {
83 if (params.getProxy() != null) {
84 InetSocketAddress socketAddress = (InetSocketAddress) params
85 .getProxy().address();
86 HttpHost proxy = new HttpHost(socketAddress.getHostName(),
87 socketAddress.getPort());
88 httpClient.getParams().setParameter(
89 ConnRoutePNames.DEFAULT_PROXY, proxy);
90 }
91 httpClient.getParams().setIntParameter(
92 CoreConnectionPNames.CONNECTION_TIMEOUT,
93 params.getConnectTimeout());
94 httpClient.getParams().setIntParameter(
95 CoreConnectionPNames.SO_TIMEOUT, params.getReadTimeout());
96 if (params.getSSLContext() != null) {
97 SSLSocketFactory socketFactory = new SSLSocketFactory(
98 params.getSSLContext());
99 if (params.getHostnameVerifier() != null) {
100 socketFactory
101 .setHostnameVerifier(new CustomHostnameVerifier(
102 params.getHostnameVerifier()));
103 }
104 Scheme scheme = new Scheme("https", socketFactory, 443);
105 httpClient.getConnectionManager().getSchemeRegistry()
106 .register(scheme);
107 }
108 ClientConnectionManager connectionManager = httpClient.getConnectionManager();
109 if(connectionManager instanceof PoolingClientConnectionManager){
110 ((PoolingClientConnectionManager) connectionManager).setMaxTotal(params.getMaxConnections());
111 ((PoolingClientConnectionManager) connectionManager).setDefaultMaxPerRoute(params.getMaxConnectionsPerHost());
112 }
113 }
114 }
115
116 @Override
117 public HttpClient newInstance(HttpParams params) {
118 resolveHttpParams(params);
119 return new HttpComponent4_1(httpClient);
120 }
121 }