root / src / com / rackspace / cloud / files / api / client / CustomHttpClient.java @ 25881fb3
History | View | Annotate | Download (1.9 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
import android.util.Log; |
5 |
|
6 |
import org.apache.http.conn.ClientConnectionManager; |
7 |
import org.apache.http.conn.scheme.PlainSocketFactory; |
8 |
import org.apache.http.conn.scheme.Scheme; |
9 |
import org.apache.http.conn.scheme.SchemeRegistry; |
10 |
import org.apache.http.conn.ssl.SSLSocketFactory; |
11 |
import org.apache.http.impl.client.DefaultHttpClient; |
12 |
import org.apache.http.impl.conn.SingleClientConnManager; |
13 |
|
14 |
import com.rackspacecloud.android.R; |
15 |
|
16 |
import java.io.InputStream; |
17 |
import java.security.KeyStore; |
18 |
|
19 |
|
20 |
/**
|
21 |
*
|
22 |
* @author Chmouel Boudjnah <chmouel.boudjnah@rackspace.co.uk>
|
23 |
*
|
24 |
* Custom implementation of HTTPClient using the keystore (in bks
|
25 |
* format) from android 2.3.1 which allow us to connect to London
|
26 |
* CloudFiles SSL host which doesn't work with the default keystore in
|
27 |
* other version than 2.3.1.
|
28 |
*/
|
29 |
public class CustomHttpClient extends DefaultHttpClient { |
30 |
|
31 |
private static KeyStore trusted; |
32 |
final Context context; |
33 |
|
34 |
public CustomHttpClient(Context context) { |
35 |
super();
|
36 |
this.context = context;
|
37 |
} |
38 |
|
39 |
@Override
|
40 |
protected ClientConnectionManager createClientConnectionManager() {
|
41 |
SchemeRegistry registry = new SchemeRegistry();
|
42 |
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); |
43 |
registry.register(new Scheme("https", newSslSocketFactory(), 443)); |
44 |
ClientConnectionManager m = new SingleClientConnManager(getParams(), registry);
|
45 |
return m;
|
46 |
} |
47 |
|
48 |
private SSLSocketFactory newSslSocketFactory() { |
49 |
try {
|
50 |
if (trusted == null) { |
51 |
trusted = KeyStore.getInstance("BKS"); |
52 |
InputStream in = context.getResources().openRawResource(R.raw.android231);
|
53 |
try {
|
54 |
trusted.load(in, "changeit".toCharArray());
|
55 |
} finally {
|
56 |
in.close(); |
57 |
} |
58 |
} |
59 |
return new SSLSocketFactory(trusted); |
60 |
} catch (Exception e) { |
61 |
throw new AssertionError(e); |
62 |
} |
63 |
} |
64 |
} |