Revision 697a6784 src/com/rackspace/cloud/files/api/client/CustomHttpClient.java
b/src/com/rackspace/cloud/files/api/client/CustomHttpClient.java | ||
---|---|---|
1 | 1 |
package com.rackspace.cloud.files.api.client; |
2 | 2 |
|
3 |
import android.content.Context; |
|
4 |
import android.util.Log; |
|
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; |
|
5 | 14 |
|
6 | 15 |
import org.apache.http.conn.ClientConnectionManager; |
7 | 16 |
import org.apache.http.conn.scheme.PlainSocketFactory; |
... | ... | |
11 | 20 |
import org.apache.http.impl.client.DefaultHttpClient; |
12 | 21 |
import org.apache.http.impl.conn.SingleClientConnManager; |
13 | 22 |
|
14 |
import com.rackspacecloud.android.R; |
|
15 |
|
|
16 |
import java.io.InputStream; |
|
17 |
import java.security.KeyStore; |
|
23 |
import android.content.Context; |
|
18 | 24 |
|
25 |
import com.rackspacecloud.android.R; |
|
19 | 26 |
|
20 | 27 |
/** |
21 | 28 |
* |
... | ... | |
27 | 34 |
* other version than 2.3.1. |
28 | 35 |
*/ |
29 | 36 |
public class CustomHttpClient extends DefaultHttpClient { |
30 |
|
|
37 |
public static final String X_AUTH_TOKEN="X-Auth-Token"; |
|
38 |
public static final String X_STORAGE_TOKEN="X-Auth-Token"; |
|
31 | 39 |
private static KeyStore trusted; |
32 |
final Context context;
|
|
33 |
|
|
40 |
final Context context; |
|
41 |
|
|
34 | 42 |
public CustomHttpClient(Context context) { |
35 | 43 |
super(); |
36 | 44 |
this.context = context; |
37 | 45 |
} |
38 |
|
|
46 |
|
|
39 | 47 |
@Override |
40 | 48 |
protected ClientConnectionManager createClientConnectionManager() { |
41 | 49 |
SchemeRegistry registry = new SchemeRegistry(); |
42 |
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); |
|
50 |
registry.register(new Scheme("http", PlainSocketFactory |
|
51 |
.getSocketFactory(), 80)); |
|
43 | 52 |
registry.register(new Scheme("https", newSslSocketFactory(), 443)); |
44 |
ClientConnectionManager m = new SingleClientConnManager(getParams(), registry); |
|
53 |
ClientConnectionManager m = new SingleClientConnManager(getParams(), |
|
54 |
registry); |
|
45 | 55 |
return m; |
46 | 56 |
} |
47 | 57 |
|
... | ... | |
49 | 59 |
try { |
50 | 60 |
if (trusted == null) { |
51 | 61 |
trusted = KeyStore.getInstance("BKS"); |
52 |
InputStream in = context.getResources().openRawResource(R.raw.android231); |
|
62 |
InputStream in = context.getResources().openRawResource( |
|
63 |
R.raw.mystore); |
|
53 | 64 |
try { |
54 |
trusted.load(in, "changeit".toCharArray());
|
|
65 |
trusted.load(in, "ez24get".toCharArray());
|
|
55 | 66 |
} finally { |
56 | 67 |
in.close(); |
57 | 68 |
} |
69 |
|
|
58 | 70 |
} |
59 | 71 |
return new SSLSocketFactory(trusted); |
60 | 72 |
} catch (Exception e) { |
61 | 73 |
throw new AssertionError(e); |
62 | 74 |
} |
63 | 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 |
} |
|
64 | 120 |
} |
Also available in: Unified diff