root / src / com / rackspace / cloud / files / api / client / CustomHttpClient.java @ 038ac9a4
History | View | Annotate | Download (3.5 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import java.io.InputStream; |
4 |
import java.security.KeyStore; |
5 |
|
6 |
import javax.net.ssl.HostnameVerifier; |
7 |
import javax.net.ssl.HttpsURLConnection; |
8 |
import javax.net.ssl.SSLContext; |
9 |
import javax.net.ssl.SSLSession; |
10 |
import javax.net.ssl.TrustManager; |
11 |
import javax.net.ssl.X509TrustManager; |
12 |
import javax.security.cert.CertificateException; |
13 |
import javax.security.cert.X509Certificate; |
14 |
|
15 |
import org.apache.http.conn.ClientConnectionManager; |
16 |
import org.apache.http.conn.scheme.PlainSocketFactory; |
17 |
import org.apache.http.conn.scheme.Scheme; |
18 |
import org.apache.http.conn.scheme.SchemeRegistry; |
19 |
import org.apache.http.conn.ssl.SSLSocketFactory; |
20 |
import org.apache.http.impl.client.DefaultHttpClient; |
21 |
import org.apache.http.impl.conn.SingleClientConnManager; |
22 |
|
23 |
import android.content.Context; |
24 |
|
25 |
import com.rackspacecloud.android.R; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author Chmouel Boudjnah <chmouel.boudjnah@rackspace.co.uk>
|
30 |
*
|
31 |
* Custom implementation of HTTPClient using the keystore (in bks
|
32 |
* format) from android 2.3.1 which allow us to connect to London
|
33 |
* CloudFiles SSL host which doesn't work with the default keystore in
|
34 |
* other version than 2.3.1.
|
35 |
*/
|
36 |
public class CustomHttpClient extends DefaultHttpClient { |
37 |
public static final String X_AUTH_TOKEN = "X-Auth-Token"; |
38 |
public static final String X_STORAGE_TOKEN = "X-Auth-Token"; |
39 |
private static KeyStore trusted; |
40 |
final Context context; |
41 |
|
42 |
public CustomHttpClient(Context context) { |
43 |
super();
|
44 |
this.context = context;
|
45 |
} |
46 |
|
47 |
@Override
|
48 |
protected ClientConnectionManager createClientConnectionManager() {
|
49 |
SchemeRegistry registry = new SchemeRegistry();
|
50 |
registry.register(new Scheme("http", PlainSocketFactory |
51 |
.getSocketFactory(), 80));
|
52 |
registry.register(new Scheme("https", newSslSocketFactory(), 443)); |
53 |
ClientConnectionManager m = new SingleClientConnManager(getParams(),
|
54 |
registry); |
55 |
return m;
|
56 |
} |
57 |
|
58 |
private SSLSocketFactory newSslSocketFactory() { |
59 |
try {
|
60 |
if (trusted == null) { |
61 |
trusted = KeyStore.getInstance("BKS"); |
62 |
InputStream in = context.getResources().openRawResource(
|
63 |
R.raw.mystore); |
64 |
try {
|
65 |
trusted.load(in, "ez24get".toCharArray());
|
66 |
} finally {
|
67 |
in.close(); |
68 |
} |
69 |
|
70 |
} |
71 |
return new SSLSocketFactory(trusted); |
72 |
} catch (Exception e) { |
73 |
throw new AssertionError(e); |
74 |
} |
75 |
} |
76 |
|
77 |
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { |
78 |
public boolean verify(String hostname, SSLSession session) { |
79 |
return true; |
80 |
} |
81 |
}; |
82 |
|
83 |
/**
|
84 |
* Trust every server - dont check for any certificate
|
85 |
*/
|
86 |
private static void trustAllHosts() { |
87 |
// Create a trust manager that does not validate certificate chains
|
88 |
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { |
89 |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
90 |
return new java.security.cert.X509Certificate[] {}; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public void checkClientTrusted( |
95 |
java.security.cert.X509Certificate[] arg0, String arg1) |
96 |
throws java.security.cert.CertificateException {
|
97 |
// TODO Auto-generated method stub
|
98 |
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void checkServerTrusted( |
103 |
java.security.cert.X509Certificate[] arg0, String arg1) |
104 |
throws java.security.cert.CertificateException {
|
105 |
// TODO Auto-generated method stub
|
106 |
|
107 |
} |
108 |
} }; |
109 |
|
110 |
// Install the all-trusting trust manager
|
111 |
try {
|
112 |
SSLContext sc = SSLContext.getInstance("TLS"); |
113 |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
114 |
HttpsURLConnection
|
115 |
.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
116 |
} catch (Exception e) { |
117 |
e.printStackTrace(); |
118 |
} |
119 |
} |
120 |
} |