-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebClientDevWrapper.java
76 lines (70 loc) · 2.87 KB
/
WebClientDevWrapper.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
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLSocket;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import java.io.IOException;
import javax.net.ssl.*;
public class WebClientDevWrapper {
private static final String TLS_INSTANCE = "TLS";
private static final String HTTPS_PROTOCOL = "https";
private static final int HTTPS_PORT = 443;
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance(TLS_INSTANCE);
X509TrustManager tm = initialiseTrustManager();
X509HostnameVerifier verifier = initialiseHostNameVerifier();
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(verifier);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme(HTTPS_PROTOCOL, ssf, HTTPS_PORT));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private static X509TrustManager initialiseTrustManager(){
return new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
}
private static X509HostnameVerifier initialiseHostNameVerifier(){
return new X509HostnameVerifier() {
@Override
public void verify(String string, SSLSocket ssls)
throws IOException {
}
@Override
public void verify(String string, X509Certificate xc)
throws SSLException {
}
@Override
public void verify(String string, String[] strings, String[] strings1)
throws SSLException {
}
@Override
public boolean verify(String string, SSLSession ssls) {
return true;
}
};
}
}