@@ -9,11 +9,18 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9
9
import org.apache.http.*;
10
10
import org.apache.http.client.*;
11
11
import org.apache.http.client.methods.*;
12
+ import org.apache.http.conn.*;
13
+ import org.apache.http.conn.scheme.*;
14
+ import org.apache.http.conn.ssl.*;
12
15
import org.apache.http.entity.StringEntity;
13
16
import org.apache.http.impl.client.*;
17
+ import org.apache.http.impl.conn.*;
18
+ import org.apache.http.params.*;
14
19
import org.apache.http.util.EntityUtils;
15
20
16
21
import java.io.File;
22
+ import java.net.Socket;
23
+ import java.net.UnknownHostException;
17
24
import java.net.URLEncoder;
18
25
19
26
import java.util.Map;
@@ -22,15 +29,50 @@ import java.util.List;
22
29
import java.io.IOException;
23
30
import java.io.UnsupportedEncodingException;
24
31
32
+ import java.security.GeneralSecurityException;
33
+ import java.security.KeyManagementException;
34
+ import java.security.KeyStore;
35
+ import java.security.NoSuchAlgorithmException;
36
+ import java.security.SecureRandom;
37
+ import java.security.cert.*;
38
+
39
+ import java.text.DateFormat;
40
+ import java.text.SimpleDateFormat;
41
+
42
+ import java.util.Date;
43
+ import java.util.Random;
44
+ import java.util.logging.Level;
45
+ import java.util.logging.Logger;
46
+
47
+ import javax.crypto.Mac;
48
+ import javax.crypto.spec.SecretKeySpec;
49
+
50
+ import javax.net.ssl.SSLContext;
51
+ import javax.net.ssl.TrustManager;
52
+ import javax.net.ssl.X509TrustManager;
53
+
25
54
public class ApiInvoker {
26
55
private static ApiInvoker INSTANCE = new ApiInvoker();
27
56
private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
28
57
29
58
private HttpClient client = null;
59
+
60
+ private boolean ignoreSSLCertificates = false ;
61
+
62
+ private ClientConnectionManager ignoreSSLConnectionManager;
63
+
64
+ public ApiInvoker() {
65
+ initConnectionManager();
66
+ }
67
+
30
68
public static ApiInvoker getInstance() {
31
69
return INSTANCE;
32
70
}
33
71
72
+ public void ignoreSSLCertificates(boolean ignoreSSLCertificates) {
73
+ this.ignoreSSLCertificates = ignoreSSLCertificates;
74
+ }
75
+
34
76
public void addDefaultHeader(String key, String value) {
35
77
defaultHeaderMap.put(key, value);
36
78
}
@@ -167,8 +209,60 @@ public class ApiInvoker {
167
209
}
168
210
169
211
private HttpClient getClient(String host) {
170
- if (client == null)
171
- client = new DefaultHttpClient();
212
+ if (client == null) {
213
+ if (ignoreSSLCertificates && ignoreSSLConnectionManager != null) {
214
+ // Trust self signed certificates
215
+ client = new DefaultHttpClient(ignoreSSLConnectionManager, new BasicHttpParams());
216
+ } else {
217
+ client = new DefaultHttpClient();
218
+ }
219
+ }
172
220
return client;
173
221
}
174
- }
222
+
223
+ private void initConnectionManager() {
224
+ try {
225
+ final SSLContext sslContext = SSLContext.getInstance(" SSL" );
226
+
227
+ // set up a TrustManager that trusts everything
228
+ TrustManager[] trustManagers = new TrustManager[] {
229
+ new X509TrustManager() {
230
+ public X509Certificate[] getAcceptedIssuers() {
231
+ return null;
232
+ }
233
+ public void checkClientTrusted(X509Certificate[] certs, String authType) { }
234
+ public void checkServerTrusted(X509Certificate[] certs, String authType) { }
235
+ }};
236
+
237
+ sslContext.init(null, trustManagers, new SecureRandom());
238
+
239
+ SSLSocketFactory sf = new SSLSocketFactory((KeyStore)null) {
240
+ private javax.net.ssl.SSLSocketFactory sslFactory = sslContext.getSocketFactory();
241
+
242
+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
243
+ throws IOException, UnknownHostException {
244
+ return sslFactory.createSocket(socket, host, port, autoClose);
245
+ }
246
+
247
+ public Socket createSocket() throws IOException {
248
+ return sslFactory.createSocket();
249
+ }
250
+ };
251
+
252
+ sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
253
+ Scheme httpsScheme = new Scheme("https", sf, 443);
254
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
255
+ schemeRegistry.register(httpsScheme);
256
+ schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
257
+
258
+ ignoreSSLConnectionManager = new SingleClientConnManager(new BasicHttpParams(), schemeRegistry);
259
+ } catch (NoSuchAlgorithmException e) {
260
+ // This will only be thrown if SSL isn' t available for some reason.
261
+ } catch (KeyManagementException e) {
262
+ // This might be thrown when passing a key into init(), but no key is being passed.
263
+ } catch (GeneralSecurityException e) {
264
+ // This catches anything else that might go wrong.
265
+ // If anything goes wrong we default to the standard connection manager.
266
+ }
267
+ }
268
+ }
0 commit comments