-
Notifications
You must be signed in to change notification settings - Fork 56
/
HttpClientBuilder.java
172 lines (128 loc) · 5.11 KB
/
HttpClientBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package co.infinum.https;
import android.content.res.Resources;
import org.apache.http.client.CookieStore;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
/**
* Builder for creating Apache HttpClient with pinned certificate.
*/
public class HttpClientBuilder {
static final String BOUNCY_CASTLE = "BKS";
static final String TLS = "TLS";
/**
* HTTP scheme name in SchemeRegistry.
*/
static final String HTTP_SCHEME = "http";
/**
* HTTPS scheme name in SchemeRegistry.
*/
static final String HTTPS_SCHEME = "https";
/**
* Default HTTP port.
*/
static final int HTTP_PORT = 80;
/**
* Default HTTPS port.
*/
static final int HTTPS_PORT = 443;
/**
* If set to true all HTTPS requests will ignore other side certificate.
* Beware that this could pose a security risk and should be used only for
* development purposes.
*/
protected boolean ignoreHttpsCertificates = false;
/**
* KeyStore containing certificates for HTTPS requests.
*/
protected KeyStore keyStore = null;
protected HttpParams httpParams = new BasicHttpParams();
protected SchemeRegistry schemeRegistry = new SchemeRegistry();
protected int httpPort = HTTP_PORT;
protected int httpsPort = HTTPS_PORT;
protected CookieStore cookieStore = new BasicCookieStore();
public HttpClientBuilder setConnectionTimeout(int connectionTimeout) {
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
return this;
}
public HttpClientBuilder setSocketTimeout(int socketTimeout) {
HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
return this;
}
public HttpClientBuilder setCookieStore(CookieStore cookieStore) {
this.cookieStore = cookieStore;
return this;
}
public HttpClientBuilder pinCertificates(InputStream resourceStream, char[] password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
ignoreHttpsCertificates = false;
keyStore = KeyStore.getInstance(BOUNCY_CASTLE);
keyStore.load(resourceStream, password);
return this;
}
public HttpClientBuilder pinCertificates(Resources resources, int certificateRawResource, char[] password) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
InputStream in = resources.openRawResource(certificateRawResource);
pinCertificates(in, password);
return this;
}
public HttpClientBuilder ignoreCertificates() {
ignoreHttpsCertificates = true;
return this;
}
public HttpClientBuilder registerScheme(String name, SocketFactory factory, int port) {
schemeRegistry.register(new Scheme(name, factory, port));
return this;
}
public HttpClientBuilder setHttpPort(int port) {
httpPort = port;
return this;
}
public HttpClientBuilder setHttpsPort(int port) {
httpsPort = port;
return this;
}
public DefaultHttpClient build() {
DefaultHttpClient httpClient;
schemeRegistry.register(new Scheme(HTTP_SCHEME, PlainSocketFactory.getSocketFactory(), httpPort));
if (!ignoreHttpsCertificates && keyStore != null) {
try {
schemeRegistry.register(new Scheme(HTTPS_SCHEME, new SSLSocketFactory(keyStore), httpsPort));
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
} else {
schemeRegistry.register(new Scheme(HTTPS_SCHEME, SSLSocketFactory.getSocketFactory(), httpsPort));
}
ThreadSafeClientConnManager clientMan = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
if (ignoreHttpsCertificates) {
httpClient = new IgnorantHttpClient();
} else {
httpClient = new DefaultHttpClient(clientMan, httpParams);
}
if(cookieStore != null) {
httpClient.setCookieStore(cookieStore);
}
return httpClient;
}
}