1 package com.foxinmy.weixin4j.http.factory;
2
3 import java.security.KeyManagementException;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.cert.CertificateException;
6 import java.security.cert.X509Certificate;
7
8 import javax.net.ssl.HostnameVerifier;
9 import javax.net.ssl.SSLContext;
10 import javax.net.ssl.SSLSession;
11 import javax.net.ssl.X509TrustManager;
12
13 import com.foxinmy.weixin4j.http.HttpClient;
14 import com.foxinmy.weixin4j.http.HttpParams;
15 import com.foxinmy.weixin4j.http.support.apache3.HttpComponent3Factory;
16 import com.foxinmy.weixin4j.http.support.apache4.HttpComponent4Factory;
17 import com.foxinmy.weixin4j.http.support.netty.Netty4HttpClientFactory;
18 import com.foxinmy.weixin4j.http.support.okhttp.OkHttpClientFactory;
19
20
21
22
23
24
25
26
27
28
29 public abstract class HttpClientFactory {
30
31
32
33
34 private static volatile HttpClientFactory defaultFactory = newDefaultFactory();
35 private static volatile HttpParams defaultParams;
36
37
38
39
40
41
42
43 private static HttpClientFactory newDefaultFactory() {
44 HttpClientFactory f;
45 try {
46 f = new Netty4HttpClientFactory();
47 } catch (Throwable e1) {
48 try {
49 f = new HttpComponent4Factory();
50 } catch (Throwable e2) {
51 try {
52 f = new HttpComponent3Factory();
53 } catch (Throwable e3) {
54 try {
55 f = new OkHttpClientFactory();
56 } catch (Throwable e4) {
57 f = new SimpleHttpClientFactory();
58 }
59 }
60 }
61 }
62 return f;
63 }
64
65
66
67
68
69
70 public static HttpClientFactory getDefaultFactory() {
71 return defaultFactory;
72 }
73
74
75
76
77
78
79 public static void setDefaultFactory(HttpClientFactory defaultFactory) {
80 if (defaultFactory == null) {
81 throw new IllegalArgumentException(
82 "'defaultFactory' must not be empty");
83 }
84 HttpClientFactory.defaultFactory = defaultFactory;
85 }
86
87
88
89
90
91
92 public static HttpParams getDefaultParams() {
93 return defaultParams;
94 }
95
96
97
98
99
100
101
102 public static void setDefaultParams(HttpParams params) {
103 if (params == null) {
104 throw new IllegalArgumentException("'params' must not be empty");
105 }
106 HttpClientFactory.defaultParams = params;
107 }
108
109
110
111
112
113
114 public static HttpClient getInstance() {
115 return getInstance(HttpClientFactory.defaultParams);
116 }
117
118
119
120
121
122
123
124
125
126 public static HttpClient getInstance(HttpParams params) {
127 HttpClientFactory clientFactory = getDefaultFactory();
128 return clientFactory.newInstance(params);
129 }
130
131
132
133
134
135
136
137
138 public abstract HttpClient newInstance(HttpParams params);
139
140 public static SSLContext allowSSLContext() {
141 try {
142 SSLContext sslContext = SSLContext.getInstance("TLS");
143 sslContext.init(null,
144 new X509TrustManager[] { AllowX509TrustManager.GLOBAL },
145 new java.security.SecureRandom());
146 return sslContext;
147 } catch (NoSuchAlgorithmException e) {
148 throw new RuntimeException(
149 "Create SSLContext NoSuchAlgorithmException:", e);
150 } catch (KeyManagementException e) {
151 throw new RuntimeException(
152 "Create SSLContext KeyManagementException:", e);
153 }
154 }
155
156 public static class AllowX509TrustManager implements X509TrustManager {
157 public static final X509TrustManager GLOBAL = new AllowX509TrustManager();
158
159 private AllowX509TrustManager() {
160 }
161
162 @Override
163 public X509Certificate[] getAcceptedIssuers() {
164 return new X509Certificate[0];
165 }
166
167 @Override
168 public void checkServerTrusted(
169 X509Certificate[] paramArrayOfX509Certificate,
170 String paramString) throws CertificateException {
171 }
172
173 @Override
174 public void checkClientTrusted(
175 X509Certificate[] paramArrayOfX509Certificate,
176 String paramString) throws CertificateException {
177 }
178 }
179
180 public static class AllowHostnameVerifier implements HostnameVerifier {
181 public static final HostnameVerifier GLOBAL = new AllowHostnameVerifier();
182
183 private AllowHostnameVerifier() {
184 }
185
186 @Override
187 public boolean verify(String hostname, SSLSession session) {
188 return true;
189 }
190 }
191 }