View Javadoc
1   package com.foxinmy.weixin4j.http.support.okhttp;
2   
3   import java.util.concurrent.TimeUnit;
4   
5   import okhttp3.Cache;
6   import okhttp3.ConnectionPool;
7   import okhttp3.CookieJar;
8   import okhttp3.Dispatcher;
9   import okhttp3.Dns;
10  import okhttp3.Interceptor;
11  import okhttp3.OkHttpClient;
12  
13  import com.foxinmy.weixin4j.http.HttpClient;
14  import com.foxinmy.weixin4j.http.HttpParams;
15  import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
16  
17  /**
18   * 使用OkHttp3
19   * 
20   * @className OkHttpClient3Factory
21   * @author jinyu(foxinmy@gmail.com)
22   * @date 2016年7月25日
23   * @since JDK 1.6
24   */
25  public class OkHttpClient3Factory extends HttpClientFactory {
26  	private volatile OkHttpClient okClient;
27  	private final OkHttpClient.Builder clientBuilder;
28  
29  	public OkHttpClient3Factory() {
30  		clientBuilder = new OkHttpClient.Builder();
31  		clientBuilder
32  				.hostnameVerifier(HttpClientFactory.AllowHostnameVerifier.GLOBAL);
33  		clientBuilder.sslSocketFactory(HttpClientFactory.allowSSLContext()
34  				.getSocketFactory(),
35  				HttpClientFactory.AllowX509TrustManager.GLOBAL);
36  	}
37  
38  	public OkHttpClient3Factory(OkHttpClient.Builder clientBuilder) {
39  		this.clientBuilder = clientBuilder;
40  	}
41  
42  	/**
43  	 * resolve Request.Parameter
44  	 * 
45  	 * */
46  	private void resolveHttpParams(HttpParams params) {
47  		if (params != null) {
48  			clientBuilder.connectTimeout(params.getConnectTimeout(),
49  					TimeUnit.MILLISECONDS);
50  			clientBuilder.readTimeout(params.getReadTimeout(),
51  					TimeUnit.MILLISECONDS);
52  			if (params.getProxy() != null) {
53  				clientBuilder.proxy(params.getProxy());
54  			}
55  			if (params.getSSLContext() != null) {
56  				clientBuilder.sslSocketFactory(params.getSSLContext()
57  						.getSocketFactory(),
58  						HttpClientFactory.AllowX509TrustManager.GLOBAL);
59  			}
60  			if (params.getHostnameVerifier() != null) {
61  				clientBuilder.hostnameVerifier(params.getHostnameVerifier());
62  			}
63  			Dispatcher dispatcher = new Dispatcher();
64  			dispatcher.setMaxRequests(params.getMaxConnections());
65  			dispatcher.setMaxRequestsPerHost(params.getMaxConnectionsPerHost());
66  			clientBuilder.dispatcher(dispatcher);
67  		}
68  	}
69  
70  	public OkHttpClient3Factory setWriteTimeout(int writeTimeout) {
71  		clientBuilder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
72  		return this;
73  	}
74  
75  	public OkHttpClient3Factory addInterceptor(Interceptor interceptor) {
76  		clientBuilder.addInterceptor(interceptor);
77  		return this;
78  	}
79  
80  	public OkHttpClient3Factory retryOnConnectionFailure(
81  			boolean retryOnConnectionFailure) {
82  		clientBuilder.retryOnConnectionFailure(retryOnConnectionFailure);
83  		return this;
84  	}
85  
86  	public OkHttpClient3Factory setCookieJar(CookieJar cookieJar) {
87  		clientBuilder.cookieJar(cookieJar);
88  		return this;
89  	}
90  
91  	public OkHttpClient3Factory setCache(Cache cache) {
92  		clientBuilder.cache(cache);
93  		return this;
94  	}
95  
96  	public OkHttpClient3Factory setConnectionPool(ConnectionPool connectionPool) {
97  		clientBuilder.connectionPool(connectionPool);
98  		return this;
99  	}
100 
101 	public OkHttpClient3Factory setDispatcher(Dispatcher dispatcher) {
102 		clientBuilder.dispatcher(dispatcher);
103 		return this;
104 	}
105 
106 	public OkHttpClient3Factory setDns(Dns dns) {
107 		clientBuilder.dns(dns);
108 		return this;
109 	}
110 
111 	@Override
112 	public HttpClient newInstance(HttpParams params) {
113 		resolveHttpParams(params);
114 		if (okClient == null) {
115 			okClient = clientBuilder.build();
116 		}
117 		return new OkHttpClient3(okClient);
118 	}
119 }