View Javadoc
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   * HttpClient工厂生产类:参考netty的InternalLoggerFactory
22   * 
23   * @className HttpClientFactory
24   * @author jinyu(foxinmy@gmail.com)
25   * @date 2015年8月12日
26   * @since JDK 1.6
27   * @see
28   */
29  public abstract class HttpClientFactory {
30  
31  	/**
32  	 * 默认的HttpClient
33  	 */
34  	private static volatile HttpClientFactory defaultFactory = newDefaultFactory();
35  	private static volatile HttpParams defaultParams;
36  
37  	/**
38  	 * NettyHttpClient -> ApacheHttpClient(HttpComponent3&4) ->
39  	 * OkHttpClient(2&3) -> SimpleHttpClient(HttpURLConnection)
40  	 * 
41  	 * @return
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  	 * 获取默认的HttpClient
67  	 * 
68  	 * @return
69  	 */
70  	public static HttpClientFactory getDefaultFactory() {
71  		return defaultFactory;
72  	}
73  
74  	/**
75  	 * 显式设置默认的HttpClient
76  	 * 
77  	 * @param defaultFactory
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  	 * 获取默认的HttpParams
89  	 * 
90  	 * @return
91  	 */
92  	public static HttpParams getDefaultParams() {
93  		return defaultParams;
94  	}
95  
96  	/**
97  	 * Resolve the Http Parameter
98  	 * 
99  	 * @param params
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 	 * 获取HttpClient实例
111 	 * 
112 	 * @return
113 	 */
114 	public static HttpClient getInstance() {
115 		return getInstance(HttpClientFactory.defaultParams);
116 	}
117 
118 	/**
119 	 * 获取HttpClient实例
120 	 * 
121 	 * @param params
122 	 *            Http参数
123 	 * 
124 	 * @return HttpClinet实例
125 	 */
126 	public static HttpClient getInstance(HttpParams params) {
127 		HttpClientFactory clientFactory = getDefaultFactory();
128 		return clientFactory.newInstance(params);
129 	}
130 
131 	/**
132 	 * 获取HttpClient实例
133 	 * 
134 	 * @param params
135 	 *            http参数 可为空
136 	 * @return
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 }